Docsity

Shortest Paths with Negative Costs in Directed Weighted Graphs, Slides of Computer Science

Partial preview of the text

Download Shortest Paths with Negative Costs in Directed Weighted Graphs and more Slides Computer Science in PDF only on Docsity!

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.

Single-Source All-Destinations

Shortest Paths With Negative Costs

  • Dijkstra’s O(n 2 ) single-source greedy algorithm doesn’t work when there are negative-cost edges.
  • Floyd’s Theta(n^3 ) all-pairs dynamic- programming algorithm does work in this case.

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.

s w v

Problem State

  • Problem state is given by (u,k), where u is the destination vertex 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.

s w v

Value Of d(*,0)

  • d(v,0) is the length of a shortest path from the source vertex to vertex v under the constraint that the path has at most 0 edges.

s

  • d(s,0) = 0.
  • d(v,0) = infinity for v != s.

Recurrence For d(*,k), k > 0

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

Recurrence For d(*,k), k > 0

  • 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)}}

s w v

  • d(v,k) = d(w,k-1) + length of edge (w,v).

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)}

} Docsity.com

Example

1 2

4

3

6

5

3 7

3

(^4 ) 5

1

9

Source vertex is 1.

1

Example

1 2

4

3

6

5

3 7

3

(^4 ) 5

1

9

1

d(v,k) p(v.k)

1 2 3 4 0

1

2 3

(^5 6) v 0 -^ -^ -^ -^ -^ -^ -^ -^ -^ -^ - k 0 3 -^7 -^ -^ - 1 - 1 - -

4

Shortest Path From 1 To 5

1 2

4

3

6

5

3 7

3

(^4 ) 5

1

9

1

d(v,5) p(v,5)

1 2 3 4 5

5 6 0 2 6 7 9 8 -^6 2 1 3

1 2 3 4 5 6

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.
  • If d(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.
  • If we stop computing as soon as we have a d(,k) that is identical to d(,k-1) the run time becomes  O(n 3 ) when adjacency matrix is used.  O(ne) when adjacency lists are used.