Lecture 14:        Shortest Paths         Steven SkienaDepartment of Computer Science State University of New York Stony B...
Problem of the DaySuppose we are given the minimum spanning tree T of agiven graph G (with n vertices and m edges) and a n...
Shortest PathsFinding the shortest path between two nodes in a graph arisesin many different applications: • Transportatio...
Shortest Paths and Sentence DisambiguationIn our work on reconstructing text typed on an (overloaded)telephone keypad, we ...
r ` q a h g d e d b a                       s£©pif2f2c£`                                                        ¡ y x ¡ y ...
Weighting the Graph                                          Code C 1        Code C 2    Code C 3                         ...
Dynamic programming (the Viterbi algorithm) can be usedon the sentences to obtain the same results, by finding theshortest ...
Shortest Paths: Unweighted GraphsIn an unweighted graph, the cost of a path is just the numberof edges on the shortest pat...
Negative Edge WeightsNote that negative cost cycles render the problem of findingthe shortest path meaningless, since you c...
Dijkstra’s AlgorithmThe principle behind Dijkstra’s algorithm is that ifs, . . . , x, . . . , t is the shortest path from ...
Initialization and UpdateThe shortest path from s to s, d(s, s) = 0. If all edge weightsare positive, the smallest edge in...
Pseudocode: Dijkstra’s Algorithmknown = {s}for i = 1 to n, dist[i] = ∞for each edge (s, v), dist[v] = d(s, v)last=swhile (...
Dijkstra Example                               5          7                           2           2                       ...
Dijkstra’s ImplementationSee how little changes from Prim’s algorithm!dijkstra(graph *g, int start) (* WAS prim(g,start) *...
while (intree[v] == FALSE) {           intree[v] = TRUE;           p = g− edges[v];           while (p ! = NULL) {        ...
Prim’s/Dijkstra’s AnalysisFinding the minimum weight fringe-edge takes O(n) time –just bump through fringe list.After addi...
Improved Time BoundsAn O(m lg n) implementation of Dijkstra’s algorithm wouldbe faster for sparse graphs, and comes from u...
All-Pairs Shortest PathNotice that finding the shortest path between a pair of vertices(s, t) in worst case requires first fi...
Dynamic Programming and Shortest PathsThe four-step approach to dynamic programming is:1. Characterize the structure of an...
InitializationFrom the adjacency matrix, we can construct the followingmatrix:D[i, j] = ∞,       if i = j and (vi , vj ) i...
Characterization Based on Path LengthThere are several ways to characterize the shortest pathbetween two nodes in a graph....
Recurrence on Path LengthLet d[i, j]m be the length of the shortest path from i to j usingat most m edges.What is d[i, j]0...
Not Floyd ImplementationThis gives us a recurrence, which we can evaluate in a bottomup fashion:for i = 1 to n       for j...
Time Analysis – BummerThis is an O(n3 ) algorithm just like matrix multiplication, butit only goes from m to m + 1 edges.S...
The Floyd-Warshall AlgorithmAn alternate recurrence yields a more efficient dynamicprogramming formulation. Number the vert...
Recurrence RelationIn general, adding a new vertex k + 1 helps iff a path goesthrough it, so  d[i, j]k = w[i, j] if k = 0 ...
ImplementationThe following algorithm implements it:do = wfor k = 1 to n      for i = 1 to n             for j = 1 to n   ...
Upcoming SlideShare
Loading in …5
×

Skiena algorithm 2007 lecture14 shortest path

660 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
660
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Skiena algorithm 2007 lecture14 shortest path

  1. 1. Lecture 14: Shortest Paths Steven SkienaDepartment of Computer Science State University of New York Stony Brook, NY 11794–4400http://www.cs.sunysb.edu/∼skiena
  2. 2. Problem of the DaySuppose we are given the minimum spanning tree T of agiven graph G (with n vertices and m edges) and a new edgee = (u, v) of weight w that we will add to G. Give an efficientalgorithm to find the minimum spanning tree of the graphG + e. Your algorithm should run in O(n) time to receivefull credit, although slower but correct algorithms will receivepartial credit.
  3. 3. Shortest PathsFinding the shortest path between two nodes in a graph arisesin many different applications: • Transportation problems – finding the cheapest way to travel between two locations. • Motion planning – what is the most natural way for a cartoon character to move about a simulated environment. • Communications problems – how look will it take for a message to get between two places? Which two locations are furthest apart, ie. what is the diameter of the network.
  4. 4. Shortest Paths and Sentence DisambiguationIn our work on reconstructing text typed on an (overloaded)telephone keypad, we had to select which of many possibleinterpretations was most likely.We constructed a graph where the vertices were the possiblewords/positions in the sentence, with an edge betweenpossible neighboring words.
  5. 5. r ` q a h g d e d b a s£©pif2f2c£` ¡ y x ¡ y C™2C¥n # $ 5 %6 8 $ u 5 %6 7 pt t 21 u $ % 56 54 t 3 w pv v v 0) pt t t & % ( % $ ’ “” „ g “ l k „ “ i ‰ …‰” …‰ £… •€£€’ 2m™•e UjYC•2™h $ 5 %6 8 # $ 5 %6 7 21 % ( $ % 56 54 3 0) % $ # # # #                 ! ! ! ¨¦ ¤ ¢§¥£¡ ¨ ¦ ¤ ¢ ©§¥£¡ ¨ ¦ ¤ ¢ ©£¥£¡ ¨ ¦ ¤ ¢ ©§¥£¡ … “” gf”e … ‘ – ‰” „ “ ˜ … „ €‘ •Y£•55£€—d•™C˜ 2£€—–         #         ! ! ! ¨¦ ¤ ¢§¥£¡ ¨ ¦ ¤ ¢ ©§¥£¡ ¨ ¦ ¤ ¢ ©£¥£¡ ¨ ¦ ¤ ¢ ©§¥£¡ …€‘ “•” “C…€’€YYUˆC€„ ƒ ‘‰ ‡ † … ‚ # 999 B D R D T Q S Q I R Q I G D D B 99 @YXWVHUPPFPPFPPHFECA@9 ¡ y x q C€2ca
  6. 6. Weighting the Graph Code C 1 Code C 2 Code C 3 1 P(W1/C 1) 2 P(W1/C 2) P(W13 3) /C 4 P(#/W 1) P(W11/#) 1 2 1 P(W2/W1) 3 P(W2/C 1) P(W2/C 3) P(W21/#) # P(W22 2) /C # P(W31/#) 1 P(W3/C 1) P(W33 3) /C P(W41/#) P(W41 1) /C P(W43 3) /CThe weight of each edge is a function of the probability thatthese two words will be next to each other in a sentence. ‘hiveme’ would be less than ‘give me’, for example.The final system worked extremely well – identifying over99% of characters correctly based on grammatical andstatistical constraints.
  7. 7. Dynamic programming (the Viterbi algorithm) can be usedon the sentences to obtain the same results, by finding theshortest paths in the underlying DAG.
  8. 8. Shortest Paths: Unweighted GraphsIn an unweighted graph, the cost of a path is just the numberof edges on the shortest path, which can be found in O(n+m)time via breadth-first search.In a weighted graph, the weight of a path between twovertices is the sum of the weights of the edges on a path.BFS will not work on weighted graphs because sometimesvisiting more edges can lead to shorter distance, ie.1 + 1 + 1 + 1 + 1 + 1 + 1 10.Note that there can be an exponential number of shortest pathsbetween two nodes – so we cannot report all shortest pathsefficiently.
  9. 9. Negative Edge WeightsNote that negative cost cycles render the problem of findingthe shortest path meaningless, since you can always looparound the negative cost cycle more to reduce the cost of thepath.Thus in our discussions, we will assume that all edge weightsare positive. Other algorithms deal correctly with negativecost edges.Minimum spanning trees are uneffected by negative costedges.
  10. 10. Dijkstra’s AlgorithmThe principle behind Dijkstra’s algorithm is that ifs, . . . , x, . . . , t is the shortest path from s to t, then s, . . . , xhad better be the shortest path from s to x.This suggests a dynamic programming-like strategy, wherewe store the distance from s to all nearby nodes, and use themto find the shortest path to more distant nodes.
  11. 11. Initialization and UpdateThe shortest path from s to s, d(s, s) = 0. If all edge weightsare positive, the smallest edge incident to s, say (s, x), definesd(s, x).We can use an array to store the length of the shortest path toeach node. Initialize each to ∞ to start.Soon as we establish the shortest path from s to a new nodex, we go through each of its incident edges to see if there is abetter way from s to other nodes thru x.
  12. 12. Pseudocode: Dijkstra’s Algorithmknown = {s}for i = 1 to n, dist[i] = ∞for each edge (s, v), dist[v] = d(s, v)last=swhile (last = t) select v such that dist(v) = minunknown dist(i) for each (v, x), dist[x] = min(dist[x], dist[v] + w(v, x)) last=v known = known ∪ {v}Complexity → O(n2 ).This is essentially the same as Prim’s algorithm.
  13. 13. Dijkstra Example 5 7 2 2 6 4 5 9 3 3 5 7 4 7 1 2 4 A 12 A G Dijkstra(G,A)
  14. 14. Dijkstra’s ImplementationSee how little changes from Prim’s algorithm!dijkstra(graph *g, int start) (* WAS prim(g,start) *){ int i; (* counter *) edgenode *p; (* temporary pointer *) bool intree[MAXV]; (* is the vertex in the tree yet? *) int distance[MAXV]; (* distance vertex is from start *) int v; (* current vertex to process *) int w; (* candidate next vertex *) int weight; (* edge weight *) int dist; (* best current distance from start *) for (i=1; i=g− nvertices; i++) { intree[i] = FALSE; distance[i] = MAXINT; parent[i] = -1; } distance[start] = 0; v = start;
  15. 15. while (intree[v] == FALSE) { intree[v] = TRUE; p = g− edges[v]; while (p ! = NULL) { w = p− y; weight = p− weight;(* CHANGED *) if (distance[w] (distance[v]+weight)) {(* CHANGED *) distance[w] = distance[v]+weight;(* CHANGED *) parent[w] = v; } p = p− next; } v = 1; dist = MAXINT; for (i=1; i= g− nvertices; i++) if ((intree[i] == FALSE) (dist distance[i])) { dist = distance[i]; v = i; } }}
  16. 16. Prim’s/Dijkstra’s AnalysisFinding the minimum weight fringe-edge takes O(n) time –just bump through fringe list.After adding a vertex to the tree, running through itsadjacency list to update the cost of adding fringe vertices(there may be a cheaper way through the new vertex) can bedone in O(n) time.Total time is O(n2 ).
  17. 17. Improved Time BoundsAn O(m lg n) implementation of Dijkstra’s algorithm wouldbe faster for sparse graphs, and comes from using a heap ofthe vertices (ordered by distance), and updating the distanceto each vertex (if necessary) in O(lg n) time for each edge outfrom freshly known vertices.Even better, O(n lg n + m) follows from using Fibonacciheaps, since they permit one to do a decrease-key operationin O(1) amortized time.
  18. 18. All-Pairs Shortest PathNotice that finding the shortest path between a pair of vertices(s, t) in worst case requires first finding the shortest path froms to all other vertices in the graph.Many applications, such as finding the center or diameter ofa graph, require finding the shortest path between all pairs ofvertices.We can run Dijkstra’s algorithm n times (once from eachpossible start vertex) to solve all-pairs shortest path problemin O(n3 ). Can we do better?
  19. 19. Dynamic Programming and Shortest PathsThe four-step approach to dynamic programming is:1. Characterize the structure of an optimal solution.2. Recursively define the value of an optimal solution.3. Compute this recurrence in a bottom-up fashion.4. Extract the optimal solution from computed information.
  20. 20. InitializationFrom the adjacency matrix, we can construct the followingmatrix:D[i, j] = ∞, if i = j and (vi , vj ) is not in ED[i, j] = w(i, j), if (vi, vj ) ∈ ED[i, j] = 0, if i = jThis tells us the shortest path going through no intermediatenodes.
  21. 21. Characterization Based on Path LengthThere are several ways to characterize the shortest pathbetween two nodes in a graph. Note that the shortest pathfrom i to j, i = j, using at most M edges consists of theshortest path from i to k using at most M − 1 edges+W (k, j)for some k.This suggests that we can compute all-pair shortest path withan induction based on the number of edges in the optimalpath.
  22. 22. Recurrence on Path LengthLet d[i, j]m be the length of the shortest path from i to j usingat most m edges.What is d[i, j]0? d[i, j]0 = 0 if i = j = ∞ if i = jWhat if we know d[i, j]m−1 for all i, j? d[i, j]m = min(d[i, j]m−1 , min(d[i, k]m−1 + w[k, j])) = min(d[i, k]m−1 + w[k, j]), 1 ≤ k ≤ isince w[k, k] = 0
  23. 23. Not Floyd ImplementationThis gives us a recurrence, which we can evaluate in a bottomup fashion:for i = 1 to n for j = 1 to n d[i, j]m = ∞ for k = 1 to n d[i, j]0 =Min( d[i, k]m , d[i, k]m−1 + d[k, j])
  24. 24. Time Analysis – BummerThis is an O(n3 ) algorithm just like matrix multiplication, butit only goes from m to m + 1 edges.Since the shortest path between any two nodes must use atmost n edges (unless we have negative cost cycles), we mustrepeat that procedure n times (m = 1 to n) for an O(n4 )algorithm.Although this is slick, observe that even O(n3 log n) isslower than running Dijkstra’s algorithm starting from eachvertex!
  25. 25. The Floyd-Warshall AlgorithmAn alternate recurrence yields a more efficient dynamicprogramming formulation. Number the vertices from 1 to n.Let d[i, j]k be the shortest path from i to j using only verticesfrom 1, 2, ..., k as possible intermediate vertices.What is d[j, j]0 ? With no intermediate vertices, any pathconsists of at most one edge, so d[i, j]0 = w[i, j].
  26. 26. Recurrence RelationIn general, adding a new vertex k + 1 helps iff a path goesthrough it, so d[i, j]k = w[i, j] if k = 0 = min(d[i, j]k−1 , d[i, k]k−1 + d[k, j]k−1 ) if k ≥ 1Although this looks similar to the previous recurrence, it isn’t.
  27. 27. ImplementationThe following algorithm implements it:do = wfor k = 1 to n for i = 1 to n for j = 1 to n d[i, j]k = min(d[i, j]k−1 , d[i, k]k−1 + d[k, j]k−1 )This obviously runs in Θ(n3 ) time, which is asymptoticallyno better than n calls to Dijkstra’s algorithm.However, the loops are so tight and it is so short and simplethat it runs better in practice by a constant factor.

×