Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.

Similar presentations


Presentation on theme: "Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni."— Presentation transcript:

1 Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

2 Minimum Cost Path Weighted, digraph G, or network Directed simple path p from s to t Cost(p) = sum of edge weights on p Minimum cost path p from s to t in G such that no other path p' from s to t in G has cost(p') < cost(p)

3 Minimum Cost Path s = 1, t = 10 What is minimum cost path? 2 3 8 10 1 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 8 2

4 A Spanning Tree path cost = 28 Is there a cheaper path? 2 3 8 10 1 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 8 2

5 A Spanning Tree path cost = 25 Is there a cheaper path? 2 3 8 10 1 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 8 2

6 A Spanning Tree path cost = 24 Is there a cheaper path? 2 3 8 10 1 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 8 2

7 Shortest Path Problems No negative weight edges allowed! s-t shortest path – Single source, destination – Fastest route to Epcot Single Source Shortest Path – Best routes from A to anywhere All Pairs Shortest Paths – Routing tables in network nodes

8 Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm

9 Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm

10 Dijkstra's Algorithm Very similar to Prim's algorithm Differences are that – Paths form a rooted tree (whereas MST is not rooted) – Graph is directed (not undirected) Grow known shortest paths from source one edge at a time Priority of edge is different

11 Dijkstra's Algorithm Set Known nodes K = {s} Set dist(s) = 0, dist(u) = ∞ for all other nodes u in G Set pred(s) = s, pred(u) = NULL for all other nodes u in G Set Seen nodes S = {neighbors of s} Set pred(u) = s for all nodes in S

12 Dijkstra's Algorithm While |K| < V Find nearest node v in S Add v to K For each edge (v,w) in E If dist(v) + cost(v,w) < dist(w) Add w to S Pred(w) = v dist(w) = dist(v) + cost(v,w)

13 Dijkstra's Algorithm s = 1 Grow SPT by adding node nearest to s 4 8 6 6 7 5 2 4 4 5 3 8 2 6 7 2 4 5 3 1 8 9 10 11 7 5 6 5 5 6 7 2 4 5 3 1 8 9 10 11 0 2 4 2 4 6 5 3 7 12 11 14 9 13 7 9 17 21 22 8 11 10 19 Update best distances

14 Dijkstra's Algorithm Correct Builds shortest path tree (SPT) Always adds nearest seen node v Path takes into account all nodes in K No other node x not in K can be closer than v Hence no path through x could be shorter than the path we have to v

15 Dijkstra's Algorithm Complexity Initialization – O(V) Select next node – O(V) linear list Update dist, pred – O(E) total Selection done V-1 times Total time – O(V 2 + E) = O(V 2 ) Using linear list for S

16 Dijkstra's Algorithm Complexity MinHeap – O((V+E) lg V) V removals, E changes to S What if G is dense – E is O(V 2 )? Worse!! Fibonacci Heap – O(V lg V + E) Even better!

17 Dijkstra's Algorithm Solves single source shortest path Builds SPT For s-t shortest path, Just stop when t is added to K Also works for sink-trees (take edges in reverse direction)

18 Dijkstra's Algorithm Does NOT work with negative edge weights! (Violates assumption needed by greedy method) Can be used to solve all pairs shortest path Build SPT from each node Complexity of SSSP times V

19 Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm

20 Shortest Path Algorithms Floyd's Algorithm Single source shortest paths Works like Warshall’s algorithm for reachability Except takes path costs into account

21 Floyd’s Algorithm Recall Warshall's Algorithm For each intermediate node i For each source s For each destination t s reaches t if s already reaches t or if s reaches i and i reaches t

22 Floyd’s Algorithm Now just track distances For each intermediate node i For each source s For each destination t cost(s,t) = lesser of cost(s,t) and cost(s,i) + cost(i,t)

23 Floyd’s Algorithm Complexity of Floyd’s algorithm: O(V 3 ) Dynamic Programming and Relaxation Build estimates Improve estimates (node relax) Converge

24 Floyd's Algorithm Consider paths through nodes numbered <1, <2, <3, etc. 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 004129 105132 2050 345036 432360 529210

25 Floyd's Algorithm Paths through nodes numbered <1 3-0-1 better, 3-0-5 worse 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 004129 105132 2050 34586036 432360 529210

26 Floyd's Algorithm Paths through nodes numbered <2 29+32 = 61 > 21 41 0 1 4 5 3 2 29 32 51 21 36 32 50 3645 29 012345 0041927329 105132 2050 34586137011836 432360 52980210

27 Floyd's Algorithm Paths through nodes numbered <3 41 0 1 4 5 3 2 29 32 51 21 36 32 50 3645 29 012345 0041921427329 105110132 2050 34586137011836 432360 52980130210

28 Floyd's Algorithm Paths through nodes numbered <4 41 0 1 4 5 3 2 29 32 51 21 36 32 50 3645 29 012345 0041921427329 114605110132137 29513605016886 34586137011836 4811223236072 51752980130210

29 Floyd's Algorithm Paths through nodes numbered <5 41 0 1 4 5 3 2 29 32 51 21 36 32 50 3645 29 012345 0041921097329 11130516832106 29513605016888 34586137011836 4811223236072 5102295357210

30 Floyd's Algorithm Paths through nodes numbered <6 41 0 1 4 5 3 2 29 32 51 21 36 32 50 3645 29 012345 004182865029 11130516832106 29511705010988 345658905736 4811013236072 5102295357210

31 Floyd’s Algorithm Update successor node when updating minimum cost path Method of choice for APSP for dense graphs Works even with negative weights But not with negative cycles! (can detect at least one)

32 Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm

33 Shortest Path Algorithms Bellman-Ford Algorithm SSSP Compute best cost path by edge relaxation – checking all edges Essentially considers paths of increasing potential length

34 Bellman-Ford Algorithm Initialize: dist[t] = {0 if t==s, else infinity} pred[v] = NULL for all v For (i = 1 to V-1) For each edge e=(u,v) in E If (dist[u] + cost[e] < dist[v]) dist[v] = dist[u] + cost[e] pred[v] = u

35 Bellman-Ford Algorithm Consider paths of length <2, <3, etc. Adjacency matrix is paths of length <2 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 004129 105132 2050 345036 432360 529210

36 Bellman-Ford Algorithm Edge (0,1) 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 004129 105132 2050 34586036 432360 529210 Enter here Leave here

37 Bellman-Ford Algorithm Edge (0,5) doesn’t help Edge (1,2) 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 00419229 105132 2050 34586137036 432360 52980210

38 Bellman-Ford Algorithm Edge (2,3) 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 00419214229 105110132 2050 34586137036 432360 52980130210

39 Bellman-Ford Algorithm Edge (3,0) 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 00419214229 114605110132 295050 34586137036 48132360 51752980130210

40 Bellman-Ford Algorithm Edge (3,5) 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 00419214229 114605110132137 29505086 34586137036 4813236072 51752980130210

41 Bellman-Ford Algorithm Edge (4,2) 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 00419214229 114606410132137 29505086 34586137036 4813236072 51752953130210

42 Bellman-Ford Algorithm Edge (4,3) 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 00419212829 11460646832137 29505086 34586137036 4813236072 5175295357210

43 Bellman-Ford Algorithm Edge (5,1) 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 00419212829 11460646832137 29511505086 34565137036 4811013236072 5175295357210

44 Bellman-Ford Algorithm Edge (5,4) 41 0 1 4 5 3 2 19 32 51 21 36 32 50 3645 29 012345 0041921285029 11460646832137 29511505010786 3456513705736 4811013236072 5175295357210

45 Bellman-Ford Algorithm Well, that was ONE pass – need to do that V times!

46 Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm


Download ppt "Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni."

Similar presentations


Ads by Google