StudyG Logo
Study G
Concept Breakdowns

Dijkstra vs Bellman-Ford Shortest Path Differences

These flashcards cover two foundational shortest-path algorithms tested on coding interviews and CS coursework. Dijkstra's algorithm excels with non-negative weights, while Bellman-Ford handles negative edges and detects negative cycles. Understanding their tradeoffs is essential for graph problem solving in FAANG interviews and algorithms courses.

Interactive Deck

5 Cards
1
Front

What is Dijkstra's algorithm?

Click to reveal
1
Back

Dijkstra's: Greedy algorithm finding shortest paths from a source in graphs with non-negative weights using a min-heap. Time: O((V+E) log V).

2
Front

What is Bellman-Ford algorithm?

Click to reveal
2
Back

Bellman-Ford: Dynamic programming algorithm finding shortest paths that handles negative edge weights and detects negative cycles. Time: O(V×E).

3
Front

When does Dijkstra fail?

Click to reveal
3
Back

Dijkstra fails when the graph contains negative edge weights — it assumes relaxing a node once is sufficient, which breaks with negative edges.

4
Locked

How does Bellman-Ford detect negative cycles?

5
Locked

Time complexity comparison

Master this topic effortlessly.

Study G helps you master any topic effortlessly using proven learning algorithms and smart review timing

Download Study G

Frequently Asked Questions

What is the main difference between Dijkstra and Bellman-Ford?

Dijkstra uses a greedy approach and only works with non-negative edge weights, running in O((V+E) log V). Bellman-Ford uses dynamic programming, handles negative weights, and detects negative cycles but is slower at O(V×E).

Which algorithm should I use for interview problems?

Use Dijkstra when all edge weights are non-negative (most common). Use Bellman-Ford when negative weights are present or when you need to detect negative cycles. Always check edge weight constraints first.

How many relaxation passes does Bellman-Ford perform?

Bellman-Ford performs V-1 relaxation passes over all edges, where V is the number of vertices. Each pass guarantees shortest paths with at most k edges are found after k passes.