floyd-warshall algorithm

44
Department of Computer Science and Information Engineering 1 Floyd-Warshall Algorithm Instructor: Yao-Ting Huang Bioinformatics Laboratory, Department of Computer Science & Information Engineering, National Chung Cheng University.

Upload: others

Post on 17-Apr-2022

22 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Floyd-Warshall Algorithm

Department of Computer Science

and Information Engineering

1

Floyd-Warshall Algorithm

Instructor: Yao-Ting Huang

Bioinformatics Laboratory,

Department of Computer Science & Information Engineering,

National Chung Cheng University.

Page 2: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

2

SLOW-ALL-PAIRS-SHORTEST-PATHS(W)

1 n row W [ ]

2 WL )1(

3 for m2 to n-1

4 do )(mL EXTENDED-SHORTEST-PATHS( ),)1( WL m

5 return )1( nL

Slow All pair shortest Path Algorithm

Page 3: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

3

FASTER-ALL-PAIRS-SHORTEST-PATHS(W)

1 n row W [ ]

2 WL )1(

3 m1

4 while n m 1

5 do )2( mL EXTENDED-SHORTEST-PATHS( ), )()( mm LL

6 m m2

7 return )(mL

Fast All pair shortest Path Algorithm

Page 4: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

4

06

052

04

710

4830

)1(L

0618

20512

11504

71403

42830

)2(L

All pair shortest Path Algorithm

2

1

5 4

3

3

6

-4

7

2

8

1

4

-5

Can we do better?

Page 5: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

5

Robert Floyd

Robert W Floyd (1936 –2001) finished school at age

14.

At the University of Chicago, he received a Bachelor's

degree in liberal arts when still only 17 and a second

Bachelor's degree in physics in 1958.

Becoming a computer operator in the early 1960s,

he began publishing many noteworthy papers.

He was appointed an associate professor at Carnegie

Mellon University by the time he was 27.

He became a full professor at Stanford University six

years later without a Ph.D. degree.

Page 6: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

6

Review of Bellman-Ford Algorithm

j

i

The shortest paths are computed on the basis of

number of path edges.

DP over the number of path edges.

e.g.,

i to a: one edge

i to b: two edges

i to c: three edges

a

b

c

Page 7: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

7

Floyd-Warshall Algorithm

j

i

Idea: the shortest path must go through some vertices,

which are also optimal subpaths.

e.g., a->b->c must be the shortest path between a and c.

Both short paths of i to j and a to c must visit through b.

a

b

c

Page 8: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

8

Floyd-Warshall Algorithm

j

i

Remember that not only paths i to j may go through b.

Why not computing the shortest paths through b first

(i.e., subproblem of b)?

a

b

c

Page 9: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

9

Floyd-Warshall Algorithm

DP on the subset of intermediate vertices.

All vertices are numbered from 1 to n.

Let Vk={1, 2, …, k}.

Sk(i, j) = length of a shortest path between i and j

whose interior vertices are all inVk .

Sk(i, j) = ??

j

ia

b

c

Page 10: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

10

Key Observation

i

k

j

P1 P2

P1:All intermediate vertices in {1,2,..,k-1} P2:All intermediate vertices in {1,2,..,k-1}

P: All intermediate vertices in {1,2,..,k}

Sk-1(i, k) Sk-1(k, j)

Sk (i, j)

Page 11: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

11

Floyd-Warshall DP Algorithm

Let Vk={1, 2, …, k}.

Sk(i, j) = length of a shortest path between i and j

whose interior vertices are all inVk .

Sk(i, j) = ??

Sk(i, j) = min{ Sk-1(i, j), Sk-1(i, k)+Sk-1(k, j) }.

j

ki

Page 12: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

12

Illustration

The shortest path P consists of shortest

subpaths: Vk={v1}

The shortest path between v5 and v4 (through v1) is

found.

s v1 vv2 v3v4v5

Page 13: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

13

Illustration

The shortest path P consists of shortest

subpaths. Vk={v1, v2}

The shortest path between s and v5 (through v2) is found.

s v1 vv2 v3v4v5

Page 14: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

14

Illustration

The shortest path P consists of shortest

subpaths. Vk={v1, v2, v3}

The shortest path between v4 and v (through v3) is found.

s v1 vv2 v3v4v5

Page 15: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

15

Illustration

The shortest path P consists of shortest

subpaths: Vk={v1, v2, v3, v4}

The shortest path between v5 and v (through v4) is found.

s v1 vv2 v3v4v5

Page 16: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

16

Illustration

The shortest path P consists of shortest

subpaths: Vk={v1, v2, v3, v4, v5}

The shortest path between s and v (through v5) is found.

s v1 vv2 v3v4v5

Page 17: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

Higher-order Illustration

Vk={v1}

The shortest paths of all pairs that visit through v1

will be found.

17

v1 v4v5

v7

v6

v2

v3

Page 18: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

18

Bottom-Up Dynamic Programming

Floyd(W[1:n,1:n],S[1:n,1:n])

For i =1 to n

For j =1 to n

if <i, j> in E then S(i, j) ← W(i, j) else S(i, j) ← ∞

For k = 1 to n

For i = 1 to n

For j = 1 to n

if S[i, j] > S[i, k] + S[k, j] then

S[i, j] ← S[i, k] + S[k, j]

Page 19: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

19

Comparison with DP by Edges

At most m-1

edges

0. since },{min

}}{min ,min{

)1(

1

)1(

1

)1()(

jjkj

m

iknk

kj

m

iknk

m

ij

m

ij

wwl

wlll

wkj

likm-1

i j

Page 20: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

20

Comparison with DP by Edges

06

052

04

710

4830

)1(L

EXTENDED-SHORTEST-PATHS(L, w)

1 ][Lrown

2 Let )( ijlL be an n n matrix

3 for 1i to n

4 do for 1j to n

5 do ijl

6 for 1k to n

7 do ),min(

kjikijijwlll

8 return L

SLOW-ALL-PAIRS-SHORTEST-PATHS(W)

1 n row W [ ]

2 WL )1(

3 for 2m to n-1

4 do )(mL EXTENDED-SHORTEST-PATHS( ),)1( WL m

5 return )1( nL

Page 21: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

21

Dynamic Programming

j

ki

Sk(i, j) = length of the shortest path between i

and j whose interior vertices are all inVk.

Sk(i, j) = min of

• Sk(i, j) = Sk-1(i, k) + Sk-1(k ,j).

• If k is on the shortest path from i to j

• Sk(i, j) = Sk-1(i, j).

• If k is not on the shortest path from i to j

How many subproblems?

Page 22: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

22

Bottom-Up Dynamic Programming

j

j

i

i

k

k

Sk (k=1,2,3,4,5)

Page 23: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

23

Bottom-Up Dynamic Programming

5

2

4

3

1

1

43

3

6

4

1

2

2

S0=W=

i=1

j=1 2

5

2

5

Page 24: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

24

Bottom-Up Dynamic Programming

5

2

4

3

1

1

43

3

6

4

1

2

2S0=W=

i=1

j=1 2

5

2

5

0

0

0

0

0

4 3

6 2

1

4 2 3

1∞∞

∞∞∞

∞∞

∞∞

For k = 1 to n

For i = 1 to n

For j = 1 to n

if S[i, j] > S[i, k] + S[k, j] then

S[i, j] ← S[i, k] + S[k, j]

Page 25: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

25

Bottom-Up Dynamic Programming

5

2

4

3

1

1

43

3

6

4

1

2

2

i=1

5

2

0

0

0

0

0

4 3

6 2

1

4 2 3

1∞∞

∞∞

∞∞

S1=

For k = 1 to n

For i = 1 to n

For j = 1 to n

if S[i, j] > S[i, k] + S[k, j] then

S[i, j] ← S[i, k] + S[k, j]

S0=W=

i=1

j=1 2

5

2

5

0

0

0

0

0

4 3

6 2

1

4 2 3

1∞∞

∞∞∞

∞∞

∞∞

Page 26: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

26

Bottom-Up Dynamic Programming

5

2

4

3

1

1

43

3

6

4

1

2

2

i=1

5

2

0

0

0

0

0

4 3

6 2

1

4 2 3

1∞∞

8

∞45

∞∞

∞∞

S1=

For k = 1 to n

For i = 1 to n

For j = 1 to n

if S[i, j] > S[i, k] + S[k, j] then

S[i, j] ← S[i, k] + S[k, j]

Page 27: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

27

5

2

4

3

1

1

43

3

6

4

1

2

2

i=1

j=1 2

5

2

5

0

0

0

0

0

4 3

6 2

1

4 2 3

1∞∞

∞∞∞

∞∞

∞∞

i=1

5

2

0

0

0

0

0

4 3

3 2

1

3 2 3

162

7

745

74

65

5

S5=

S0=W=

Page 28: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

28

Analysis

Running time is clearly O(|V|3).

Faster than all previous algorithms.

O(|V|4) and O(|V|3lg|V|).

Page 29: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

29

Remarks

Is negative edges allowed?

Is it possible to detect negative cycle

using Floyd-Warshall’s algorithm?

Page 30: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

Remarks

Is it possible to detect negative cycle

using Floyd-Warshall’s algorithm?

30

s

u v

-1 -1

-1

Page 31: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

31

Extracting the Shortest Paths

How can we retrieve the shortest paths?

Maintain a predecessor p[i, j].

If the shortest path between i and j does not pass

through any intermediate vertex, then the

predecessor p[i, j] = 0.

0

0

0

0

0

4 3

3 2

1

3 2 3

162

7

745

74

65

5

Page 32: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

32

How do we backtrack the actual path?

Maintain a predecessor p[i, j].

If the shortest path between i and j does not pass

through any intermediate vertex, then the predecessor

p[i, j] = 0.

Define Pk[i, j] as follows:

P0[i, j] = 0 for all i, j in V

Pk[i, j] = Pk-1[i, j], if Sk[i, j] = Sk-1[i, j]

Pk[i, j] = k, if Sk[i, j] ≠ Sk-1[i, j]

Page 33: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

33

How do we retrieve the actual path?

We maintain Pk[i,j] as follows:

P0[i, j] = 0 for all i,j in V

Pk[i, j] = Pk-1[i,j], if Sk[i,j] = Sk-1[i,j]

Pk[i, j] = k, if Sk[i,j] ≠ Sk-1[i,j]

To reconstruct the shortest path in Pn[i, j]

If Pn[i, j]=0, edge <i, j> is the shortest path.

If Pn[i, j]=k, then k is an interior vertex on the path, other interior vertices can be obtained by checking Pn[i, k] and Pn[k, j].

Page 34: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

34

Floyd’s Algorithm (with matrix P)

Floyd(W[1:n,1:n],S[1:n,1:n],P[1:n,1:n])

For i =1 to n

For j =1 to n

P[i,j] ← 0

if <i,j> in E then S[i,j] ← W[i,j] else S[i,j] ← ∞

For k = 1 to n

For i = 1 to n

For j = 1 to nif S[i,j] > S[i,k] + S[k,j] then

S[i,j] ← S[i,k] + S[k,j]

P[i,j] ← k

Page 35: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

35

A detailed example

5

2

4

3

1

1

43

3

6

4

1

2

2

i=1

j=1 2

5

2

5

0

0

0

0

0

4 0

5 0

0

3 0 0

033

3

211

55

20

3

i=1

5

2

0

0

0

0

0

4 3

3 2

1

3 2 3

162

7

745

74

65

5

S5=

P5=

How do we retrieve the

shortest path from 3 to 5?

Page 36: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

36

A detailed example

5

2

4

3

1

1

43

3

6

4

1

2

2

i=1

j=1 2

5

2

5

0

0

0

0

0

4 0

5 0

0

3 0 0

033

3

211

55

20

3

i=1

5

2

0

0

0

0

0

4 3

3 2

1

3 2 3

162

7

745

74

65

5

S5=

P5=

How do we retrieve the

shortest path from 3 to 5?

P[3,5]=2, so 3…2…5

Page 37: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

37

A detailed example

5

2

4

3

1

1

43

3

6

4

1

2

2

i=1

j=1 2

5

2

5

0

0

0

0

0

4 0

5 0

0

3 0 0

033

3

211

55

20

3

i=1

5

2

0

0

0

0

0

4 3

3 2

1

3 2 3

162

7

745

74

65

5

S5=

P5=

How do we retrieve the

shortest path from 3 to 5?

P[3,5]=2, so 3…2…5

P[3,2]=1,so 3…1…2…5

P[3,1]=P[2,5]=P[1,2]=0

So: 3→1→2→5.

Page 38: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

38

Application: Transitive Closure

Given directed graph G = (V, E)

Compute transitive closure G* = (V, E* )

E* = {(i, j) : there is path from i to j in G}

How to apply the previous algorithm to solve this

problem?

3

2

1

3

2

1

Page 39: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

39

Application: Transitive Closure

Given directed graph G = (V, E)

Compute transitive closure G* = (V, E* )

E* = {(i, j) : there is path from i to j in G}

How to apply the previous algorithm to solve this

problem?

1

4 3

2

Page 40: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

40

Transitive Closure – Solution1

Using Floyd-Warhshall Algorithm

Assign weight of 1 to each edge, then run FLOYD-

WARSHALL with this weight.

If dij(n) < n, there is a transitive path from i to j.

Page 41: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

41

Transitive Closure – Solution2

Instead of D(k), we have T(k) = (tij(k))

tij(0) = 0 (if i != j and (i, j) not in E)

1 (if i = j or (i, j) in E)

tij(k) = 1 (if there is a path from i to j with all

intermediate vertices in {1, 2,…, k})

(tij(k-1) is 1) or (tik

(k-1) is 1 and tkj(k-1) is 1)

0 (otherwise)

Page 42: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

42

Transitive Closure – Solution2

TRANSITIVE-CLOSURE(E, n)

for i = 1 to n

do for j = 1 to n

do if i=j or (i, j) in E

then tij(0) = 1

else tij(0) = 0

for k = 1 to n

do for i = 1 to n

do for j = 1 to n

do tij(k) = tij

(k-1) || ( tik(k-1) && tkj

(k-1) )

return T(n)

Page 43: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

43

1

4 3

2

1101

0110

1110

0001

)0(T

1101

0110

1110

0001

)1(T

1101

1110

1110

0001

)2(T

1111

1110

1110

0001

)3(T

1111

1111

1111

0001

)4(T

Page 44: Floyd-Warshall Algorithm

Dept. of Computer Science

& Information Engineering

44

Conclusion

Floyd-Warshall runs in O(V3) time, which is faster than previous algorithms and easy to implement.

The running time is independent of the number of edges.

The dynamic programming focuses on the vertices instead of edges.