Top Banner
CS420 lecture twelve Shortest Paths wim bohm cs csu
50

CS420 lecture twelve Shortest Paths wim bohm cs csu.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS420 lecture twelve Shortest Paths wim bohm cs csu.

CS420 lecture twelveShortest Paths

wim bohm cs csu

Page 2: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Shortest Paths Problems

• Given a weighted directed graph G=(V,E) find the shortest path – path length is the sum of its edge weights.

• The shortest path (SP) from u to v is if there is no path from u to v.

Page 3: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Variations

1) SSSP (Single source SP): find the SP from some node s to all nodes in the graph.

2) SPSP (single pair SP): find the SP from some u to some v.

We can use 1) to solve 2), also there is no asymptotically faster algorithm for 2) than that for 1).

Page 4: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Variations

3) SDSP (single destination SP) can use 1) by reversing its edges.

4) APSP (all pair SPs) could be solved by |V|

applications of 1), but can be solved faster.

We will thus concentrate on SSSP and APSP.

Page 5: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Optimal Substructure

• Optimal Substructure the property that the SP from u to v via w consists of a SP from u to w, and a SP from w to v or that a sub path from p to q is a SP from p to q.

Page 6: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Negative weight edges and cycles

• Negative weight cycles create an ill defined problem. Why?

• If some weights in the graph are negative but no cycle in the graph has a negative length, the SP problem stays well defined.

• Some algorithms (Dijkstra) assume all weights to be positive, some (Bellman Ford) allow negative weights but not negative length cycles.

Page 7: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Zero length cycles

• We have already ruled out negative length cycles, a shortest path cannot contain a positive length cycle, what about zero length cycles?

• We can remove zero length cycles and produce a path with the same length. Hence we can assume that shortest paths have no cycles and thus have at most |V|-1 edges.

Page 8: CS420 lecture twelve Shortest Paths wim bohm cs csu.

SSSP and Shortest path trees

• For each vertex v on the shortest path (or the shortest path in construction) we maintain its predecessor (v), a node or nil.

• The predecessor sub graph G = (V, E) has the vertices v and edges (v) nil plus the source s.

• Shortest path algorithms make G a shortest path tree with root s with shortest paths to all vertices reachable from s.

Page 9: CS420 lecture twelve Shortest Paths wim bohm cs csu.

d(v)

• For each vertex v we maintain d(v), the shortest-path estimate: an upper bound on the shortest path length to v.

• We initialize d-s and -s as follows: Init-SingleSource (G,s){ for each vertex v {d[v]=; [v]=nil} d[s]=0; }

Page 10: CS420 lecture twelve Shortest Paths wim bohm cs csu.

• Arithmetic with :

if a -, we have a+ = +a =

if a , a+(-)=(-)+a=-.

Page 11: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Relax

• Shortest paths algorithms use the operation relax edge (u,v): testing whether we can improve a path from s to v by using edge (u,v) and, if so, updating d[v] and [v]:

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

Page 12: CS420 lecture twelve Shortest Paths wim bohm cs csu.

(s,v)

• The final value for d[v] is denoted as (s,v), the shortest path from s to v.

• Assuming the algorithm starts with Init-SingleSource and only does updates on d

and using Relax, the following properties of shortest paths and relax can be proved (Cormen et. al., ch. 24.5).

Page 13: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Properties

• Triangle Inequality: For any edge (u,v): (s,v) <= (s,u) + w(u,v) optimal sub structure

• Upper-bound Property: d[v] >= (s,v) and once d[v] achieves (s,v), it

does not change anymore. nature of relax

Page 14: CS420 lecture twelve Shortest Paths wim bohm cs csu.

More Properties

• No-path property: If there is no path from s to v, d[v]= (s,v)= relax never changed d

• Convergence property: If s u v is a shortest path and d[u]= (s,u)

before relaxing edge (u,v), then d[v] = (s,v) afterward.

optimal sub structure

Page 15: CS420 lecture twelve Shortest Paths wim bohm cs csu.

and more

• Path-relaxation property: If p = <v0,v1,...,vk> is a shortest path from s=v0

to vk, and the edges are relaxed in order (v0,v1), (v1,v2) etc., then d[v]= (s,v).

inductive argument

• Predecessor-subgraph property: Once d[v]= (s,v) for all v in V, the predecessor

subgraph is the shortest path tree rooted at s. nature of Relax

Page 16: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Bellman-Ford SSSP

• Allows negative edge weights• Checks for negative length cycles• returns false if there is a negative length cycle• otherwise, BF returns true and and the

shortest paths in d and the shortest path tree in .

Page 17: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Bellman-Ford(G,w,s){ Init-SingleSource(G,s) for i = 1 to |V|-1 for each (u,v) in E Relax(u,v,w) // check for negative cycles for each (u,v) in E if (d[v]> d[u]+w(u,v) return false return true}complexity?

Page 18: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Bellman-Ford(G,w,s){ Init-SS(G,s) for i = 1 to |V|-1 for each (u,v) in E Relax(u,v,w) // check for negative cycles for each (u,v) in E if (d[v]> d[u]+w(u,v) return false return true}complexity O(|V| * |E| )

Page 19: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Correctness Bellman-Ford

• Cormen et.al.: proof in the case that there are no negative length cycles is based on path relaxation lemma: for each shortest path the first edge is found in sweep one, the i-th edge is found in sweep i.

• If after |V|-1 sweeps there is still improvement then there must be a negative length path.

Page 20: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

b

c

∞ a

6

7 -3

5

-2

Example BF1.

State: right after Init-SS. The nodes contain their d values, all values are nil.

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

What happens in the first sweep? ..... Relax all edges .....

Page 21: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

b

c

∞ a

6

7 -3

5

-2

Example BF1.

State: right after Init-SS. The nodes contain their d values, all values are nil.

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

What happens in the first sweep? In the first sweep: d[b] > 0+6 so d[b]=6 and [b]=s d[c] > 0+7 so d[c]=7 and [c]=s

Page 22: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

6

b

7

c

∞ a

6

7 -3

5

-2

Example BF1.

State: after sweep 1

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

What happens in the second sweep?

Page 23: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

6

b

7

c

∞ a

6

7 -3

5

-2

Example BF1.

State: after sweep 1

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

What happens in the second sweep?In the second sweep: d[a] > 6+5 so d[a]=11 and [a]=b d[a] > 7-3 so d[a]=4 and [a]=c d[b] > 4-2 so d[b]=2 and [b]=a

Page 24: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

2

b

7

c

4 a

6

7 -3

5

-2

What happens in the third sweep?What is returned?

Page 25: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

2

b

7

c

4 a

6

7 -3

5

-2

In the third sweep nothing changes

BF1 returns true

Page 26: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

b

c

∞ a

6

7 -3

-5

2

Example BF2.

Slightly changed state: right after Init-SS. The nodes contain their d values, all values are nil.

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

What happens in the first sweep?

Page 27: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

6

b

7

c

∞ a

6

7 -3

-5

2

Example BF2.

State after first sweep

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

In the first sweep: d[b] > 0+6 so d[b]=6 and [b]=s d[c] > 0+7 so d[c]=7 and [c]=s

Page 28: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

6

b

7

c

∞ a

6

7 -3

-5

2

Example BF2.

State after first sweep

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

What happens in the second sweep?

Page 29: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

6

b

7

c

1 a

6

7 -3

-5

2

Example BF2.

State after second sweep

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

In the second sweep: d[a] > 6-5 so d[a]=1 and [a]=b

Page 30: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

6

b

7

c

1 a

6

7 -3

-5

2

Example BF2.

State after second sweep

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

What happens in the third sweep?

Page 31: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

3

b

7

c

1 a

6

7 -3

-5

2

Example BF2.

State after third sweep

BF does 3 sweeps over all edges, assume order (a,b) , (b,a), (c,a), (s,b), (s,c).

Relax(u,v,w) { if (d[v] > (r = d[u]+w(u,v))) {d[v] = r; [v]= u} }

In sweep three we can now improve d[b] but the predecessor sub graph is broken!!

We now can keep lowering d[a] and and d[b] in subsequent sweeps. The Bellman-Ford algorithm detects this in its last sweep and returns false.

Page 32: CS420 lecture twelve Shortest Paths wim bohm cs csu.

SSSP in a DAG

• By relaxing edges in a DAG in topological sort order we need to only relax each edge once • because in any path p = <v0,v1,...,vk>, (v0,v1)

topologically precedes (v1,v2) etc. • So the path relaxation property implies d[v]= (s,v) for all v.

Page 33: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Dijkstra's SSSP

• Dijkstra's greedy algorithm solves SSSP for directed graphs with non-negative edges.

• Very much like in Primm's minimal spanning tree algorithm, a set S of vertices whose minimal path has been determined is stepwise extended, and distances to yet unreached vertices are updated.

• The vertices are maintained in a min priority queue, keyed on their d value.

Page 34: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Back to Primm MST

• In lecture 8 Greedy algorithms we sketched Primm's MST:– the growing MST A is a tree– start with arbitrary point, pick minimal emanating

edge– keep adding minimal distance to A nodes,

adjusting the minimal distance of the points not in to A

– store V in min priority Queue

Page 35: CS420 lecture twelve Shortest Paths wim bohm cs csu.

MST-Prim(G,w,s) // G=(V,E), weights w, root s init_SingleSource(G,s) Q=V; // in initial priority Queue form, // eg heapified while Q not empty u = extract-min(Q) // first one will be s for each v in adj(u) if v in Q and w(u,v) < u.d v.d = w(u,v) ; v. = u

Page 36: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Complexity Primm

• Q: binary min heap (see sorting lecture)– we can build the heap in O(|V|) time

• The while loop executes |V| times – each extract-min takes O(lg|V|) time totalling O(|V|lg|V|) time

Page 37: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Complexity Primm cont'

• The inner for loop executes |E| times in total– because each vertex is extracted only once and

therefore each edge in the adjacency list representing G is considered once

– v.d = w(u,v) in the conditional in the innermost loop involves a decrease-key operation in the heap taking O(lg|V|) time

• Hence, complexity when using priority min heap: O((|V|+|E|)(lg(|V|)) which is O(|E|lg|V|)

Page 38: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Dijkstra(G,w,s) { // very much like Primm S = empty; Q = V(G); // min priority queue while (Q not empty) { u=Extract-min(Q); S=S{u} for each adjacent v of u Relax(u,v,w) }}

The while loop keeps Q=V-S invariantThe first u=sThe Relax function decreases d-values for some vertices using the decrease-key min-priority queue function (see Sorting lecture).Relax creates d and

Page 39: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Dijkstra(G,w,s) { S = empty; Q = V(G); // min priority queue while (Q not empty) { u=Extract-min(Q); S=S{u} for each adjacent v of u Relax(u,v,w) }}

Correctness proof very similar to Primm's MST proof

Page 40: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

b

c

∞ a

2

7 1

5

2

1

Page 41: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

b

c

∞ a

2

7 1

5

2

1 0s

2

b

7

c

∞ a

2

7 1

5

2

1

Page 42: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

b

c

∞ a

2

7 1

5

2

1 0s

2

b

7

c

∞ a

2

7 1

5

2

1

0s

2

b

3

c

7 a

2

7 1

5

2

1

Page 43: CS420 lecture twelve Shortest Paths wim bohm cs csu.

0s

b

c

∞ a

2

7 1

5

2

1 0s

2

b

7

c

∞ a

2

7 1

5

2

1

0s

2

b

3

c

7 a

2

7 1

5

2

1 0s

2

b

3

c

4 a

2

7 1

5

2

1

Page 44: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Floyd-Warshall's APSP algorithm

• Floyd-Warshall's dynamic programming APSP algorithm runs in (|V|3) time and uses the adjacency matrix representation:

wij = 0 if i=j

weightij if (i,j) in E

otherwise• Negative weight edges can be present, but

negative length cycles cannot.

Page 45: CS420 lecture twelve Shortest Paths wim bohm cs csu.

vertex sets

• Denote the Vertex set V to be {1,2,...,n} and let Vk be the subset {1,2,...k}.

• The algorithm uses a recurrence, which in step k considers paths from all i to all j

either (don't take node k into account) with intermediate nodes from Vk-1 only

or (do take node k into account) with intermediate nodes Vk-1 and also node k

Page 46: CS420 lecture twelve Shortest Paths wim bohm cs csu.

k intermediate node in Vk?

• If k is not an intermediate node then the shortest path from i to j in step k is equal to the shortest path in step k-1.

• If k is an intermediate node then the shortest path from i to j in step k is the shortest from i to k in step k-1 plus the shortest path from k to j in step k-1.

Page 47: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Vk:via set via which paths from i to j go

• equation form of previous slide:

dij(k) = wij if k=0

min(dij(k-1), dik

(k-1)+ dkj(k-1)) if k>0

Page 48: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Floyd Warshall

Floyd-Warshall(W) D=W for k = 1 to n for i = 1 to n for j = 1 to n D[i,j]=min(D[i,j], D[i,k]+D[k,j]

Page 49: CS420 lecture twelve Shortest Paths wim bohm cs csu.

Predecessors in Floyd-Warshall

• A recurrence for the predecessor can be derived much like the recurrence for d.

ij(0) = nil if i=j or (i,j) not in E

i otherwise ij

(k) = ij(k-1) if dij

(k-1) dik(k-1) + dkj

(k-1)

kj(k-1) if dij

(k-1)> dik(k-1) + dkj

(k-1)

• Code for computing the predecessor is easily incorporated in the Floyd-Warshall function.

Page 50: CS420 lecture twelve Shortest Paths wim bohm cs csu.

transitive closure

• Given a graph G=(V,E), the transitive closure graph G*=(V,E*) has an edge (i,j) if there is a path from i to j in G.

• G* can be computed using Floyd-Warshall by – initially assigning Dij=1 if (i,j) in E and 0 otherwise, – and replacing min by or and + by and in the loop.