62837Lecture 04 (b) Dijkstras Algorithm
62837Lecture 04 (b) Dijkstras Algorithm
Definition:
Dijkstra's algorithm is a graph search algorithm used to find the shortest path from a starting
node to all other nodes in a graph with non-negative edge weights. It is widely used in network
routing and mapping applications.
How It Works
1. Initialization:
o Start by assigning a distance of 0 to the source node and infinity to all other
nodes.
o Keep a priority queue to store the nodes with their current distances.
2. Iterative Updates:
o Pick the node with the smallest known distance (the closest unvisited node).
o For each of its neighbors, calculate the tentative distance through the current
node.
o If this new distance is smaller than the previously known distance, update it.
3. Repeat:
o Continue until all nodes have been visited or the shortest path to the target node is
found.
4. Reconstruct Path (Optional):
o Use the predecessors recorded during the updates to reconstruct the shortest path.
Step-by-Step Solution
1. Initialization:
o Start at node A.
o Set distances:
Distance: A = 0, B = ∞, C = ∞, D = ∞
o Unvisited nodes: {A, B, C, D}
2. Iteration 1:
o Pick the node with the smallest distance: A (distance = 0).
o Update neighbors:
▪ B: New distance = 0 + 1 = 1.
▪ C: New distance = 0 + 4 = 4.
o Update table:
Distance: A = 0, B = 1, C = 4, D = ∞
o Unvisited nodes: {B, C, D}
3. Iteration 2:
o Pick the node with the smallest distance: B (distance = 1).
o Update neighbors:
▪ C: New distance = 1 + 2 = 3 (better than 4, so update).
▪ D: New distance = 1 + 6 = 7.
o Update table:
Distance: A = 0, B = 1, C = 3, D = 7
o Unvisited nodes: {C, D}
4. Iteration 3:
o Pick the node with the smallest distance: C (distance = 3).
o Update neighbors:
▪ D: New distance = 3 + 3 = 6 (better than 7, so update).
o Update table:
Distance: A = 0, B = 1, C = 3, D = 6
o Unvisited nodes: {D}
5. Iteration 4:
o Pick the node with the smallest distance: D (distance = 6).
o No neighbors to update.
6. Final Table:
Shortest Distances: A = 0, B = 1, C = 3, D = 6
Shortest Paths
• A to A: 0 (itself).
• A to B: A → B (distance = 1).
• A to C: A → B → C (distance = 3).
• A to D: A → B → C → D (distance = 6).
Key Properties
• Non-Negative Weights: Dijkstra's algorithm requires non-negative edge weights.
• Efficiency: Works efficiently for sparse graphs and small-to-medium graph sizes.
Limitations
• Cannot Handle Negative Weights: Use Bellman-Ford algorithm for graphs with
negative weights.
• High Memory Usage: Not suitable for very large graphs.
This example and explanation should clarify the concept and workings of Dijkstra's Algorith
graph: Dictionary where keys are nodes and values are dictionaries of neighbors with edge
weights.
start: The starting node.
Returns:
- A dictionary with the shortest distance from the start node to each node.
- A dictionary with the shortest path (via predecessors) to reconstruct the route.
"""
# Priority queue to hold (distance, node)
priority_queue = []
heapq.heappush(priority_queue, (0, start))
while priority_queue:
current_distance, current_node = heapq.heappop(priority_queue)
Returns:
- A list representing the shortest path from start to goal.
"""
path = []
current = goal
while current is not None:
path.append(current)
current = predecessors[current]
path.reverse()
return path
Output Example
For the graph above:
Shortest distances: {'A': 0, 'B': 1, 'C': 3, 'D': 6}
Shortest path from A to D: ['A', 'B', 'C', 'D']
Explanation
1. Graph Representation: The graph is represented as a dictionary where each node points
to its neighbors along with the edge weights.
2. Priority Queue: The algorithm uses a priority queue (via heapq) to efficiently select the
node with the smallest distance.
3. Distance Table: Keeps track of the shortest distance from the start node to every other
node.
4. Predecessor Table: Records the previous node for each node to reconstruct the shortest
path.
5. Reconstruct Path: Using the predecessor table, the shortest path is reconstructed.
This implementation ensures efficient computation and works well for both small and medium-
sized graphs. Let me know if you need further explanations!