Skip to interview prep
Apexed

29. Compare BFS, Dijkstra, Bellman-Ford and Floyd-Warshall for shortest paths.

MediumGraph algorithms

Technical
Show model answer

BFS: unweighted graphs, O(V + E). Dijkstra: non-negative weights, O(E log V) with a heap. Bellman-Ford: allows negative edges and detects negative cycles, O(VE). Floyd-Warshall: all pairs, O(V³), simple for small dense graphs. For a DAG, relax edges in topological order in O(V + E).

Compare BFS, Dijkstra, Bellman-Ford and Floyd-Warshall for shortest paths. — Data structures & algorithms interview question — Apexed