![Page 1: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/1.jpg)
Dijkstra’s AlgorithmLauren McLaughlin - 11/7/06
![Page 2: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/2.jpg)
Quotes
• “Object-oriented programming is an exceptionally bad idea which could only have originated in California.”
• “Don't compete with me: firstly, I have more experience, and secondly, I have chosen the weapons.”
![Page 3: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/3.jpg)
Quotes
• “Program testing can be used to show the presence of bugs, but never to show their absence!”
• “Perfecting oneself is as much unlearning as it is learning.”
![Page 4: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/4.jpg)
Traveling Salesman
• Objective:Find the cheapest way of visiting all of the cities
and returning to your starting point
![Page 5: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/5.jpg)
Dining Philosophers
• Objective:To prevent deadlocks and starvation
![Page 6: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/6.jpg)
Dining Philosophers
![Page 7: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/7.jpg)
Shortest Path Algorithm
![Page 8: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/8.jpg)
Edge
• Ordered pair
• u = Vertex @ Beginning of path
• v = Vertex @ End of path
• Has a value assigned• Cost
• (u,v)
![Page 9: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/9.jpg)
Data Structures
• Set of vertices whose shortest path has been determined (F)
• Set of remaining vertices (R)
• Array containing shortest path (length)
• Adjacency Array (W)
• Array holding current vertex (touch)
![Page 10: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/10.jpg)
Basic Steps
• Set F to empty set
• Initialize arrays
• Find shortest path
• Place its vertex in F
• Remove vertex from R
• Relax vertices in R
• Change length of shortest vertex to -1
![Page 11: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/11.jpg)
Edge Relaxation
• After vertex u is placed in F, every edge in R is relaxed
• Get current shortest distance (length[v])
• Determine distance by going through the recently placed vertex in R
• If the path is shorter when going through u, set length[v] to that amount
![Page 12: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/12.jpg)
Pseudocodeindex i, vnear;char touch[n];int length[n];
//Empty setF = 0;
for(i = 0; i < n; i++){ touch[ i ] = source; if(W[source][ i ] == 0)
W[source][ i ] = oo;
length[ i ] = W[source][ i ];}
}
repeat(n - 1 times){ int min = oo; for(i = 0; i < n; i++) { if(i != source) { if(0 <= length[ i ] < min) { min = length[ i ]; vnear = i; } }
![Page 13: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/13.jpg)
Pseudocode
add vnear to F; remove vnear from R; for(i = 0; i < n; i++) { if(length[vnear] + W[vnear][i] < length[i]) { length[i] = length[vnear] + W[vnear][i]; touch[i] = vnear; } } length[vnear] = -1;}
![Page 14: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/14.jpg)
Example Objective
• Find shortest path from source node, A, to all other nodes
![Page 15: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/15.jpg)
7
46
2
1
1
5
3
A
E B
CD
F = AR = B, C, D, E
![Page 16: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/16.jpg)
Shortest Paths
A B C D E
A 0 7 4 6 1
B 0
C 2 0 5
D 3 0
E 1 0
![Page 17: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/17.jpg)
Shortest Paths
A B C D E
A ∞ 7 4 6 1
B ∞C 2 ∞ 5
D 3 ∞E 1 ∞
![Page 18: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/18.jpg)
Shortest Paths
A B C D E
A ∞ 7 4 6 1
B ∞C 2 ∞ 5
D 3 ∞E 1 ∞
![Page 19: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/19.jpg)
7
46
2
1
1
5
3
A
E B
CD
F = A, ER = B, C, D
![Page 20: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/20.jpg)
Shortest Paths
A B C D E
A ∞ 7 4 2 1
B ∞C 2 ∞ 5
D 3 ∞E 1 ∞
![Page 21: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/21.jpg)
Shortest Paths
A B C D E
A ∞ 7 4 2 1
B ∞C 2 ∞ 5
D 3 ∞E 1 ∞
![Page 22: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/22.jpg)
7
46
2
1
1
5
3
A
E B
CD
F = A, D, ER = B, C
![Page 23: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/23.jpg)
Shortest Paths
A B C D E
A ∞ 5 4 2 1
B ∞C 2 ∞ 5
D 3 ∞E 1 ∞
![Page 24: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/24.jpg)
Shortest Paths
A B C D E
A ∞ 5 4 2 1
B ∞C 2 ∞ 5
D 3 ∞E 1 ∞
![Page 25: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/25.jpg)
7
46
2
1
1
5
3
A
E B
CD
F = A, C, D, ER = B
![Page 26: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/26.jpg)
Shortest Paths
A B C D E
A ∞ 5 4 2 1
B ∞C 2 ∞ 5
D 3 ∞E 1 ∞
![Page 27: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/27.jpg)
Shortest Paths
A B C D E
A ∞ 5 4 2 1
B ∞C 2 ∞ 5
D 3 ∞E 1 ∞
![Page 28: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/28.jpg)
7
46
2
1
1
5
3
A
E B
CD
F = A, B, C, D, ER = EMPTY
![Page 29: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/29.jpg)
Running Time
• O(V2)• Linear Search• Vertices stored in array or linked list
• O((E + V)logV)• Binary heap used as priority queue• Vertices stored in adjacency lists
• O(E + V logV)• Fibonacci heap used as priority queue• Vertices stored in adjacency lists
![Page 30: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/30.jpg)
Binary vs. FibonacciBinary
• Binary Tree
• More Structure
Fibonacci
• Collection of trees satisfying the minimum-heap priority
• Maintains pointer to root of tree with smallest priority
• More flexible
![Page 31: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/31.jpg)
• “The competent programmer is fully aware of the limited size of his own skull. He therefore approaches his task with full humility, and avoids clever tricks like the plague.”
![Page 32: Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f225503460f94c3a288/html5/thumbnails/32.jpg)
Movie
• \\10.1.154.4\Classes\Comp349\Dijkstra