1 Graph Algorithms Single source shortest paths problem Dana Shapira.

Post on 15-Jan-2016

233 views 0 download

Transcript of 1 Graph Algorithms Single source shortest paths problem Dana Shapira.

1

Graph Algorithms

Single source shortest paths problem

Dana Shapira

2

Given vertex s, for every vertex vV find a shortest path from s to v.

Dijkstra’s algorithm solves this problem efficiently for the case in which all weights are nonnegative.

Single Source Shortest Path Problem

1

2 3 4

6 5

101

5

4

3

31

2

61

1

8

3

Single Source Shortest Path Problem

We are given a weighted,directed graph G = (V, E), with weight function : E → R mapping edges to real valued weights.

The weight of a path

is

1 2, ,..., kp v v v

11

,k

i ii

v v

1

2 3 4

6 5

101

5

4

3

31

2

6

1

8

4

Shortest Path Properties

We have optimal substructure: Let p = <v1, .. vk> be the shortest path between v1 and vk. The sub-path between vi and vj, where 1 ≤ i,j ≤ k, pij = <vi, .. vj> is a shortest path.

Proof: suppose some subpath is not a shortest path There must then exist a shorter subpath Could substitute the shorter subpath for a shorter

path But then overall path is not shortest path.

Contradiction

5

Shortest Path Properties

Define (u,v) to be the weight of the shortest path from u to v

Triangle inequality:

(u,v) (u,x) + (x,v) For each edge (x,v) (u,v) (u,x) + (x,v) (why?)

x

u v

min :,

p u v if there is a path from u to vu v

otherwize

6

Shortest Path Properties

In graphs with negative weight cycles, some shortest paths will not exist (Why?):

< 0

7

Negative weight cycles

-1

a

b

c

e

d-2

34

-2

32

-51

8

Relaxation

For all v, maintain upper bound d[v] on (s,v) Relax(u,v,w) {

if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v);

}

943

743

Relax

643

643

Relax

9

Dijkstra’s Algorithm

Dijkstra(G)

for each v V d[v] = ; d[s] = 0; S = ; Q = V; (s)=NULL; while (Q ) u = ExtractMin(Q);

S = S{u}; for each v Adj[u] if (d[v] > d[u]+w(u,v))

d[v] = d[u]+w(u,v);

(v)=u

RelaxationStep

B

C

DA

10

4 3

2

15

What will be the total running time?

10

Correctness Of Dijkstra's Algorithm

d[v] (s,v) v Let u be first vertex in S such that d[u] (s,u) when it is removed from Q.

s

xy

up2

p1

We must show that when u is removed from Q, it has already converged

11

Correctness Of Dijkstra's Algorithm

Let y be first vertex V-S on actual shortest path from su d[y] = (s,y)

Because d[x] is set correctly for y's predecessor x S on the shortest path (u was the first vertex in S such that d[u] (s,u)) ,

When we put x into S, we relaxed (x,y), giving d[y] the correct value

s

xy

up2

p1

12

Correctness Of Dijkstra's Algorithm

d[u] > (s,u)= (s,y) + (y,u) (Why?)= d[y] + (y,u) d[y] But if d[u] > d[y], wouldn't have chosen u.

Contradiction.

s

xy

up2

p1

13

Question

Can you give an example of a graph that includes negative weights, and does not work with Dijkstra’s algorithm?

A B

D C

-2

1

3

4 A B

D C

0

1

3

44

4 3

14

Bellman-Ford Algorithm

BellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v));

(v)=u; for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v);

Initialize d[], whichwill converge to shortest-path value

Relaxation: Make |V|-1 passes, relaxing each edge

Test for solution Under what conditiondo we get a solution?

15

Bellman-Ford Algorithm

BellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v));

(v)=u; for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v);

What will be the running time?

O(|V||E|)

16

Bellman-Ford Algorithm

BellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v));

(v)=u; for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w

B

E

DC

A

-1 2

2

1-3

5

3

4

Example

s

17

Bellman-Ford

Note that order in which edges are processed affects how quickly it converges.

Correctness: show d[v] = (s,v) after |V|-1 passes Lemma: d[v] (s,v) always

Initially true Let v be first vertex for which d[v] < (s,v) Let u be the vertex that caused d[v] to change:

d[v] = d[u] + (u,v) Then d[v] < (s,v) (s,u) + (u,v) d[u] +

(u,v) (Why?) So d[v] < d[u] + (u,v). Contradiction.

18

Bellman-Ford

Prove: after |V|-1 passes, all d values correct Consider shortest path from s to v:

s v1 v2 v3 … v Initially, d[s] = 0 is correct, and doesn’t change

(Why?) After 1 pass through edges, d[v1] is correct (Why?)

and doesn’t change After 2 passes, d[v2] is correct and doesn’t change … Terminates in |V| - 1 passes: (Why?) What if it doesn’t?

19

Let be a negative cycle. Suppose that the algorithm does not find any problem in the last iteration.

Since contradiction

0 1 0,..., ,k kc v v v v

Bellman-Ford

1 0 0 1

2 1 1 2

1 1

1

11 0 1

[v ] [v ] ,

[v ] [v ] ,

[v ] [v ] ,

[ ] [ ] ( , )

k k - k k

k k k

i i i ii i i

d d v v

d d v v

d d v v

d v d v v v

0 11

[v ]= [v ] 0 ( , )k

k i ii

d d v v

20

Bellman-Ford Example

5

s

zy

6

7

8-3

72

9

-2xt

-4

21

DAG Shortest Paths Problem: finding shortest paths in DAG

Bellman-Ford takes O(|V||E|) time. How can we do better? Idea: use topological sort

If were lucky and processes vertices on each shortest path from left to right, would be done in one pass

Every path in a DAG is subsequence of topologically sorted vertex order, so processing vertices in that order, we will do each path in forward order (will never relax edges out of vertices before doing all edges into vertices).

Thus: just one pass. What will be the running time?

22

DAG Shortest Paths Example

s∞

3 a b c d

5

1 4

2

9

6

-1