Analysis of Algorithms: Dijkstra's Algorithm
Analysis of Algorithms: Dijkstra's Algorithm
Analysis of
Algorithms
Dijkstra's
Algorithm
Works on both directed and undirected graphs. However, all edges must
have nonnegative weights.
Approach: Greedy
Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge
weights are nonnegative
Output: Lengths of shortest paths (or the shortest paths themselves) from
a given source vertex v∈V to all other vertices
4
► weighted graph 5
5
2
4
3 -2
1 7
2 3
0 1
7
5 -2
6
4 -4
7
directed weighted graph
► weighted graph 5
5
2
4
3 -2
1 7
2 3
0 1
7
5 -2
6
4 -4
8
Dijkstra's algorithm - Pseudocode
return dist
https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/
9
C++ code for Dijkstra’s
Algorithm
https://favtutor.com/blogs/dijkstras-algorithm-cpp#:~:text=Algori
thm%20for%20Dijkstra's%20in%20C%2B%2B&text=Consider%20sour
ce%20vertex%20as%20current,replace%20it%20otherwise%20ign
ore%20it
10
pseudocode for Dijkstra's Algorithm
Shortest path: 0 3 4 2
13
path from 0 to 4
https://youtu.be/T8KcNk6cAHo
6
7
4 8
7 3
5
9 4
8
2
6
Shortest path: 0 1 4
14
14
path from 0 to 5
https://youtu.be/T8KcNk6cAHo
6
7
4 8
7 3
5
9 4
8
2
6
Shortest path: 0 1 4 5
17
15
path from 0 to 5
6
7
4 18
7 3
5
9 4
8
2
6
Shortest path: 0 1 2 3 4 5
26
16
path from 0 to 5
6
7
4 -18
7 3
5
9 4
8
2
6
Shortest path: 0 1 4 5
-9
17
Dijkstra's algorithm example on
an undirected graph
18
Dijkstra's algorithm example on
an undirected graph cont’d
19
Dijkstra's algorithm example on
an undirected graph cont’d
20
Dijkstra's algorithm example on
an undirected graph cont’d
21
Dijkstra's algorithm example on
an undirected graph cont’d
Update
If (d[u]+c(u,v)<d[v])
d[v]= d[u]+c(u,v)
22
Dijkstra's algorithm example on
an undirected graph cont’d
23
Dijkstra's algorithm example on
an undirected graph cont’d
24
Dijkstra's algorithm example on
an undirected graph cont’d
Update
If (d[u]
+c(u,v)<d[v])
d[v]= d[u]+c(u,v)
25
Dijkstra's algorithm example on
an undirected graph cont’d
26
Dijkstra's algorithm example on
an undirected graph cont’d
27
Dijkstra's algorithm example on
an undirected graph cont’d
Update
If (d[u]+c(u,v)<d[v])
d[v]= d[u]+c(u,v)
28
Dijkstra's algorithm example on
an undirected graph cont’d
29
Dijkstra's algorithm example on
an undirected graph cont’d
30
path from Previous vertex
31
Dijkstra's algorithm example on
a directed graph
7
4 Update
2
1 If (d[u]+c(u,v)<d[v])
2 d[v]= d[u]+c(u,v)
1 2 6
0 1
4 5
3 5
3
32
Dijkstra's algorithm example on
a directed graph
Update
8 If (d[u]+c(u,v)<d[v])
9
2 ∞ d[v]= d[u]+c(u,v)
2 7 4 9
11
1
2 ∞
1 2 6 V D[V]
0 1
2 2
4 5 3 3
3 5 4 8
3 6
3
∞ 5 6
4
6 9
33
Homework: Dijkstra's algorithm
on a directed graph
15 35
10 20 30
5 6
4
15 3
34
Dijkstra's algorithm
Selected 2 3 4 5 6
node
4 50 45 10 Max Max
5 50 45 10 25 Max
Start from 1 2 45 45 10 25 Max
3 45 45 10 25 Max
6 45 45 10 25 Max
45
1 3
50 2 10
15 35
10 20 30
5 6
4
15 3
35
Homework: Dijkstra's algorithm
36
Homework: Dijkstra's algorithm
37
Homework: Dijkstra's algorithm
Shortest path: 0 7 6 5 4
38
39
40
Homework: Dijkstra's algorithm
41
42
43
Implementations and Running Times
The simplest implementation is to store vertices in an array
or linked list. This will produce a running time of
O(|V|^2 + |E|)
For sparse graphs, or graphs with very few edges and many
nodes, it can be implemented more efficiently storing the
graph in an adjacency list using a binary heap or priority
queue. This will produce a running time of