Data Structures & AlgorithmsModerateWipro · Accenture pattern
Which single-source shortest path algorithm can detect a negative-weight cycle reachable from the source?
- A. Prim
- B. BFS
- C. Dijkstra
- D. Bellman–Ford
Correct answer: D. Bellman–Ford
After V − 1 rounds of relaxation, Bellman–Ford makes one more pass: if any edge can still be relaxed, a negative cycle is reachable. Dijkstra assumes non-negative weights, and Prim builds spanning trees, not paths.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A topological ordering of a directed graph exists if and only if the graph is:
- A heap is stored in a 0-indexed array. What is the index of the parent of the element at index 10, and of its left child?
- Using the master theorem, what is the solution of T(n) = 2T(n/2) + n?
- In the activity selection problem (choose the most non-overlapping intervals), which greedy rule always gives an optimal answer?
- Why does the greedy method (best value-per-weight first) solve the fractional knapsack problem but not the 0/1 knapsack problem?
- How many edges does a complete undirected simple graph on 10 vertices have?