Lecture 2 (Extra) - Dijkstra's Algorithm
Lecture 2 (Extra) - Dijkstra's Algorithm
Dijkstra’s Algorithm
Most. Kaniz Fatema Isha
Md Mehrab Hossain Opi
Recap 2
• We learnt the basic of graph theory in previous semester.
• Nodes, Edges, Paths, etc.
• We saw how we can find the shortest path between two nodes in an unweighted graph.
• Using BFS.
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Shortest Path Problem 3
• What is a path in a graph ?
• A sequence of vertices such that there is an edge between each pair of consecutive
vertices in the sequence.
• is an edge in the set E.
0 3
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Shortest Path Problem 4
• How do we find the shortest path now?
• How can you say a path is longer/shorter than another one?
• We need to assign a value to each path to compare them.
• Let’s call this value the cost of a path.
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Cost of a Path 5
• In an unweighted graph, we considered the cost as number of edges.
1
10 1
0 3
1
1
10
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Cost of a Path 6
• In general, the cost of a path is calculated by summing up the weights of the edges along
the path.
• But you can change the cost function.
• Multiply the weights for example.
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Finding the Shortest Path 7
• Our target is to understand some well-known algorithms that will find the shortest path.
• There are some variations in shortest path.
• Single-pair shortest path.
• Single-Source Shortest Path.
• Single-Destination Shortest Path.
• All-Pairs Shortest Path.
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Single Source Shortest Path Algorithms 8
• From a source vertex s, find the shortest path to all other vertices in the graph.
• Some Common Algorithms
• Dijkstra’s Algorithm
• Bellman-Ford Algorithm
• SPFA (Shortest Path Faster Algorithm)
• Dial’s Algorithm
• Thorup’s Algorithm
• And So on.
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Dijkstra’s Algorithm 9
• Created and published by Dr. Edsger W. Dijkstra a Dutch Computer Scientist.
• Original Paper Titled “A note on two problems in connexion with graphs”
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Dijkstra’s Algorithm 10
• Choose a starting node.
• Let dist[N] be the distance of node N from the source node.
• Initially, set the distance of all the nodes to infinity.
• We will try to improve the distances step by step.
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Dijkstra’s Algorithm 11
1. Mark all nodes an unvisited. Create a set of all the unvisited nodes – unvisited set.
2. Assign to every node a distance from start value.
1. For starting node it is zero, For other nodes it is infinity.
3. From the unvisited set, select the node with smallest finite distance as current node.
4. For the current node, consider all of its unvisited neighbors and update their distance
through the current node.
5. Mark the current node as visited and remove it from the unvisited.
6. Once the loop exits every visited node will contain its shortest distance from the
starting node.
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 12
• Let’s consider the following graph.
• We will select node 0 as starting node.
1 4
2 6 10 2
0 3 6
6 1 6
15
2 5
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 13
• We will need a distance array.
• Initially all will have infinity except 0(source node)
distance
1 4
0 1 2 3 4 5 6
0 2 6 10 2
0 3 6
6 1 6
15
2 5
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 14
• We also need an unvisited set.
• We will maintain a visited array for this purpose.
visited
1 4
0 1 2 3 4 5 6
0 0 0 0 2 6 10 2
0 3 6
6 1 6
15
2 5
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 15
• We need to find the node with minimum distance that is not marked visited.
• It will be the source node initially.
1 4
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 0 0 0 0
distance 0
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 16
• Check all unvisited neighbors of 0 and update their distance.
• For neighbor v of node u,
1 4
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 0 0 0 0
distance 0
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 17
• Finally, mark 0 as visited.
1 4
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 0
1 0 0 0
distance 0
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 18
• We repeat this loop (find unvisited node & update neighbors distance) until there’s no
unvisited node.
1 4
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 1 0 0 0
distance 0
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 19
• Unvisited node with minimum distance.
• 1
1 4
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 1 0 0 0
distance 0
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 20
• Update distance of neighbor (node 3).
1 4
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 1 0 0 0
distance 0 8
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 21
• Finally, mark 1 as visited.
1 4
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 0
1 0
1 0 0
distance 0
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 22
Current Node 2
Neighbor Node 3 4
1
2 6 10 2
0 3 6
6 1 6
15
2 5
No more neighbors.
Mark 2 as visited.
Node 0 1 2 3 4 5 6
visited 1
0 1
0 0 0
distance 0
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 23
Current Node 3
Neighbor Node 4
5 4
1
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 1 1 1 0 0
distance 0 7
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 24
Current Node 4
Neighbor Node 6 4
1
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 1 1 1 10 0
distance 0 7
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 25
Current Node 6
Neighbor Node 5 4
1
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 1 1 1 1 0
distance 0 7
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Algorithm Simulation 26
Current Node 5
Neighbor Node 4
1
2 6 10 2
0 3 6
6 1 6
15
2 5
Node 0 1 2 3 4 5 6
visited 1 1 1 1 0
1 1
distance 0 7
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Implementation 27
Dijkstra (Graph, source)
1. Create vertex set Q
2. for each vertex v in Graph:
1. distance[v]:= infinity
2. add v to Q
3. distance[source] := 0
4. While Q is not empty:
1. u:= vertex in Q with smallest distance[] value
The process of relaxing an edge (u,v) consists of
2. Remove u from Q testing whether going through vertex u
3. For each neighbor v of u where v is in Q: improves the shortest path to vertex v
found so far and, if so, updating distance[v]
1. Relax distance[v]
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Complexity Analysis 28
• What will be the complexity now?
• Initializing the distance array will take
• In the main loop, we find the vertex with minimum distance.
• This will take
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Improving the Complexity 29
• In Dijkstra’s Algorithm, we repeatedly tried to find the minimum value in an array.
• Can you remember any data structure that can find the minimum of an array in constant
time?
• We can use a min-heap in this case.
• The min-heap will support two operations:
• Extract-Min
• Find the node with minimum distance
• Decrease-Key
• Reduce the distance of a given node.
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Pseudocode with Min-Heap 30
Dijkstra(G,s)
1. distance := array of size |G|, initialized to .
2. minHeap := a new min-heap
3. for each vertex v in G:
1. minHeap.insert(v,0) if v is the source else
2. minHeap.insert(v,)
4. while minHeap is not empty:
1. u:= minHeap.extractMin()
2. for each neighbor v of u:
1. alt:= distance[u] + weight(u,v)
2. if alt<distance[v]:
1. distance[v] = alt
2. minHeap.decreaseKey(v,alt)
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
Using STL 31
• We can either use set or priority_queue that supports the operation of heap.
• In both data structure we will store data as pairs – the distance and the index of the
vertex.
• You need to use some workaround while using priority_queue.
• It is a max-heap.
• It does not support removing of a random element.
• Can’t use decreaseKey operation.
• Interestingly, a 2007 technical report concluded the variant of the algorithm not using
decrease-key operations ran faster than the decrease-key variant, with a greater
performance gap for sparse graphs.
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024
References 32
• Introduction to Algorithm, 4th ed, Leiserson, Charles Eric, Ronald L. Rivest, Thomas H.
Cormen, and Clifford Stein.
• Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8 – GeeksforGeeks
• Dijkstra's Shortest Path Algorithm using priority_queue of STL - GeeksforGeeks
• Dijkstra on sparse graphs - Algorithms for Competitive Programming (cp-algorithms.com)
CSE 2202: Algorithm Analysis and Design Laboratory December 21, 2024