1 / 27

Single-Source All-Destinations Shortest Paths With Negative Costs

Compute the shortest path from a given source vertex to each vertex in a directed weighted graph with negative costs, using the Bellman-Ford algorithm. Includes pseudocode and examples.

justinhall
Download Presentation

Single-Source All-Destinations Shortest Paths With Negative Costs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Single-Source All-Destinations Shortest Paths With Negative Costs • Directed weighted graph. • Edges may have negative cost. • No cycle whose cost is < 0. • Find a shortest path from a given source vertex s to each of the n vertices of the digraph.

  2. Single-Source All-Destinations Shortest Paths With Negative Costs • Dijkstra’s O(n2) single-source greedy algorithm doesn’t work when there are negative-cost edges. • Floyd’s Q(n3) all-pairs dynamic-programming algorithm does work in this case.

  3. Bellman-Ford Algorithm • Single-source all-destinations shortest paths in digraphs with negative-cost edges. • Uses dynamic programming. • Runs in O(n3) time when adjacency matrices are used. • Runs in O(ne) time when adjacency lists are used.

  4. s w v Decision Sequence • To construct a shortest path from the source to vertex v, decide on the max number of edges on the path and on the vertex that comes just before v. • Since the digraph has no cycle whose length is < 0, we may limit ourselves to the discovery of cycle-free (acyclic) shortest paths. • A path that has no cycle has at most n-1 edges.

  5. Problem State s w v • Problem state is given by (u,k), where u is the destinationvertex and k is the max number of edges. • (v,n-1) is the state in which we want the shortest path to v that has at most n-1 edges.

  6. Cost Function s w v • Let d(v,k) be the length of a shortest path from the source vertex to vertex v under theconstraint that the path has at most k edges. • d(v,n-1) is the length of a shortest unconstrained path from the source vertex to vertex v. • We want to determine d(v,n-1) for every vertex v.

  7. s Value Of d(*,0) • d(v,0) is the length of a shortest path from the source vertex to vertex v under theconstraint that the path has at most 0 edges. • d(s,0) = 0. • d(v,0) = infinity for v != s.

  8. Recurrence For d(*,k), k > 0 • d(v,k) is the length of a shortest path from the source vertex to vertex v under theconstraint that the path has at most k edges. • If this constrained shortest path goes through no edge, then d(v,k) = d(v,0).

  9. s w v Recurrence For d(*,k), k > 0 • If this constrained shortest path goes through at least one edge, then let w be the vertex just before v on this shortest path (note that w may be s). • We see that the path from the source to w must be a shortest path from the source vertex to vertex w under theconstraint that this path has at most k-1 edges. • d(v,k) = d(w,k-1) + length of edge (w,v).

  10. Recurrence For d(*,k), k > 0 • d(v,k) = d(w,k-1) + length of edge (w,v). s w v • We do not know what w is. • We can assert • d(v,k) = min{d(w,k-1) + length of edge (w,v)}, where the min is taken over all w such that (w,v) is an edge of the digraph. • Combining the two cases considered yields: • d(v,k) = min{d(v,0), min{d(w,k-1) + length of edge (w,v)}}

  11. Pseudocode To Compute d(*,*) // initialize d(*,0) d(s,0) = 0; d(v,0) = infinity, v != s; // compute d(*,k), 0 < k < n for (int k = 1; k < n; k++) { d(v,k) = d(v,0), 1 <= v <= n; for (each edge (u,v)) d(v,k) = min{d(v,k), d(u,k-1) + cost(u,v)} }

  12. p(*,*) • Let p(v,k) be the vertex just before vertex v on the shortest path for d(v,k). • p(v,0) is undefined. • Used to construct shortest paths.

  13. 1 -6 6 3 1 1 2 4 6 7 3 5 3 4 5 9 Example Source vertex is 1.

  14. 1 -6 6 3 1 1 2 4 6 7 3 5 3 4 5 9 - - - - - - - - - - - 0 - - - - - - - 0 3 7 1 1 - 0 3 7 7 16 8 1 2 1 4 4 - 0 2 7 7 10 8 6 2 1 3 4 - 0 2 6 7 10 8 6 2 1 3 4 Example v 1 2 3 4 5 6 0 k 1 2 3 4 d(v,k) p(v.k)

  15. 1 -6 6 3 1 1 2 4 6 7 3 5 3 4 5 9 - 0 2 6 7 8 6 2 1 3 4 - 0 2 6 7 9 8 6 2 1 3 4 Example v 1 2 3 4 5 6 4 10 k 5 d(v,k) p(v.k)

  16. 1 - 0 2 6 7 9 8 6 2 1 3 4 Shortest Path From 1 To 5 1 -6 6 3 1 2 4 6 7 3 5 3 4 5 9 1 2 3 4 5 6 1 2 3 4 5 6 5 p(v,5) d(v,5)

  17. Observations • d(v,k) = min{d(v,0), min{d(w,k-1) + length of edge (w,v)}} • d(s,k) = 0 for all k. • Ifd(v,k) = d(v,k-1) for all v, then d(v,j) = d(v,k-1), for all j >= k-1 and all v. • i.e. the shortest path uses k-1 edges • If we stop computingas soon as we havea d(*,k) that is identical to d(*,k-1) the run time becomes • O(n3) when adjacency matrix is used. • O(ne) when adjacency lists are used.

  18. Observations • The computation may be done in-place. d(v) = min{d(v), min{d(w) + length of edge (w,v)}} instead of d(v,k) = min{d(v,0), min{d(w,k-1) + length of edge (w,v)}} • Following iteration k, d(v,k+1) <= d(v) <= d(v,k) • On termination d(v) = d(v,n-1). • Space requirement becomes O(n) for d(*) and p(*).

  19. Applications • Distance-vector routing protocols • Routing Information Protocol (RIP) • Interior Gateway Routing Protocol (IGRP)

  20. Edit Distance* • How similar are two strings? • Applications in Spell correction, computational biology, Machine Translation, etc. • For example: • The user type “Graffe” • Which is closest? • Graf • Graft • Grail • Giraffe

  21. Edit Distance* • The minimum edit distance between two strings • Editing operations are: • Insertion • Deletion • Substitution  Minimum number of editing operations to transform one string into another

  22. Edit Distance* • Sequence of edits from one string to another

  23. Edit Distance* • Initial state: the word we’re transforming • Operations: insert, delete, substitute • Goal state: the word we’re trying to get to • Path cost (target function to be minimized): the number of edits

  24. Edit Distance* • All edit sequences are too many • We cannot explore all of them (brute force) • Many paths wind up at the same state • Keep the shortest path to each of those revisited states

  25. Edit Distance* • All edit sequences are too many • We cannot explore all of them (brute force) • Many paths wind up at the same state • Keep the shortest path to each of those revisited states

  26. Edit Distance* • For two strings • X of length n • Y of length m • Define d(i,j) • The edit distance between X[1…i] and Y[1..j] • i.e. the edit distance concerning the first i characters of X and the first j characters of Y • The goal state is d(n,m) • i.e. the edit distance concerning all characters of X and all characters of Y

  27. Edit Distance* • Initialization: • d(i,0) = i • d(0,j) = j • Recurrence: For each i = 1 … N For each j = 1 … M d(i,j) = min { d(i-1,j) + 1, d(i,j-1) + 1, if X(i)==Y(j) { d(i-1,j-1) } else {d(i-1,j-1) + 1 }

More Related