Dijkstra's algorithm
graph theory
shortest path
algorithm explanation
computer science

Why does Dijkstra's algorithm work?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Dijkstra's algorithm is a fundamental algorithm used in computer science for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. By understanding its operation and why it reliably finds the shortest path, we can better appreciate its application in various fields such as networking, transportation, and robotics.

Overview of Dijkstra's Algorithm

Dijkstra's algorithm is designed to work on graphs with non-negative edge weights, and it effectively solves the single-source shortest path problem. The essence of the algorithm is to maintain a set of vertices whose shortest path from the source is known, and iteratively extend this set by selecting the edge with the minimal weight leading to a vertex not yet included.

How Dijkstra's Algorithm Works

  1. Initialization: • Assign a tentative distance value to every node: zero for the initial node and infinity for all other nodes. Set the initial node as current and mark all other nodes unvisited. • A priority queue is often utilized to facilitate this process, where nodes are inserted based on their distance values.
  2. Processing Each Node: • For the current node, consider all of its unvisited neighbors. Calculate their tentative distances through the current node. • If the calculated distance of a vertex is less than the current assigned value, update the shortest distance for that vertex.
  3. Updating the Priority Queue: • Insert the updated distances for the neighboring nodes back into the priority queue. • Remove the current node from the queue and mark it as visited (a visited node will not be checked again).
  4. Selecting the Next Node: • Select the unvisited node with the smallest tentative distance, set it as the current node, and repeat the process until all nodes are visited.
  5. Shortest Path Tree Construction: • The algorithm also constructs a shortest path tree, a captured subgraph where the path length to any vertex from the source is minimized.

Correctness of Dijkstra’s Algorithm

Optimal Substructure: Dijkstra's algorithm works by leveraging the graph's optimal substructure property. From any vertex in the shortest path from source to destination, the shortest path to the target must also be a shortest path within the subpath.

Greedy Approach: The algorithm employs a greedy strategy, always opting for the local optimum (the shortest tentative distance vertex) with the hope of finding the global optimum (shortest path to all vertices). This approach is justified in graphs with non-negative weights since extending paths will not increase their weights erroneously.

Proof by Contradiction: Assume there's a vertex `v` where the path found by Dijkstra's is not the shortest. If there existed a shorter path to `v`, it must have been possible to reach `v` with a smaller tentative distance, which should have been selected already due to the nature of the priority queue.

Example

Consider a simple graph with vertices and directed edges:

Initialization: Distances `{A: 0, B: ∞, C: ∞, D: ∞}`. Node `A` is current. • Iteration 1: Visit neighbors of `A`. Update distances: `{A: 0, B: 1, C: 4, D: ∞}`. • Iteration 2: `B` is next (smallest tentative distance). Visit `D` from `B`: `{A: 0, B: 1, C: 4, D: 3}`. • Iteration 3: `D` is next (tentative distance = 3). No further updates as all paths from `D` are not shorter. • Iteration 4: `C` is final. Graph traversal is complete with paths `{A to B: 1, A to C: 4, A to D: 3}`. • Non-negative Weights: It does not support graphs with negative weights. For such cases, the Bellman-Ford algorithm is more suitable. • Dynamic Graphs: Frequent updating of the graph's edge weights or structure requires a fresh recomputation.


Course illustration
Course illustration

All Rights Reserved.