Download presentation
Presentation is loading. Please wait.
Published byMargaret Dayna Blake Modified over 9 years ago
1
Dijkstra’s Algorithm Lauren McLaughlin - 11/7/06
2
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.”
3
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.”
4
Traveling Salesman Objective: Find the cheapest way of visiting all of the cities and returning to your starting point
5
Dining Philosophers Objective: To prevent deadlocks and starvation
6
Dining Philosophers
7
Shortest Path Algorithm
8
Edge Ordered pair u = Vertex @ Beginning of path v = Vertex @ End of path Has a value assigned Cost (u,v)
9
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)
10
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
11
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
12
Pseudocode index i, vnear; char touch[n]; int length[n]; //Empty set F = 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; }
13
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; }
14
Example Objective Find shortest path from source node, A, to all other nodes
16
Shortest Paths ABCDE A07461 B0 C205 D30 E10
17
ABCDE A ∞ 7461 B ∞ C2 ∞ 5 D3 ∞ E1 ∞
18
ABCDE A ∞ 7461 B ∞ C2 ∞ 5 D3 ∞ E1 ∞
20
ABCDE A ∞ 7421 B ∞ C2 ∞ 5 D3 ∞ E1 ∞
21
ABCDE A ∞ 7421 B ∞ C2 ∞ 5 D3 ∞ E1 ∞
23
ABCDE A ∞ 5421 B ∞ C2 ∞ 5 D3 ∞ E1 ∞
24
ABCDE A ∞ 5421 B ∞ C2 ∞ 5 D3 ∞ E1 ∞
26
ABCDE A ∞ 5421 B ∞ C2 ∞ 5 D3 ∞ E1 ∞
27
ABCDE A ∞ 5421 B ∞ C2 ∞ 5 D3 ∞ E1 ∞
29
Running Time O(V 2 ) 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
30
Binary vs. Fibonacci Binary Binary Tree More Structure Fibonacci Collection of trees satisfying the minimum-heap priority Maintains pointer to root of tree with smallest priority More flexible
31
“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.”
32
Movie \\10.1.154.4\Classes\Comp349\Dijkstra
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.