Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortest Path in Weighted Graph : Dijkstra’s Algorithm.

Similar presentations


Presentation on theme: "Shortest Path in Weighted Graph : Dijkstra’s Algorithm."— Presentation transcript:

1 Shortest Path in Weighted Graph : Dijkstra’s Algorithm

2 9-1 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 Dijkstra Algorithm: Finding shortest paths in order sww"w' Closest node to s is 1 hop away w"xx' 2 nd closest node to s is 1 hop away from s or w” xzz' 3rd closest node to s is 1 hop away from s, w”, or x w' Find shortest paths from source s to all other destinations

3 9-2 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 Shortest paths – Dijkstra’s algorithm Single Source Shortest Paths Problem: Given a weighted connected (directed) graph G, find shortest paths from source vertex s to each of the other vertices Dijkstra’s algorithm: Among vertices not already in the tree, it finds vertex u with the smallest sum d v + w(v,u) d v + w(v,u)where v is a vertex for which shortest path has been already found on preceding iterations (such vertices form a tree rooted at s) v is a vertex for which shortest path has been already found on preceding iterations (such vertices form a tree rooted at s) d v is the length of the shortest path from source s to v w(v,u) is the length (weight) of edge from v to u d v is the length of the shortest path from source s to v w(v,u) is the length (weight) of edge from v to u

4 9-3 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 Dijkstra's algorithm Dijkstra's algorithm - is a solution to the single-source shortest path problem in graph theory. Dijkstra's algorithm - is a solution to the single-source shortest path problem in graph theory. Works on both directed and undirected graphs. However, all edges must have nonnegative weights. Approach: Greedy Input: Weighted graph G={E,V} and source vertex v ∈ V, such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex v ∈ V to all other vertices

5 9-4 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 Dijkstra's algorithm - Pseudocode (distance to source vertex is zero) dist[s] ← 0 (distance to source vertex is zero) for all v ∈ V–{s} do dist[v] ← ∞ (set all other distances to infinity) S ← ∅ (S, the set of visited vertices is initially empty) Q ← V (Q, the queue initially contains all vertices) while Q ≠ ∅ (while the queue is not empty) do u ← mindistance(Q,dist)(select the element of Q with the min. distance) S ← S ∪ {u} (add u to list of visited vertices) for all v ∈ neighbors[u] do if dist[v] > dist[u] + w(u, v) (if new shortest path found) then d[v] ← d[u] + w(u, v)(set new value of shortest path) (if desired, add traceback code) return dist

6 9-5 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 3 -5 Example 4 :Dijkstra’s algorithm In the cost adjacency matrix, all entries not shown are + .

7 9-6 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 3 -6 b Time complexity : O(n 2 ), n = |V|.

8 9-7 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 Example 1 : Dijkstra’s algorithm IterationND2D2 D3D3 D4D4 D5D5 D6D6 Initial{1}325  1{1,3}324  3 2{1,2,3}32473 3{1,2,3,6}32453 4{1,2,3,4,6}32453 5{1,2,3,4,5,6}32453 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 1 2 4 5 6 1 1 2 3 2 3 5 2 4 33 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 3 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 3 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 3 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 3 6/5/20167

9 9-8 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 Shortest Paths in Dijkstra’s Algorithm 1 2 4 5 6 1 1 2 3 2 3 5 2 4 33 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 3 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 3 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 3 1 2 4 5 6 1 1 2 3 2 3 5 2 4 3 3 6/5/20168

10 9-9 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 9/15 Example 2:Dijkstra’s Algorithm all nodes settled a,d,c,e,b 44 a,d,c,e 243 adjust w/e a,d,c 2252 adjust w/c a  12500 edcba settled distanceiteration b ca ed 5 2 2 1 10 1 1 4 1 a,d  251 adjust w/d 2 Circled numbers: shortest path lengths from a.

11 9-10 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 6/5/2016 10/15 Trace with Different Weights all nodes settled a,d,e,c,b 54 a,d,e,c 363 adjust w/c a,d,e 2462 adjust w/e a  14600 edcba settled distanceiteration b ca ed 6 2 4 1 10 1 1 3 1 a,d  461 adjust w/d 25 Circled numbers: shortest path lengths from a.

12 9-11 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 Example 3 : d 4 Tree vertices Remaining vertices a(-,0) b(a,3) c(-,∞) d(a,7) e(-,∞) a b 4 e 3 7 6 2 5 c a b d 4 c e 3 7 4 6 2 5 a b d 4 c e 3 7 4 6 2 5 a b d 4 c e 3 7 4 6 2 5 b(a,3) c(b,3+4) d(b,3+2) e(-,∞) d(b,5) c(b,7) e(d,5+4) c(b,7) e(d,9) e(d,9) d a b d 4 c e 3 7 4 6 2 5

13 9-12 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9 Notes on Dijkstra’s algorithm b Correctness can be proven by induction on the number of vertices. b Doesn’t work for graphs with negative weights (whereas Floyd’s algorithm does, as long as there is no negative cycle). Can you find a counterexample for Dijkstra’s algorithm? b Applicable to both undirected and directed graphs b Efficiency O(|V| 2 ) for graphs represented by weight matrix and array implementation of priority queueO(|V| 2 ) for graphs represented by weight matrix and array implementation of priority queue O(|E|log|V|) for graphs represented by adj. lists and min-heap implementation of priority queueO(|E|log|V|) for graphs represented by adj. lists and min-heap implementation of priority queue We prove the invariants: (i) when a vertex is added to the tree, its correct distance is calculated and (ii) the distance is at least those of the previously added vertices.

14 9-13 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 9


Download ppt "Shortest Path in Weighted Graph : Dijkstra’s Algorithm."

Similar presentations


Ads by Google