470 likes | 649 Views
Dijkstra, Warshall-Floyd. Presented by Eddy Chan. Introduction. Shortest Path Algorithms Requirements Properties (Complexity in coding and runtime, Advantages & Disadvantages etc.). BFS. BFS can be used to find the shortest path from a source to destinations. S. T. S. T. S. T. S. T.
E N D
Dijkstra, Warshall-Floyd Presented by Eddy Chan
Introduction • Shortest Path Algorithms • Requirements • Properties (Complexity in coding and runtime, Advantages & Disadvantages etc.)
BFS • BFS can be used to find the shortest path from a source to destinations
S T
S T
S T
S T
S T
DFS • DFS can also be used to find the shortest path
S T
S T
S T
S T
S T
S T
S T
S T
S T
S T
S T
S T
Dijkstra Algorithm for each i , d[i] ← ∞ d[s] ← 0 u ← s whilenot (u == t) do for eachvwhere (u, v) in E if d[v] > d[u] + wuvthen d[v] = d[u] + wuv u ← v , where v is not used before and d[v] is min
Dijkstra Algorithm for each i , d[i] ← ∞ d[s] ← 0 u ← s whilenot (u == t) do for eachvwhere (u, v) in E if d[v] > d[u] + wuvthen d[v] = d[u] + wuv p[v] = u u ← v , where v is not used before and d[v] is min
3 3 3 2 S 2 T 3 2 1 3 3 3 3 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 S 0 A I B I C I D I E I F I G I H I T I 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 S 0 0 A I 3 B I 2 C I I D I 3 E I I F I I G I I H I I T I I 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 S 0 0 A I 3 3 B I 2 C I I 4 D I 3 3 E I I I F I I I G I I I H I I I T I I I 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 S 0 0 A I 3 3 B I 2 C I I 4 4 D I 3 3 3 E I I I 6 F I I I I G I I I I H I I I I T I I I I 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 4 S 0 0 A I 3 3 B I 2 C I I 4 4 4 D I 3 3 3 E I I I 6 6 F I I I I I G I I I I I H I I I I 5 T I I I I I 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 4 5 S 0 0 A I 3 3 B I 2 C I I 4 4 4 D I 3 3 3 E I I I 6 6 6 F I I I I I 7 G I I I I I 7 H I I I I 5 5 T I I I I I I 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 4 5 6 S 0 0 A I 3 3 B I 2 C I I 4 4 4 D I 3 3 3 E I I I 6 6 6 6 F I I I I I 7 7 G I I I I I 7 7 H I I I I 5 5 T I I I I I I I 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 4 5 6 7 S 0 0 A I 3 3 B I 2 C I I 4 4 4 D I 3 3 3 E I I I 6 6 6 6 F I I I I I 7 7 7 G I I I I I 7 7 7 H I I I I 5 5 T I I I I I I I I 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 4 5 6 7 8 S 0 0 A I 3 3 B I 2 C I I 4 4 4 D I 3 3 3 E I I I 6 6 6 6 F I I I I I 7 7 7 G I I I I I 7 7 7 7 H I I I I 5 5 T I I I I I I I I 9 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 4 5 6 7 8 9 S 0 0 A I 3 3 B I 2 C I I 4 4 4 D I 3 3 3 E I I I 6 6 6 6 F I I I I I 7 7 7 G I I I I I 7 7 7 7 H I I I I 5 5 T I I I I I I I I 9 8 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 4 5 6 7 8 9 S 0 0 A I 3 3 B I 2 C I I 4 4 4 D I 3 3 3 E I I I 6 6 6 6 F I I I I I 7 7 7 G I I I I I 7 7 7 7 H I I I I 5 5 T I I I I I I I I 10 9 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 4 5 6 7 8 9 S0 A I 3 3 BI2 C II 4 4 4 D I 3 3 3 E I I I 6 6 6 6 F I I I I I 7 7 7 G I I I I I 7 7 7 7 H I I I I 5 5 T I I I I I I I I 10 9 1 3 C G 3 3 D 3 H 2
A 3 E 3 3 B 2 S F 2 T 3 2 0 1 2 3 4 5 6 7 8 9 S 0 0 A I 3 3 B I 2 C I I 4 4 4 2 D I 3 3 3 E I I I 6 6 6 F I I I I I 7 G I I I I I 7 H I I I I 5 5 T I I I I I I 1 3 C G 3 -3 D 3 H 2
Dijkstra Algorithm • Single source shortest path algorithm • If there is a negative edge, what happens? • Complexity: O(V2)
Dijkstra Algorithm • Improvement on the algorithm • Use priority queue to find get the vertex v with minimum d[v] • Complexity: O(V log V) The details will not be taught in this lesson
Warshall-Floyd • All-pairs shortest path algorithm • Negative edges may be present, but not negative cycle
Warshall-Floyd d[i][j] ← wuvif (i,j) is an edge d[i][j] ← ∞ if (i,j) is not an edge for eachi in V for eachjin V for eachkin V if d[j][k] > d[j][i] + d[i][k] d[j][k] ← d[j][i] + d[i][k]
Warshall-Floyd d[i][j] ← wuvif (i,j) is an edge d[i][j] ← ∞ if (i,j) is not an edge for eachi in V for eachjin V for eachkin V if d[j][k] > d[j][i] + d[i][k] d[j][k] ← d[j][i] + d[i][k] p[k]= i
Warshall-Floyd (30048) d[i][j] ← wuvif (i,j) is an edge d[i][j] ← ∞ if (i,j) is not an edge for eachi in V for eachjin V for eachkin V if d[j][k] > max(d[j][i] , d[i][k]) d[j][k] ← max(d[j][i] , d[i][k])
Warshall-Floyd • Be careful of the positions of i,j and k • Complexity: O(V3)