Practice free →
HomeGATE CSEcomputerscienceAlgorithms › Floyd-Warshall detects the presence of a negativ…

Floyd-Warshall detects the presence of a negative cycle by

Arunning V passes of Bellman-Ford after the main loop
Bchecking if the priority queue is empty
Crebuilding the graph and re-running
Dchecking the diagonal: any d[i][i] < 0 flags a negative cycle
Answer & Solution
Correct answer: D. checking the diagonal: any d[i][i] < 0 flags a negative cycle
After the main loop, the shortest distance from i to itself should be 0 (the empty path). If d[i][i] < 0, the algorithm found a shorter cyclic path through i — a negative cycle. This is the cheapest cycle detection in Floyd-Warshall. Bellman-Ford runs separately and detects via the V-th iteration; the queue idea is Dijkstra-only.
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions