170 likes | 389 Views
Shortest Path Algorithms. Shortest Path Finding. Unweighted Shortest Path All-pairs Shortest-path Single-source Shortest-path Single-source-destination Shortest-path. Single-source Shortest Path. Bellman-Ford algorithm Dijkstra’s algorithm. Dijkstra’s Algorithm. Dijkstra(DG, source)
E N D
Shortest Path Algorithms //pages.cpsc.ucalgary.ca/~verwaal/335
Shortest Path Finding • Unweighted Shortest Path • All-pairs Shortest-path • Single-source Shortest-path • Single-source-destination Shortest-path //pages.cpsc.ucalgary.ca/~verwaal/335
Single-source Shortest Path • Bellman-Ford algorithm • Dijkstra’s algorithm //pages.cpsc.ucalgary.ca/~verwaal/335
Dijkstra’s Algorithm Dijkstra(DG, source) initialize(DG, source) verticesFound queue.addAll(DG.vertices) while (queue not empty) v queue.deleteMin() add v to verticesFound relaxEdges(v) //pages.cpsc.ucalgary.ca/~verwaal/335
Example b c b c null null 1 1 10 10 2 a 2 4 4 Initialize 9 9 a 0 null 3 3 6 6 5 5 7 7 d e null null 2 2 d e queue: a(0), b(), c(), d(), e() verticesFound: {} //pages.cpsc.ucalgary.ca/~verwaal/335
Example – 1st Iteration 10 a b c b c null null null null 1 1 10 10 10 a 2 a 2 4 1st Iter 4 9 9 0 null 0 null 3 3 6 6 5 5 5 7 7 5 a null null null null 2 2 d e d e 10 5 queue: a(0), b(), c(), d(), e() verticesFound: {} queue: b(), c(), d(), e() verticesFound: {a} //pages.cpsc.ucalgary.ca/~verwaal/335
Example – 2nd Iteration 14 d 8 d b c b c 10 a null 1 10 a null 1 10 10 10 a 2 4 a 2 2nd Iter 4 9 9 0 null 9 3 3 0 null 3 6 5 6 5 5 7 7 7 d 5 a null 2 2 5 a null 2 d e d e 8 14 7 queue: d(5), b(10), c(), e() verticesFound: {a} queue: b(10), c(), e() verticesFound: {a, d} //pages.cpsc.ucalgary.ca/~verwaal/335
Example – 3rd Iteration 13 e b c b c 8 d 14 c 8 d 14 d 1 1 10 10 a 2 a 2 4 3rd Iter 4 9 9 9 9 0 null 0 null 3 3 3 3 6 6 6 5 5 7 7 7 5 a 7 d 5 a 7 d 2 2 2 2 d e d e 13 queue: e(7), b(8), c(14) verticesFound: {a, d} queue: b(8), c(14) verticesFound: {a, d, e} //pages.cpsc.ucalgary.ca/~verwaal/335
Example – 4th Iteration 9 b b c b c 8 d 13 e 8 d 13 e 1 1 1 10 10 a 2 a 2 2 4 4th Iter 4 9 9 9 9 0 null 0 null 3 3 3 3 6 6 6 6 5 5 7 7 7 7 5 a 7 d 5 a 7 d 2 2 2 2 d e d e 9 queue: b(8), c(13) verticesFound: {a, d, e} queue: c(13) verticesFound: {a, d, e, b} //pages.cpsc.ucalgary.ca/~verwaal/335
Example – 5th Iteration b c b c 8 d 9 b 8 d 9 b 1 1 1 1 10 10 a 2 a 2 2 4 4 5th Iter 2 4 9 9 9 9 0 null 0 null 3 3 3 3 6 6 6 6 5 5 7 7 7 7 5 a 7 d 5 a 7 d 2 2 2 2 d e d e queue: c(9) verticesFound: {a, d, e, b} queue: verticesFound: {a, d, e, b, c} //pages.cpsc.ucalgary.ca/~verwaal/335
Analysis of Dijkstra’s Algorithm • Loop executed |V| times • call removeMin in queue |V| times • decreaseKey in queue called |E| times • Priority Queue implementation: • Array – O(|V|2) • Binary Heap – O(|E|log|V|) • Fibonacci Heap – O(|V|log|V| + |E|) //pages.cpsc.ucalgary.ca/~verwaal/335
All-pairs Shortest-path • Run Dijkstra’s algorithm on all vertices • Recursive Matrix Multiplication • Floyd-Warshall Algorithm //pages.cpsc.ucalgary.ca/~verwaal/335
Recursive Solution • Considers length of shortest path • At most |V| - 1 • Path must contain sub path which is also shortest • The shortest path from u to v either • goes directly from u to v (path length 1), or • goes through some other vertex w //pages.cpsc.ucalgary.ca/~verwaal/335
Divide and Conquer Algorithm APSP-Recursive(G, u, v) if (u = v) return 0 minPathLength for each vertex w in G a APSP-Recursive(G, u, w) if (a+weight(w,v)< minPathLength) minPath a + weight(w,v) return minPathLength //pages.cpsc.ucalgary.ca/~verwaal/335
Analysis of Divide and Conquer • Run time O(2n) • Duplicating work • Instead compute from the bottom-up • Dynamic Programming Algorithm • In table store shortest path considering each vertex as intermediate //pages.cpsc.ucalgary.ca/~verwaal/335
Dynamic Programming Algorithm APSP-DP(G ) // G is adjacency matrix D G for k 1 to |V| do for i 1 to |V| do for j 1 to |V| do if D[i,k] + D(k,j) < D[i,j] D[i,j] = D[i,k] + D(k,j) //pages.cpsc.ucalgary.ca/~verwaal/335