Transcript
Page 1: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest path problems

Page 2: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest path problems

Page 3: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest path problems

Page 4: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Single-Source Shortest Paths

Given graph (directed or undirected) G = (V,E) withweight function w: E R and a vertex sV, find for all vertices vV the minimum possible weight for path from s to v.

We will discuss two general case algorithms:

• Dijkstra's (positive edge weights only)• Bellman-Ford (positive end negative edge weights)

If all edge weights are equal (let's say 1), the problem is solved by BFS in (V+E) time.

Page 5: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - RelaxRelax(vertex u, vertex v, weight w)

if d[v] > d[u] + w(u,v) thend[v] d[u] + w(u,v)p[v] u

Page 6: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Idea

Page 7: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - SSSP-DijkstraSSSP-Dijkstra(graph (G,w), vertex s)

InitializeSingleSource(G, s)S Q V[G]while Q 0 do

u ExtractMin(Q)S S {u}for v Adj[u] do

Relax(u,v,w)

InitializeSingleSource(graph G, vertex s)for v V[G] do

d[v] p[v] 0

d[s] 0

Page 8: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

10

1

5

2

649

7

2 3

Page 9: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

0

10

1

5

2

649

7

2 3

Page 10: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

0

5

10

10

1

5

2

649

7

2 3

Page 11: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

0

5

10

10

1

5

2

649

7

2 3

Page 12: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

0

5

8

7

1410

1

5

2

649

7

2 3

Page 13: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

0

5

8

7

1410

1

5

2

649

7

2 3

Page 14: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

0

5

8

7

1310

1

5

2

649

7

2 3

Page 15: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

0

5

8

7

1310

1

5

2

649

7

2 3

Page 16: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

0

5

8

7

910

1

5

2

649

7

2 3

Page 17: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Example

0

5

8

7

910

1

5

2

649

7

2 3

Page 18: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsDijkstra’s Algorithm - Complexity

SSSP-Dijkstra(graph (G,w), vertex s)InitializeSingleSource(G, s)S Q V[G]while Q 0 do

u ExtractMin(Q)S S {u}for u Adj[u] do

Relax(u,v,w)

InitializeSingleSource(graph G, vertex s)for v V[G] do

d[v] p[v] 0

d[s] 0

Relax(vertex u, vertex v, weight w)if d[v] > d[u] + w(u,v) then

d[v] d[u] + w(u,v)p[v] u

(V)

(1) ?

(E) times in total

executed (V) times

Page 19: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Complexity

InitializeSingleSource TI(V,E) = (V)

Relax TR(V,E) = (1)?

SSSP-Dijkstra

T(V,E) = TI(V,E) + (V) + V (log V) + E TR(V,E) =

= (V) + (V) + V (log V) + E (1) = (E + V log V)

Page 20: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Complexity

Page 21: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - Correctness

Page 22: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Dijkstra’s Algorithm - negative weights?

Page 23: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsBellman-Ford Algorithm - negative cycles?

Page 24: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Idea

Page 25: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsBellman-Ford Algorithm - SSSP-BellmanFord

SSSP-BellmanFord(graph (G,w), vertex s)InitializeSingleSource(G, s)for i 1 to |V[G] 1| do

for (u,v) E[G] doRelax(u,v,w)

for (u,v) E[G] doif d[v] > d[u] + w(u,v) then

return falsereturn true

Page 26: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Example

6

7

7-3

2

8-4

9

5-2

Page 27: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Example

0

6

7

7-3

2

8-4

9

5-2

Page 28: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Example

0

7

6

6

7

7-3

2

8-4

9

5-2

Page 29: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Example

0

7

6

2

46

7

7-3

2

8-4

9

5-2

Page 30: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Example

0

7

2

2

46

5

7

7-3

2

8

-2

-4

9

Page 31: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Example

0

7

2

-2

46

5

7

7-3

2

8

-2

-4

9

Page 32: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Complexity

SSSP-BellmanFord(graph (G,w), vertex s)InitializeSingleSource(G, s)for i 1 to |V[G] 1| do

for (u,v) E[G] doRelax(u,v,w)

for (u,v) E[G] doif d[v] > d[u] + w(u,v) then

return falsereturn true

executed (V) times

(E)

(E)

(1)

Page 33: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Complexity

InitializeSingleSource TI(V,E) = (V)

Relax TR(V,E) = (1)?

SSSP-BellmanFord

T(V,E) = TI(V,E) + V E TR(V,E) + E == (V) + V E (1) + E = = (V E)

Page 34: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Correctness

Page 35: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Correctness

Page 36: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Correctness

Page 37: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Bellman-Ford Algorithm - Correctness

Page 38: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - SSSP-DAG

SSSP-DAG(graph (G,w), vertex s)

topologically sort vertices of G

InitializeSingleSource(G, s)

for each vertex u taken in topologically sorted order do for each vertex v Adj[u] do

Relax(u,v,w)

Page 39: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

5 2 7 -1 -2

6 1

3 42

Page 40: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

0 5 2 7 -1 -2

6 1

3 42

Page 41: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

0 5 2 7 -1 -2

6 1

3 42

Page 42: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

0 5 2 7 -1 -2

6 1

3 42

Page 43: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

6 0 25 2 7 -1 -2

6 1

3 42

Page 44: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

6 6 4 0 25 2 7 -1 -2

6 1

3 42

Page 45: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

6 6 4 0 25 2 7 -1 -2

6 1

3 42

Page 46: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

6 5 4 0 25 2 7 -1 -2

6 1

3 42

Page 47: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

6 5 4 0 25 2 7 -1 -2

6 1

3 42

Page 48: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

6 5 3 0 25 2 7 -1 -2

6 1

3 42

Page 49: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Example

6 5 3 0 25 2 7 -1 -2

6 1

3 42

Page 50: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Shortest Paths in DAGs - Complexity

T(V,E) = (V + E) + (V) + (V) + E (1) = (V + E)

SSSP-DAG(graph (G,w), vertex s)

topologically sort vertices of G

InitializeSingleSource(G, s)

for each vertex u taken in topologically sorted order do for each vertex v Adj[u] do

Relax(u,v,w)

Page 51: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsApplication of SSSP - currency conversion

Page 52: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsApplication of SSSP - currency conversion

Page 53: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsApplication of SSSP - currency conversion

Page 54: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsApplication of SSSP - constraint satisfaction

Page 55: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsApplication of SSSP - constraint satisfaction

Page 56: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsApplication of SSSP - constraint satisfaction

Page 57: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsApplication of SSSP - constraint satisfaction

Page 58: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsApplication of SSSP - constraint satisfaction

Page 59: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsApplication of SSSP - constraint satisfaction

Page 60: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

All-Pairs Shortest PathsGiven graph (directed or undirected) G = (V,E) withweight function w: E R find for all pairs of vertices u,v V the minimum possible weight for path from u to v.

Page 61: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Idea

Page 62: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Idea

Page 63: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Idea

ds,t(i) – the shortest path from s to t containing only vertices

v1, ..., vi

ds,t(0) = w(s,t)

ds,t(k) =

w(s,t) if k = 0

min{ds,t(k-1), ds,k

(k-1) + dk,t(k-1)} if k > 0

Page 64: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Algorithm

FloydWarshall(matrix W, integer n)for k 1 to n do

for i 1 to n do for j 1 to n do

dij(k) min(dij

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

(k-1))return D(n)

Page 65: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Example

2

45

1 3

3 4

-4 -5

6

7 1

82

0 3 8 -4

0 1 7

4 0 2 -5 0 6 0

W

Page 66: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Example

0 3 8 -4

0 1 7

4 0 2 -5 0 6 0

0 0 0 0

0 0 0

0 0

0 0 0

0 0

D(0) (0)

Page 67: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Example

0 3 8 -4

0 1 7

4 0 2 5 -5 0 -2

6 0

0 0 0 0

0 0 0

0 0

0 1 0 0 1

0 0

D(1) (1)

Page 68: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Example

0 3 8 4 -4

0 1 7

4 0 5 11

2 5 -5 0 -2

6 0

0 0 0 2 0

0 0 0

0 0 2 2

0 1 0 0 1

0 0

D(2) (2)

Page 69: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Example

0 3 8 4 -4

0 1 7

4 0 5 11

2 -1 -5 0 -2

6 0

0 0 0 2 0

0 0 0

0 0 2 2

0 3 0 0 1

0 0

D(3) (3)

Page 70: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Example

0 3 -1 4 -4

3 0 -4 1 -1

7 4 0 5 3

2 -1 -5 0 -2

8 5 1 6 0

0 0 4 2 0

4 0 4 0 1

4 0 0 2 1

0 3 0 0 1

4 3 4 0 0

D(4) (4)

Page 71: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Example

0 3 -1 2 -4

3 0 -4 1 -1

7 4 0 5 3

2 -1 -5 0 -2

8 5 1 6 0

0 0 4 5 0

4 0 4 0 1

4 0 0 2 1

0 3 0 0 1

4 3 4 0 0

D(5) (5)

Page 72: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsFloyd-Warshall Algorithm - Extracting the shortest paths

Page 73: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

Floyd-Warshall Algorithm - Complexity

T(V,E) = (n3) = (V3)

FloydWarshall(matrix W, integer n)for k 1 to n do

for i 1 to n do for j 1 to n do

dij(k) min(dij

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

(k-1))return D(n)

3 for cycles, each executed exactly n times

Page 74: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph AlgorithmsAll-Pairs Shortest Paths -Johnson's algorithm

Page 75: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

All-Pairs Shortest Paths - Reweighting

Page 76: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

All-Pairs Shortest Paths - Reweighting

Page 77: Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems

Graph Algorithms

All-Pairs Shortest Paths - Reweighting


Top Related