Which algorithm handles negative weights but no negative cycles for shortest paths?

Prepare for the GATE General Aptitude and CS Test. Study with comprehensive multiple-choice questions, each equipped with hints and explanations. Master your exam!

Multiple Choice

Which algorithm handles negative weights but no negative cycles for shortest paths?

Explanation:
Bellman-Ford is designed for graphs that may have negative edge weights, as long as there are no negative cycles reachable from the source. It works by relaxing every edge repeatedly; after V-1 passes (where V is the number of vertices), if there are no negative cycles, the shortest distances from the source have stabilized. A final pass can reveal a negative cycle, because further relaxation would still reduce some distance. This combination lets you compute single-source shortest paths even with negative weights, which Dijkstra cannot guarantee since it relies on non-negative weights. Floyd-Warshall can handle negative weights too, but it computes all-pairs shortest paths (and detects negative cycles), not just from a single source. So for single-source shortest paths with possible negative weights and no negative cycles, Bellman-Ford is the right choice.

Bellman-Ford is designed for graphs that may have negative edge weights, as long as there are no negative cycles reachable from the source. It works by relaxing every edge repeatedly; after V-1 passes (where V is the number of vertices), if there are no negative cycles, the shortest distances from the source have stabilized. A final pass can reveal a negative cycle, because further relaxation would still reduce some distance. This combination lets you compute single-source shortest paths even with negative weights, which Dijkstra cannot guarantee since it relies on non-negative weights. Floyd-Warshall can handle negative weights too, but it computes all-pairs shortest paths (and detects negative cycles), not just from a single source. So for single-source shortest paths with possible negative weights and no negative cycles, Bellman-Ford is the right choice.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy