Which algorithm is used to detect a negative cycle in a graph?
a) Bellman-Ford Algorithm
b) Dijkstra’s Algorithm
c) Floyd-Warshall Algorithm
d) Prim’s Algorithm
Answer:
a) Bellman-Ford Algorithm
Explanation:
The Bellman-Ford algorithm is used to detect negative weight cycles in a graph. By running the algorithm for one additional iteration beyond the number of vertices, any negative cycle can be detected if the shortest path estimate is updated again.
If a negative cycle exists, the algorithm will indicate that there is no solution for the shortest path, as paths that involve the negative cycle can reduce the path length indefinitely.
While Bellman-Ford can handle negative weights and cycles, Dijkstra’s algorithm cannot. This makes Bellman-Ford a more versatile option for graphs with negative weights.
Reference:
DSA (Data Structures and Algorithms) MCQ Questions and Answers