Comparison Mode
Side-by-side: use case, requirements, complexity, strengths, weaknesses, example problems, and a clear “choose this when…”.
BFS vs DFSDijkstra vs Bellman-FordKruskal vs PrimMerge Sort vs Quick SortHeap vs Priority QueueHash Map vs Tree MapBFS vs DijkstraSliding Window vs Two PointersPrefix Sum vs Segment TreeGreedy vs Dynamic ProgrammingMemoization vs TabulationTarjan vs KosarajuSegment Tree vs Fenwick TreeArray vs Linked ListStack vs QueueQuick Sort vs Heap SortKMP vs Rabin-KarpUnion-Find vs DFSTrie vs Hash MapAVL Tree vs Red-Black Tree
DijkstraGraph Algos | Bellman-FordGraph Algos | |
|---|---|---|
| Use case | Single-source shortest paths with non-negative edge weights. | Single-source shortest paths with negative weights, negative-cycle detection, or "at most k edges". |
| Requirements | A min-heap; all weights >= 0. | Only an edge list; no heap. |
| Time complexity | O((V + E) log V) with a binary heap. | O(V * E); O(k * E) when limited to k rounds. |
| Space complexity | O(V) distances plus heap entries up to O(E). | O(V); O(2V) with the previous-round copy needed for the k-edge variant. |
| Strengths | Fast on sparse graphs; each node is settled once, so it stops early when a target is reached. | Handles negative edges; a V-th improving round proves a negative cycle; trivially bounded to k edges. |
| Weaknesses | Wrong answers with negative edges; cannot detect negative cycles; cannot bound the number of edges used. | Slow on large graphs; every round scans every edge. |
| Example problems | Network delay time, path with minimum effort, cheapest flights (without the stop limit). | Cheapest flights within k stops, currency arbitrage detection, negative-cycle checks. |
| Choose this when | Choose Dijkstra when weights are non-negative and the graph is large; it is the default weighted shortest-path tool. | Choose Bellman-Ford when edges can be negative, when you must detect a negative cycle, or when the path may use at most k edges. |