46
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Embed Size (px)

Citation preview

Page 1: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Data Structures & Algorithms

Shortest Paths

Richard Newmanbased on book by R. Sedgewick

and slides by S. Sahni

Page 2: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Minimum Cost Path

• Weighted, digraph G, or network• Directed simple path p from s to t• Cost(p) = sum of edge weights on p• Minimum cost path p from s to t in G

such that no other path p' from s to t in G has cost(p') < cost(p)

Page 3: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Minimum Cost Path

s = 1, t = 10What is minimum cost path?

23

8101

45

911

67

48

6

6

7

5

24

4 53

8

2

Page 4: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

A Spanning Tree

path cost = 28Is there a cheaper path?

23

8101

45

911

67

48

6

6

7

5

24

4 53

8

2

Page 5: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

A Spanning Tree

path cost = 25Is there a cheaper path?

23

8101

45

911

67

48

6

6

7

5

24

4 53

8

2

Page 6: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

A Spanning Tree

path cost = 24Is there a cheaper path?

23

8101

45

911

67

48

6

6

7

5

24

4 53

8

2

Page 7: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Shortest Path Problems• No negative weight edges allowed!• s-t shortest path

– Single source, destination– Fastest route to Epcot

• Single Source Shortest Path– Best routes from A to anywhere

• All Pairs Shortest Paths– Routing tables in network nodes

Page 8: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Shortest Path Algorithms

• Dijkstra's Algorithm• Floyd's Algorithm• Bellman-Ford Algorithm

Page 9: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Shortest Path Algorithms

• Dijkstra's Algorithm• Floyd's Algorithm• Bellman-Ford Algorithm

Page 10: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Dijkstra's Algorithm• Very similar to Prim's algorithm• Differences are that

– Paths form a rooted tree (whereas MST is not rooted)

– Graph is directed (not undirected)• Grow known shortest paths from

source one edge at a time• Priority of edge is different

Page 11: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Dijkstra's Algorithm• Set Known nodes K = {s}• Set dist(s) = 0, dist(u) = ∞ for all

other nodes u in G• Set pred(s) = s, pred(u) = NULL for

all other nodes u in G• Set Seen nodes S = {neighbors of s}• Set pred(u) = s for all nodes in S

Page 12: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Dijkstra's AlgorithmWhile |K| < V

Find nearest node v in SAdd v to KFor each edge (v,w) in E

If dist(v) + cost(v,w) < dist(w)Add w to SPred(w) = vdist(w) = dist(v) + cost(v,w)

Page 13: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Dijkstra's Algorithm

s = 1Grow SPT by adding node nearest to s

48

6

6

7

5

24

4 53

8

2

67

2

4 5

3

1 8

9

10

11

7

5

6

5

5

67

2

4 5

3

1 8

9

10

11

0

2

42

4

6

5

3

7

12

11

14

9

13

7

9

17

21

228

11

10

19

Update bestdistances

Page 14: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Dijkstra's Algorithm Correct

• Builds shortest path tree (SPT)• Always adds nearest seen node v• Path takes into account all nodes in K• No other node x not in K can be

closer than v• Hence no path through x could be

shorter than the path we have to v

Page 15: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Dijkstra's Algorithm Complexity

• Initialization – O(V)• Select next node – O(V) linear list• Update dist, pred – O(E) total• Selection done V-1 times• Total time – O(V2 + E) = O(V2)• Using linear list for S

Page 16: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Dijkstra's Algorithm Complexity

• MinHeap – O((V+E) lg V)• V removals, E changes to S• What if G is dense – E is O(V2)?

• Worse!! • Fibonacci Heap – O(V lg V + E)

• Even better!

Page 17: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Dijkstra's Algorithm

• Solves single source shortest path• Builds SPT

• For s-t shortest path, • Just stop when t is added to K

• Also works for sink-trees (take edges in reverse direction)

Page 18: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Dijkstra's Algorithm

• Does NOT work with negative edge weights! (Violates assumption needed by greedy method)

• Can be used to solve all pairs shortest path• Build SPT from each node

• Complexity of SSSP times V

Page 19: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Shortest Path Algorithms

• Dijkstra's Algorithm• Floyd's Algorithm• Bellman-Ford Algorithm

Page 20: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Shortest Path Algorithms

• Floyd's Algorithm• Single source shortest paths• Works like Warshall’s algorithm for

reachability• Except takes path costs into

account

Page 21: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd’s Algorithm

• Recall Warshall's AlgorithmFor each intermediate node i

For each source sFor each destination t

s reaches t if s already reaches t or if s reaches i and i reaches t

Page 22: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd’s Algorithm

• Now just track distancesFor each intermediate node i

For each source sFor each destination t

cost(s,t) = lesser ofcost(s,t) and cost(s,i) + cost(i,t)

Page 23: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd’s Algorithm

• Complexity of Floyd’s algorithm:• O(V3)• Dynamic Programming and• Relaxation

• Build estimates• Improve estimates (node relax)• Converge

Page 24: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd's Algorithm

Consider paths through nodes numbered <1, <2, <3, etc.

410 1

4

5

3 2

19

32 51

21

3632

50

3645

290 1 2 3 4 5

0 0 41 29

1 0 51 32

2 0 50

3 45 0 36

4 32 36 0

5 29 21 0

Page 25: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd's Algorithm

Paths through nodes numbered <13-0-1 better, 3-0-5 worse

410 1

4

5

3 2

19

32 51

21

3632

50

3645

290 1 2 3 4 5

0 0 41 29

1 0 51 32

2 0 50

3 45 86 0 36

4 32 36 0

5 29 21 0

Page 26: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd's Algorithm

Paths through nodes numbered <229+32 = 61 > 21

410 1

4

5

3 2

29

32 51

21

3632

50

3645

290 1 2 3 4 5

0 0 41 92 73 29

1 0 51 32

2 0 50

3 45 86 137 0 118 36

4 32 36 0

5 29 80 21 0

Page 27: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd's Algorithm

Paths through nodes numbered <3

410 1

4

5

3 2

29

32 51

21

3632

50

3645

290 1 2 3 4 5

0 0 41 92 142 73 29

1 0 51 101 32

2 0 50

3 45 86 137 0 118 36

4 32 36 0

5 29 80 130 21 0

Page 28: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd's Algorithm

Paths through nodes numbered <4

410 1

4

5

3 2

29

32 51

21

3632

50

3645

290 1 2 3 4 5

0 0 41 92 142 73 29

1 146 0 51 101 32 137

2 95 136 0 50 168 86

3 45 86 137 0 118 36

4 81 122 32 36 0 72

5 175 29 80 130 21 0

Page 29: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd's Algorithm

Paths through nodes numbered <5

410 1

4

5

3 2

29

32 51

21

3632

50

3645

290 1 2 3 4 5

0 0 41 92 109 73 29

1 113 0 51 68 32 106

2 95 136 0 50 168 88

3 45 86 137 0 118 36

4 81 122 32 36 0 72

5 102 29 53 57 21 0

Page 30: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd's Algorithm

Paths through nodes numbered <6

410 1

4

5

3 2

29

32 51

21

3632

50

3645

290 1 2 3 4 5

0 0 41 82 86 50 29

1 113 0 51 68 32 106

2 95 117 0 50 109 88

3 45 65 89 0 57 36

4 81 101 32 36 0 72

5 102 29 53 57 21 0

Page 31: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Floyd’s Algorithm

• Update successor node when updating minimum cost path

• Method of choice for APSP for dense graphs

• Works even with negative weights• But not with negative cycles!

• (can detect at least one)

Page 32: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Shortest Path Algorithms

• Dijkstra's Algorithm• Floyd's Algorithm• Bellman-Ford Algorithm

Page 33: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Shortest Path Algorithms

• Bellman-Ford Algorithm• SSSP• Compute best cost path by edge

relaxation – checking all edges• Essentially considers paths of

increasing potential length

Page 34: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Initialize: dist[t] = {0 if t==s, else infinity}pred[v] = NULL for all v

For (i = 1 to V-1)For each edge e=(u,v) in E

If (dist[u] + cost[e] < dist[v])dist[v] = dist[u] + cost[e]pred[v] = u

Page 35: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Consider paths of length <2, <3, etc.Adjacency matrix is paths of length <2

410 1

4

5

3 2

19

32 51

21

3632

50

3645

290 1 2 3 4 5

0 0 41 29

1 0 51 32

2 0 50

3 45 0 36

4 32 36 0

5 29 21 0

Page 36: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Edge (0,1)

410 1

4

5

3 2

19

32 51

21

3632

50

3645

29 0 1 2 3 4 5

0 0 41 29

1 0 51 32

2 0 50

3 45 86 0 36

4 32 36 0

5 29 21 0

Enter here

Leave here

Page 37: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Edge (0,5) doesn’t helpEdge (1,2)

410 1

4

5

3 2

19

32 51

21

3632

50

3645

29 0 1 2 3 4 5

0 0 41 92 29

1 0 51 32

2 0 50

3 45 86 137 0 36

4 32 36 0

5 29 80 21 0

Page 38: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Edge (2,3)

410 1

4

5

3 2

19

32 51

21

3632

50

3645

29 0 1 2 3 4 5

0 0 41 92 142 29

1 0 51 101 32

2 0 50

3 45 86 137 0 36

4 32 36 0

5 29 80 130 21 0

Page 39: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Edge (3,0)

410 1

4

5

3 2

19

32 51

21

3632

50

3645

29 0 1 2 3 4 5

0 0 41 92 142 29

1 146 0 51 101 32

2 95 0 50

3 45 86 137 0 36

4 81 32 36 0

5 175 29 80 130 21 0

Page 40: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Edge (3,5)

410 1

4

5

3 2

19

32 51

21

3632

50

3645

29 0 1 2 3 4 5

0 0 41 92 142 29

1 146 0 51 101 32 137

2 95 0 50 86

3 45 86 137 0 36

4 81 32 36 0 72

5 175 29 80 130 21 0

Page 41: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Edge (4,2)

410 1

4

5

3 2

19

32 51

21

3632

50

3645

29 0 1 2 3 4 5

0 0 41 92 142 29

1 146 0 64 101 32 137

2 95 0 50 86

3 45 86 137 0 36

4 81 32 36 0 72

5 175 29 53 130 21 0

Page 42: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Edge (4,3)

410 1

4

5

3 2

19

32 51

21

3632

50

3645

29 0 1 2 3 4 5

0 0 41 92 128 29

1 146 0 64 68 32 137

2 95 0 50 86

3 45 86 137 0 36

4 81 32 36 0 72

5 175 29 53 57 21 0

Page 43: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Edge (5,1)

410 1

4

5

3 2

19

32 51

21

3632

50

3645

29 0 1 2 3 4 5

0 0 41 92 128 29

1 146 0 64 68 32 137

2 95 115 0 50 86

3 45 65 137 0 36

4 81 101 32 36 0 72

5 175 29 53 57 21 0

Page 44: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

Edge (5,4)

410 1

4

5

3 2

19

32 51

21

3632

50

3645

29 0 1 2 3 4 5

0 0 41 92 128 50 29

1 146 0 64 68 32 137

2 95 115 0 50 107 86

3 45 65 137 0 57 36

4 81 101 32 36 0 72

5 175 29 53 57 21 0

Page 45: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bellman-Ford Algorithm

• Well, that was ONE pass – need to do that V times!

Page 46: Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Shortest Path Algorithms

• Dijkstra's Algorithm• Floyd's Algorithm• Bellman-Ford Algorithm