Algorithm Lecture 12 Dijkstra Algorithm.ppt.Pptx
Algorithm Lecture 12 Dijkstra Algorithm.ppt.Pptx
1
Single-Source Shortest Path Problem
6
Example: Initialization
4 1 3 10
2 2
∞ C D E ∞
5 8 ∞ 4 6
1
F G
∞ ∞
7
Example: Update neighbors'
distance
0 2
2
A B
4 1 3 10
2 2
∞ C D E ∞
5 8 1 4 6
1
Distance(B) = 2 F G
Distance(D) = 1
∞ ∞
8
Example: Remove vertex with
minimum distance
0 2
2
A B
4 1 3 10
2 2
∞ C D E ∞
5 8 1 4 6
1
F G
∞ ∞
9
Example: Update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
Distance(C) = 1 + 2 = 3 F G
Distance(E) = 1 + 2 = 3
Distance(F) = 1 + 8 = 9 9 5
Distance(G) = 1 + 4 = 5
10
Example: Continued...
Pick vertex in List with minimum distance (B) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
Note : distance(D) not
1
F G updated since D is
already known and
9 5 distance(E) not updated
since it is larger than
previously computed
11
Example: Continued...
Pick vertex List with minimum distance (E) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
No updating
9 5
12
Example: Continued...
Pick vertex List with minimum distance (C) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
Distance(F) = 3 + 5 = 8 F G
8 5
13
Example: Continued...
Pick vertex List with minimum distance (G) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
Previous distance
6 5
Distance(F) = min (8, 5+1) = 6
14
Example (end)
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
6 5
Pick vertex not in S with lowest cost (F) and update neighbors
15
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Time Complexity: Using List
The simplest implementation of the Dijkstra's algorithm
stores vertices in an ordinary linked list or array
– Good for dense graphs (many edges)
26