douglas wilhelm harder, m.math. lel department of electrical and computer engineering university of...

73
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca [email protected] © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved. All-pairs shortest path ECE 250 Algorithms and Data Structures

Upload: winifred-mcdonald

Post on 24-Dec-2015

227 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

Douglas Wilhelm Harder, M.Math. LELDepartment of Electrical and Computer Engineering

University of Waterloo

Waterloo, Ontario, Canada

ece.uwaterloo.ca

[email protected]

© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.

All-pairs shortest path

ECE 250 Algorithms and Data Structures

Page 2: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

2

Outline

This topic will:– Review Dijkstra’s algorithm for finding a shortest path– Consider what happens if we want to find all shortest paths– We will look at the Floyd-Warshall algorithm for:

• Finding these shortest distances• Finding the paths corresponding to these distances

– We conclude by finding the transitive closure

All-pairs Shortest Path

Page 3: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

3

Background

Dijkstra’s algorithm finds the shortest path between two nodes– Run time:

If we wanted to find the shortest path between all pairs of nodes, we could apply Dijkstra’s algorithm to each vertex:– Run time:

In the worst case, if , the run time is

5.1

All-pairs Shortest Path

2V

lnV E V

2E V 3

lnV V

Page 4: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

4

Background

Any algorithm that finds the shortest path between all pairs must consider, each pair of vertices; therefore, a lower bound on the execution would be

Now, Dijkstra’s algorithm has the following run times:– Best case:

If , running Dijkstra for each vertex is

– Worst case:

If , running Dijkstra for each vertex is

5.1

All-pairs Shortest Path

2V

E V

2E V

2lnV V

3lnV V

Page 5: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

5

Problem

Question: for the worst case, can we find a algorithm?

We will look at the Floyd-Warshall algorithm

5.1.1

All-pairs Shortest Path

3lno V V

Page 6: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

6

Strategy5.1.2

All-pairs Shortest Path

First, let’s consider only edges that connect vertices directly:

Here, wi,j is the weight of the edge connecting vertices i and j

– Note, this can be a directed graph; i.e., it may be that

In C++, we would define a two-dimensional array

double d[num_vertices][num_vertices];

(0), ,

0 If

If there is an edge from to

Otherwisei j i j

i j

d w i j

(0) (0), ,i j j id d

Page 7: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

7

Strategy5.1.2

All-pairs Shortest Path

Consider this graph of seven vertices– The edges defining the values and are highlighted 0

5,3d 06,7d

Page 8: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

8

Strategy5.1.2

All-pairs Shortest Path

Suppose now, we want to see whether or not the path going through vertex v1 is shorter than a direct edge?

– Is ?

– Is ?

0 0 05,3 5,1 1,3d d d 0 0 06,7 6,1 1,7d d d

Page 9: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

9

Strategy5.1.2

All-pairs Shortest Path

Thus, for each pair of edges, we will define by calculating:

1 0 0 0, , ,1 1,min ,i j i j i jd d d d

1,i jd

Page 10: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

10

Strategy5.1.2

All-pairs Shortest Path

Note that and ; thus, we need just run the algorithm for each pair of vertices:

1 01, 1,j jd d 1 0

,1 ,1i id d

for ( int i = 0; i < num_vertices; ++i ) { for ( int j = 0; j < num_vertices; ++j ) { d[i][j] = std::min( d[i][j], d[i][0] + d[0][j] ); }}

Page 11: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

11

The General Step5.1.2

All-pairs Shortest Path

Define as the shortest distance, but only allowing intermediate visits to vertices v1, v2, …, vk–1

– Suppose we have an algorithm that has found these values for all pairs

1,k

i jd

Page 12: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

12

The General Step5.1.2

All-pairs Shortest Path

How could we find ; that is, the shortest path allowing intermediate visits to vertices v1, v2, …, vk–1 , vk?

,k

i jd

Page 13: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

13

The General Step5.1.2

All-pairs Shortest Path

With v1, v2, …, vk–1 as intermediates, have assumed we have found the shortest paths from vi to vj, vi to vk and vk to vj

– The only possible shorter path including vk would be the path from vi to vk continuing from there to vj

Thus, we calculate

1 1 1, , , ,min ,k k k k

i j i j i k k jd d d d

Page 14: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

14

The General Step5.1.2

All-pairs Shortest Path

Finding this for all pairs of vertices gives us all shortest paths fromvi to vj possibly going through vertices v1, v2, …, vk

– Again, note that and do not change under this step– To simplify, the notation, we can remove the superscripts

1, ,k k

i k i kd d 1, ,k k

k j k jd d

Page 15: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

15

The General Step5.1.2

All-pairs Shortest Path

Thus, the calculation is straight forward:

for ( int i = 0; i < num_vertices; ++i ) { for ( int j = 0; j < num_vertices; ++j ) { d[i][j] = std::min( d[i][j], d[i][k] + d[k][j] ); }}

Page 16: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

16

The Floyd-Warshall Algorithm5.1.2

All-pairs Shortest Path

Thus, we have found the Floyd-Warshall algorithm:

Run time?

double d[num_vertices][num_vertices];

// Initialize the matrix d: Theta(|V|^2)// ...

// Run Floyd-Warshallfor ( int k = 0; k < num_vertices; ++k ) { for ( int i = 0; i < num_vertices; ++i ) { for ( int j = 0; j < num_vertices; ++j ) { d[i][j] = std::min( d[i][j], d[i][k] + d[k][j] ); } }}

3V

Page 17: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

17

The Floyd-Warshall Algorithm5.1.2

All-pairs Shortest Path

Question: we’ve already argued that at step k, di,k and dk,j remainunchanged, would you want to avoid the calculation if i = k or j = k?

Would you perform checks to avoid a operation? V

// Run Floyd-Warshallfor ( int k = 0; k < num_vertices; ++k ) { for ( int i = 0; i < num_vertices; ++i ) { for ( int j = 0; j < num_vertices; ++j ) { if ( i != k && j != k ) { d[i][j] = std::min( d[i][j], d[i][k] + d[k][j] ); } } }}

1

Page 18: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

18

The Floyd-Warshall Algorithm5.1.2

All-pairs Shortest Path

In such a case, if you must absolutely minimize the iterations:// Run Floyd-Warshallfor ( int k = 0; k < num_vertices; ++k ) { for ( int i = 0; i < k; ++i ) { for ( int j = 0; j < k; ++j ) { d[i][j] = std::min( d[i][j], d[i][k] + d[k][j] ); }

for ( int j = k + 1; j < num_vertices; ++j ) { d[i][j] = std::min( d[i][j], d[i][k] + d[k][j] ); } }

for ( int i = k + 1; i < num_vertices; ++i ) { for ( int j = 0; j < k; ++j ) { d[i][j] = std::min( d[i][j], d[i][k] + d[k][j] ); }

for ( int j = k + 1; j < num_vertices; ++j ) { d[i][j] = std::min( d[i][j], d[i][k] + d[k][j] ); } }}

If you do this, document it well!

Page 19: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

19

Example

Consider this graph

All-pairs Shortest Path

Page 20: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

20

Example

The adjacency matrix is

This would define ourmatrix D = (dij)

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.554 0 0.333 0.931

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 21: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

21

Example

With the first pass, k = 1, we attempt passing through vertex v1

We would start: (2, 3) → (2, 1, 3)

0.191 ≯$ 0.465 + 0.101

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.554 0 0.333 0.931

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 22: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

22

Example

With the first pass, k = 1, we attempt passing through vertex v1

We would start: (2, 4) → (2, 1, 4)

0.192 ≯$ 0.465 + 0.142

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.554 0 0.333 0.931

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 23: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

23

Example

With the first pass, k = 1, we attempt passing through vertex v1

We would start: (2, 5) → (2, 1, 5)

0.587 ≯$ 0.465 + 0.277

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.554 0 0.333 0.931

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 24: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

24

Example

With the first pass, k = 1, we attempt passing through vertex v1

Here is a shorter path: (3, 2) → (3, 1, 2)

0.554 ≯ 0.245 + 0.100 = 0.345

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.554 0 0.333 0.931

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 25: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

25

Example

With the first pass, k = 1, we attempt passing through vertex v1

We update the table (3, 2) → (3, 1, 2)

0.554 ≯ 0.245 + 0.100 = 0.345

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.345 0 0.333 0.931

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 26: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

26

Example

With the first pass, k = 1, we attempt passing through vertex v1

And a second shorter path: (3, 5) → (3, 1, 5)

0.931 ≯ 0.245 + 0.277 = 0.522

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.345 0 0.333 0.931

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 27: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

27

Example

With the first pass, k = 1, we attempt passing through vertex v1

We update the table

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.345 0 0.333 0.522

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 28: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

28

Example

With the first pass, k = 1, we attempt passing through vertex v1

Continuing: (4, 2) → (4, 1, 2)

0.668 ≯$ 1.032 + 0.100

In fact, no other shorter pathsthrough vertex v1 exist

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.554 0 0.333 0.931

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 29: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

29

Example

With the next pass, k = 2, we attempt passing through vertex v2

There are three shorter paths: (5, 1) → (5, 2, 1)

0.867 ≯ 0.119 + 0.465 = 0.584 (5, 3) → (5, 2, 3)

0.352 ≯ 0.119 + 0.191 = 0.310

(5, 4) → (5, 2, 4)

0.398 ≯ 0.119 + 0.192 = 0.311

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.345 0 0.333 0.522

1.032 0.668 0.656 0 0.151

0.867 0.119 0.352 0.398 0

Page 30: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

30

Example

With the next pass, k = 2, we attempt passing through vertex v2

We update the table

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.345 0 0.333 0.522

1.032 0.668 0.656 0 0.151

0.584 0.119 0.310 0.311 0

Page 31: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

31

Example

With the next pass, k = 3, we attempt passing through vertex v3

There are three shorter paths: (2, 1) → (2, 3, 1)

0.465 ≯ 0.191 + 0.245 = 0.436 (4, 1) → (4, 3, 1)

1.032 ≯ 0.656 + 0.245 = 0.901

(5, 1) → (5, 3, 1)

0.584 ≯ 0.310 + 0.245 = 0.555

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.465 0 0.191 0.192 0.587

0.245 0.345 0 0.333 0.522

1.032 0.668 0.656 0 0.151

0.584 0.119 0.310 0.311 0

Page 32: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

32

Example

With the next pass, k = 3, we attempt passing through vertex v3

We update the table

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.436 0 0.191 0.192 0.587

0.245 0.345 0 0.333 0.522

0.901 0.668 0.656 0 0.151

0.555 0.119 0.310 0.311 0

Page 33: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

33

0 0.100 0.101 0.142 0.277

0.436 0 0.191 0.192 0.587

0.245 0.345 0 0.333 0.522

0.901 0.668 0.656 0 0.151

0.555 0.119 0.310 0.311 0

Example

With the next pass, k = 4, we attempt passing through vertex v4

There are two shorter paths: (2, 5) → (2, 4, 5)

0.587 ≯ 0.192 + 0.151 (3, 5) → (3, 4, 5)

0.522 ≯ 0.333 + 0.151

All-pairs Shortest Path

Page 34: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

34

0 0.100 0.101 0.142 0.277

0.436 0 0.191 0.192 0.343

0.245 0.345 0 0.333 0.484

0.901 0.668 0.656 0 0.151

0.555 0.119 0.310 0.311 0

Example

With the next pass, k = 4, we attempt passing through vertex v4

We update the table

All-pairs Shortest Path

Page 35: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

35

0 0.100 0.101 0.142 0.277

0.436 0 0.191 0.192 0.343

0.245 0.345 0 0.333 0.484

0.901 0.668 0.656 0 0.151

0.555 0.119 0.310 0.311 0

Example

With the last pass, k = 5, we attempt passing through vertex v5

There are three shorter paths: (4, 1) → (4, 5, 1)

0.901 ≯ 0.151 + 0.555 = 0.706 (4, 2) → (4, 5, 2)

0.668 ≯ 0.151 + 0.119 = 0.270

(4, 3) → (4, 5, 3)

0.656 ≯ 0.151 + 0.310 = 0.461

All-pairs Shortest Path

Page 36: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

36

0 0.100 0.101 0.142 0.277

0.436 0 0.191 0.192 0.343

0.245 0.345 0 0.333 0.484

0.706 0.270 0.461 0 0.151

0.555 0.119 0.310 0.311 0

Example

With the last pass, k = 5, we attempt passing through vertex v5

We update the table

All-pairs Shortest Path

Page 37: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

37

Example

Thus, we have a table of all shortest paths:

All-pairs Shortest Path

0 0.100 0.101 0.142 0.277

0.436 0 0.191 0.192 0.343

0.245 0.345 0 0.333 0.484

0.706 0.270 0.461 0 0.151

0.555 0.119 0.310 0.311 0

Page 38: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

38

What Is the Shortest Path?

This algorithm finds the shortest distances, but what are the paths corresponding to those shortest distances?– Recall that with Dijkstra’s algorithm, we could find the shortest paths by

recording the previous node– You would start at the end and work your way back…

All-pairs Shortest Path

Page 39: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

39

What Is the Shortest Path?

Suppose the shortest path from vi to vj is as follows:

All-pairs Shortest Path

Page 40: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

40

What Is the Shortest Path?

Is this path not the (vi, v5) followed by the shortest path from v5 to vj?

– If there was a shorter path from vi to vj through v5 that didn’t follow v2, v13, etc., then we would also find a shorter path from v5 to vj

All-pairs Shortest Path

Page 41: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

41

What Is the Shortest Path?

Now, suppose we have the shortest path from vi to vj which passes through the vertices v1, v2, …, vk–1

– In this example, the next vertex in the path is v5

All-pairs Shortest Path

Page 42: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

42

What Is the Shortest Path?

What if we find a shorter path passing through vk?

– In this example, all we’d have to do is now remember that the new path has v4 as the second node—the rest of the path would be recursively stored as the shortest path from v4 to vj

All-pairs Shortest Path

Page 43: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

43

What Is the Shortest Path?

In this case, let us store the shortest path moving forward:

Now, if we find a shorter path, update the value– This matrix will store the next vertex in the list in the shortest path

starting at vertex vi

All-pairs Shortest Path

,

If

If there is an edge from to

Otherwisei j

i j

p j i j

Page 44: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

44

What Is the Shortest Path?

Thus, if we ever find a shorter path, update it the next node:

All-pairs Shortest Path

unsigned int p[num_vertices][num_vertices];

// Initialize the matrix p: Theta(|V|^2)// ...

// Run Floyd-Warshallfor ( int k = 0; k < num_vertices; ++k ) { for ( int i = 0; i < num_vertices; ++i ) { for ( int j = 0; j < num_vertices; ++j ) { if ( d[i][j] > d[i][k] + d[k][j] ) { p[i][j] = p[i][k]; d[i][j] = d[i][k] + d[k][j]; } } }}

, ,i j i kp p

Page 45: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

45

Example

In our original example, initially, the next node is exactly that:

This would define ourmatrix P = (pij)

All-pairs Shortest Path

2 3 4 5

1 3 4 5

1 2 4 5

1 2 3 5

1 2 3 4

Page 46: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

46

2 3 4 5

1 3 4 5

1 2 4 5

1 2 3 5

1 2 3 4

Example

With the first pass, k = 1, we attempt passing through vertex v1

There are two shorter paths: (3, 2) → (3, 1, 2)

0.554 ≯ 0.245 + 0.100 (3, 5) → (3, 1, 5)

0.931 ≯ 0.245 + 0.277

All-pairs Shortest Path

Page 47: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

47

2 3 4 5

1 3 4 5

1 1 4 1

1 2 3 5

1 2 3 4

Example

With the first pass, k = 1, we attempt passing through vertex v1

We update each of these

All-pairs Shortest Path

Page 48: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

48

Example

After all the steps, we end up with the matrix P = (pi,j):

All-pairs Shortest Path

2 3 4 5

3 3 4 4

1 1 4 4

5 5 5 5

2 2 2 2

Page 49: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

49

2 3 4 5

3 3 4 4

1 1 4 4

5 5 5 5

2 2 2 2

Example

These are all the adjacent edges that are still the shortest distance

For each of these, pi,j = j

In all cases, the shortest distancefrom vertex 0 is the direct edge

All-pairs Shortest Path

Page 50: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

50

Example

From vertex v2, p2,3 = 3 and p2,4 = 4; we go directly to vertices v3 and v4

But p2,1 = 3 and p3,1 = 1;the shortest path to v1 is (2, 3, 1)

Also, p2,5 = 4 and p4,5 = 5;the shortest path to v5 is (2, 4, 5)

All-pairs Shortest Path

2 3 4 5

3 3 4 4

1 1 4 4

5 5 5 5

2 2 2 2

Page 51: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

51

Example

From vertex v3, p3,1 = 1 and p3,4 = 4; we go directly to vertices v1 and v4

But p3,2 = 1 and p1,2 = 2;the shortest path to v2 is (3, 1, 2)

Also, p3,5 = 4 and p4,5 = 5;the shortest path to v5 is (3, 4, 5)

All-pairs Shortest Path

2 3 4 5

3 3 4 4

1 1 4 4

5 5 5 5

2 2 2 2

Page 52: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

52

Example

From vertex v4, p4,5 = 5; we go directly to vertex v5

But p4,1 = 5, p5,1 = 2, p2,1 = 3, p3,1 = 1; the shortest path to v1 is (4, 5, 2, 3, 1)

All-pairs Shortest Path

2 3 4 5

3 3 4 4

1 1 4 4

5 5 5 5

2 2 2 2

Page 53: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

53

Example

From vertex v5, p5,2 = 2; we go directly to vertex v2

But p5,4 = 2 and p2,4 = 4; the shortest path to v4 is (5, 2, 4)

All-pairs Shortest Path

2 3 4 5

3 3 4 4

1 1 4 4

5 5 5 5

2 2 2 2

Page 54: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

54

Comment

CLRS implements it backward, so that a matrix P stores the predecessors—similar to Dijkstra’s algorithm

Another approach is to store the value k

All-pairs Shortest Path

,

If

If there is an edge from to

Otherwisei j

i j

p i i j

, ,i j k jp p

Page 55: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

55

Which Vertices are Connected?

Finally, what if we only care if a connection exists?– Recall that with Dijkstra’s algorithm, we could find the shortest paths by

recording the previous node– In this case, can make the observation that:

A path from vi to vj exists if either:

A path exists through the vertices from v1 to vk–1, or

A path, through those same nodes, exists from vi to vk and a path exists from vk to vj

All-pairs Shortest Path

Page 56: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

56

Which Vertices are Connected?

The transitive closure is a Boolean graph:

All-pairs Shortest Path

bool tc[num_vertices][num_vertices];

// Initialize the matrix tc: Theta(|V|^2)// ...

// Run Floyd-Warshallfor ( int k = 0; k < num_vertices; ++k ) { for ( int i = 0; i < num_vertices; ++i ) { for ( int j = 0; j < num_vertices; ++j ) { tc[i][j] = tc[i][j] || (tc[i][k] && tc[k][j]); } }}

Page 57: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

57

Example

Consider this directed graph– Each pair has only one directed

edge

– Vertex v1 is a source andv4 is a sink

We will apply all threematrices– Shortest distance– Paths– Transitive closure

All-pairs Shortest Path

Page 58: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

58

Example

We set up the three initial matrices

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T F

F F F F F F

F T T T T T

F T F T F T

F F T T F F

2 3 4 5 6 7

4 7

2 4 6

2 3 4 6 7

2 4 7

3 4

0 11 7 9 8 1 5

0 5 5

9 0 7 2

0

4 8 8 0 10 6

5 6 0 3

10 7 0

Page 59: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

59

Example

At step 1, no path leads to v1, sono shorter paths could be foundpassing through v1

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T F

F F F F F F

F T T T T T

F T F T F T

F F T T F F

2 3 4 5 6 7

4 7

2 4 6

2 3 4 6 7

2 4 7

3 4

0 11 7 9 8 1 5

0 5 5

9 0 7 2

0

4 8 8 0 10 6

5 6 0 3

10 7 0

Page 60: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

60

Example

At step 2, we find:– A path (3, 2, 7) of length 14

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T F

F F F F F F

F T T T T T

F T F T F T

F F T T F F

2 3 4 5 6 7

4 7

2 4 6

2 3 4 6 7

2 4 7

3 4

0 11 7 9 8 1 5

0 5 5

9 0 7 2

0

4 8 8 0 10 6

5 6 0 3

10 7 0

Page 61: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

61

Example

At step 2, we find:– A path (3, 2, 7) of length 14

We update

d3,7 = 14, p3,7 = 2 and c3,7 = T

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T T

F F F F F F

F T T T T T

F T F T F T

F F T T F F

2 3 4 5 6 7

4 7

2 4 6 2

2 3 4 6 7

2 4 7

3 4

0 11 7 9 8 1 5

0 5 5

9 0 7 2 14

0

4 8 8 0 10 6

5 6 0 3

10 7 0

Page 62: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

62

Example

At step 3, we find:– A path (7, 3, 2) of length 19– A path (7, 3, 6) of length 12

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T T

F F F F F F

F T T T T T

F T F T F T

F F T T F F

2 3 4 5 6 7

4 7

2 4 6 2

2 3 4 6 7

2 4 7

3 4

0 11 7 9 8 1 5

0 5 5

9 0 7 2 14

0

4 8 8 0 10 6

5 6 0 3

10 7 0

Page 63: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

63

Example

At step 3, we find:– A path (7, 3, 2) of length 19– A path (7, 3, 6) of length 12

We update

d7,2 = 19, p7,2 = 3 and c7,2 = T

d7,6 = 12, p7,6 = 3 and c7,6 = T

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T T

F F F F F F

F T T T T T

F T F T F T

F T T T F T

2 3 4 5 6 7

4 7

2 4 6 2

2 3 4 6 7

2 4 7

3 3 4 3

0 11 7 9 8 1 5

0 5 5

9 0 7 2 14

0

4 8 8 0 10 6

5 6 0 3

19 10 7 12 0

Page 64: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

64

Example

At step 4, there are no paths outof vertex v4, so we are finished

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T T

F F F F F F

F T T T T T

F T F T F T

F T T T F T

2 3 4 5 6 7

4 7

2 4 6 2

2 3 4 6 7

2 4 7

3 3 4 3

0 11 7 9 8 1 5

0 5 5

9 0 7 2 14

0

4 8 8 0 10 6

5 6 0 3

19 10 7 12 0

Page 65: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

65

Example

At step 5, there is one incomingedge from v1 to v5, and it doesn’tmake any paths out of vertex v1

any shorter...

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T T

F F F F F F

F T T T T T

F T F T F T

F T T T F T

2 3 4 5 6 7

4 7

2 4 6 2

2 3 4 6 7

2 4 7

3 3 4 3

0 11 7 9 8 1 5

0 5 5

9 0 7 2 14

0

4 8 8 0 10 6

5 6 0 3

19 10 7 12 0

Page 66: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

66

Example

At step 6, we find:– A path (1, 6, 2) of length 6– A path (1, 6, 4) of length 7– A path (1, 6, 7) of length 4– A path (3, 6, 2) of length 7– A path (3, 6, 7) of length 5– A path (7, 3, 6, 2) of length 17

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T T

F F F F F F

F T T T T T

F T F T F T

F T T T F T

2 3 4 5 6 7

4 7

2 4 6 2

2 3 4 6 7

2 4 7

3 3 4 3

0 11 7 9 8 1 5

0 5 5

9 0 7 2 14

0

4 8 8 0 10 6

5 6 0 3

19 10 7 12 0

Page 67: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

67

Example

At step 6, we find:– A path (1, 6, 2) of length 6– A path (1, 6, 4) of length 7– A path (1, 6, 7) of length 4– A path (3, 6, 2) of length 7– A path (3, 6, 7) of length 5– A path (7, 3, 6, 2) of length 17

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T T

F F F F F F

F T T T T T

F T F T F T

F T T T F T

6 3 6 5 6 6

4 7

6 4 6 6

2 3 4 6 7

2 4 7

3 3 4 3

0 6 7 7 8 1 4

0 5 5

7 0 7 2 5

0

4 8 8 0 10 6

5 6 0 3

17 10 7 12 0

Page 68: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

68

Example

At step 7, we find:– A path (2, 7, 3) of length 15– A path (2, 7, 6) of length 17– A path (6, 7, 3) of length 13

All-pairs Shortest Path

T T T T T T

F F T F F T

F T T F T T

F F F F F F

F T T T T T

F T F T F T

F T T T F T

6 3 6 5 6 6

4 7

6 4 6 6

2 3 4 6 7

2 4 7

3 3 4 3

0 6 7 7 8 1 4

0 5 5

7 0 7 2 5

0

4 8 8 0 10 6

5 6 0 3

17 10 7 12 0

Page 69: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

69

Example

Finally, at step 7, we find:– A path (2, 7, 3) of length 15– A path (2, 7, 6) of length 17– A path (6, 7, 3) of length 13

All-pairs Shortest Path

T T T T T T

F T T F T T

F T T F T T

F F F F F F

F T T T T T

F T T T F T

F T T T F T

6 3 6 5 6 6

7 4 7 7

6 4 6 6

2 3 4 6 7

2 7 4 7

3 3 4 3

0 6 7 7 8 1 4

0 15 5 17 5

7 0 7 2 5

0

4 8 8 0 10 6

5 13 6 0 3

17 10 7 12 0

Page 70: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

70

Example

Note that:

– From v1 we can go anywhere

– From v5 we can go anywhere but v1

– We go between any of the verticesin the set {v2, v3, v6, v7}

– We can’t go anywhere from v4

All-pairs Shortest Path

T T T T T T

F T T F T T

F T T F T T

F F F F F F

F T T T T T

F T T T F T

F T T T F T

6 3 6 5 6 6

7 4 7 7

6 4 6 6

2 3 4 6 7

2 7 4 7

3 3 4 3

0 6 7 7 8 1 4

0 15 5 17 5

7 0 7 2 5

0

4 8 8 0 10 6

5 13 6 0 3

17 10 7 12 0

Page 71: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

71

Example

We could reinterpret this graph as follows:– Vertices {v2, v3, v6, v7} form a strongly connected subgraph

– You can get from any onevertex to any other

– With the transitive closure graph,it is much faster finding suchstrongly connected components

All-pairs Shortest Path

0 6 7 7 8 1 4

0 15 5 17 5

7 0 7 2 5

0

4 8 8 0 10 6

5 13 6 0 3

17 10 7 12 0

Page 72: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

72

Summary

This topic:– The concept of all-pairs shortest paths– The Floyd-Warshall algorithm– Finding the shortest paths– Finding the transitive closure

All-pairs Shortest Path

Page 73: Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca

73

References

Cormen, Leiserson, Rivest and Stein,Introduction to Algorithms, The MIT Press, 2001, §25.2, pp.629-35.

Mark A. Weiss,Data Structures and Algorithm Analysis in C++, 3rd Ed., Addison Wesley, 2006, Ch.?, p.?.

Joh Kleinberg and Eva Tardos,

Algorithm Design, Pearson, 2006.

Elliot B. Koffman and Paul A.T. Wolfgang,

Objects, Abstractions, Data Structures and Design using C++, Wiley, 2006.

These slides are provided for the ECE 250 Algorithms and Data Structures course. The material in it reflects Douglas W. Harder’s best judgment in light of the information available to him at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.

All-pairs Shortest Path