Graphs
Shortest Path
Master Shortest Path algorithms: Dijkstra's priority-queue edge relaxation and Bellman-Ford negative weight cycle detection.
Last Updated: August 2, 2026
•
20 min read
1. Introduction
What is the Shortest Path?
Shortest Path algorithms find the path between two vertices in a graph such that the sum of the weights of its constituent edges is minimized.Why is it Important?
In weighted networks, finding the path with the fewest hops is not enough. We must find the path with the lowest overall cost (distance, time, or cost).Where is it Used?
2. Mental Model: Elastic Strings & Relaxation
Imagine a model of nodes connected by elastic strings representing edges:
V through node U that is shorter than its current path, you loosen/update the string (dist[v] = dist[u] + weight(u,v)).3. Shortest Path Algorithms
1. Dijkstra's Algorithm
Dijkstra's is a greedy algorithm that finds the shortest path from a single source to all other vertices.(distance, vertex). Pop the closest node, relax its neighbors, and push updated distances back.O((V + E) log V) using a binary heap.2. Bellman-Ford Algorithm
Bellman-Ford is a dynamic programming algorithm that solves single-source shortest paths on any graph.V - 1 times.V-th relaxation step still decreases any distance, it means a negative weight cycle exists, making the shortest path undefined (-\infty).O(V × E).4. Visualizing Edge Relaxation
Given nodes A, B, and C with values relaxed iteratively:
dist[B] = inf, dist[C] = inf.A -> B and A -> C: dist[B] = 5, dist[C] = 10.B -> C: dist[C] = min(10, 5 + 2) = 7. Path A -> B -> C is shorter than direct edge A -> C!5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test complexity constraints:Common Mistakes
Warning: 1. Dijkstra on Negative Weights: Attempting to run Dijkstra's on graphs containing negative edge weights. You must use Bellman-Ford instead.
> 2. Duplicate Queue Overhead: Pushing updated nodes onto the priority queue without checking if a shorter path has already been processed (if (d > dist[u]) continue), causing timeouts on large dense graphs.
7. Summary
O((V+E)log V) using Min-Priority Queue.O(V × E).O(V³).8. Quiz
Question 1: Why does Bellman-Ford relax all edges exactly V - 1 times?
Answer: In a graph withV vertices, the longest simple path (without cycles) can contain at most V - 1 edges. Therefore, V - 1 passes are sufficient to propagate shortest path values to all vertices.
Question 2: What is a negative weight cycle?
Answer: A cycle in which the sum of the weights of the edges is negative. Traversing this cycle repeatedly decreases path distance infinitely, so a shortest path does not exist.Question 3: If all edges in a weighted graph have identical weights, what algorithm is most optimal?
Answer: Breadth-First Search (BFS), since the graph behaves identically to an unweighted graph, letting us find shortest paths inO(V + E) time instead of O((V + E)log V).
Question 4: True or False: Dijkstra's algorithm always finds the shortest path in a DAG even if it contains negative edges.
Answer: False. Dijkstra's can fail on any graph with negative edges. However, you can find shortest paths on DAGs with negative weights inO(V + E) time by relaxing edges in topological order.
Question 5: What is the space complexity of Dijkstra's algorithm?
Answer:O(V) auxiliary space to store the distance array and the priority queue elements.