floyd’s algorithm (shortest-path problem)

25
Floyd’s Algorithm Floyd’s Algorithm (shortest-path (shortest-path problem) problem) Section 8.2 Section 8.2

Upload: uriel-jimenez

Post on 14-Mar-2016

66 views

Category:

Documents


0 download

DESCRIPTION

Floyd’s Algorithm (shortest-path problem). Section 8.2. Shortest-path Problem. Given: A set of objects (called vertices) and A set of distances between objects (called edges) Find: The shortest path from a designated starting point The shortest path between any pair of vertices. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm(shortest-path (shortest-path

problem)problem)Section 8.2Section 8.2

Page 2: Floyd’s Algorithm (shortest-path problem)

Shortest-path ProblemShortest-path Problem► Given: Given: ► A set of objects (called vertices) andA set of objects (called vertices) and► A set of distances between objects (called A set of distances between objects (called

edges)edges)► Find:Find:► The shortest path from a designated starting The shortest path from a designated starting

pointpoint► The shortest path between any pair of The shortest path between any pair of

verticesvertices

Page 3: Floyd’s Algorithm (shortest-path problem)

Shortest-path ProblemShortest-path Problem► Given: Given: ► A set of objects (called vertices) andA set of objects (called vertices) and► A set of distances between objects (called A set of distances between objects (called

edges)edges)► Find:Find:► The shortest path from a designated starting The shortest path from a designated starting

pointpoint► The shortest path between any pair of verticesThe shortest path between any pair of vertices► Floyd’s algorithm solves this problemFloyd’s algorithm solves this problem

Page 4: Floyd’s Algorithm (shortest-path problem)

Shortest-path ProblemShortest-path Problem► Given: Given: ► A set of objects (called vertices) andA set of objects (called vertices) and► A set of distances between objects (called A set of distances between objects (called

edges)edges)► Find:Find:► The shortest path from a designated starting The shortest path from a designated starting

pointpoint► BTW, Dijkstra’s algorithm solves this oneBTW, Dijkstra’s algorithm solves this one► The shortest path between any pair of verticesThe shortest path between any pair of vertices

Page 5: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Dynamic Dynamic

Programming Programming AlgorithmAlgorithm

► It uses an N X N It uses an N X N matrixmatrix

► N is the # of verticesN is the # of vertices► BTW, Pink Floyd’s BTW, Pink Floyd’s

The Wall is great The Wall is great movie to watch if you movie to watch if you want to…want to…

Page 6: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Recall a graph can be represented using an Recall a graph can be represented using an

N X N matrixN X N matrix

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00

BB 00

CC 00

DD 00

EE 00

88

3

3 4

4

2

21

1 881

11

1

Page 7: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Dynamic ProgrammingDynamic Programming

Solves one step of the problemSolves one step of the problem Stores it, so it doesn’t have to be Stores it, so it doesn’t have to be

recomputedrecomputed Uses stored step to solve the next stepUses stored step to solve the next step Avoids re-computing progressive stepsAvoids re-computing progressive steps

► Many Algorithms (Brute Force, even Divide-Many Algorithms (Brute Force, even Divide-and Conquer) re-compute the same and Conquer) re-compute the same information over and over again.information over and over again.

Page 8: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm►Dynamic ProgrammingDynamic Programming

Uses extra memory to store steps Uses extra memory to store steps (or sub-problems)(or sub-problems)

Avoids re-computationAvoids re-computation However, the extra memory can be However, the extra memory can be

expensive.expensive.

Page 9: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Famous Example of Dynamic ProgrammingFamous Example of Dynamic Programming

Optimal sequence alignment algorithmOptimal sequence alignment algorithm►Most famous version is called the Smith-Most famous version is called the Smith-

Waterman AlgorithmWaterman Algorithm Align two sequences of length NAlign two sequences of length N

►Previous algorithm O(2Previous algorithm O(2NN) comparisons O(N) ) comparisons O(N) memorymemory

►Smith-Waterman Algorithm O(NSmith-Waterman Algorithm O(N22) comparisons ) comparisons O(NO(N22) memory.) memory.

In this case the trade-off was worth it.In this case the trade-off was worth it.

Page 10: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Back to the shortest-path problem.Back to the shortest-path problem.

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00

BB 00

CC 00

DD 00

EE 00

88

3

3 4

4

2

21

1 881

11

1

Page 11: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► First step, where can we go First step, where can we go

without using an intermediate without using an intermediate vertexvertex

► MMkk, where k the set of , where k the set of intermediate verticesintermediate vertices

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

M{}

Page 12: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Second step, where can we go if Second step, where can we go if

we use A as an intermediate we use A as an intermediate verticesvertices

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 99 22CC 33 44 00 11 11DD 11 99 11 00 88EE 22 11 88 00

M{A}

Page 13: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Third step, where can we go if we Third step, where can we go if we

use A and B as intermediate use A and B as intermediate verticesvertices

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 88 33 11 1010BB 88 00 44 99 22CC 33 44 00 11 11DD 11 99 11 00 88EE 1010 22 11 88 00

M{A,B}

Page 14: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Fourth step, where can we go if Fourth step, where can we go if

we use A, B, and C as we use A, B, and C as intermediate verticesintermediate vertices

► You tell me!You tell me!

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 88 33 11 1010BB 88 00 44 99 22CC 33 44 00 11 11DD 11 99 11 00 88EE 1010 22 11 88 00

M{A,B,C}

Page 15: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Fourth step, where can we go if Fourth step, where can we go if

we use A, B, and C as we use A, B, and C as intermediate verticesintermediate vertices

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 77 33 11 44BB 77 00 44 55 22CC 33 44 00 11 11DD 11 55 11 00 22EE 44 22 11 22 00

M{A,B,C}

Page 16: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Fifth step, where can we go if we Fifth step, where can we go if we

use A, B, C, and D as intermediate use A, B, C, and D as intermediate verticesvertices

► You tell me!You tell me!

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 77 22 11 44BB 77 00 44 55 22CC 22 44 00 11 11DD 11 55 11 00 22EE 44 22 11 22 00

M{A,B,C,D}

Page 17: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Fifth step, where can we go if we Fifth step, where can we go if we

use A, B, C, and D as intermediate use A, B, C, and D as intermediate verticesvertices

► OK, here is the answer.OK, here is the answer.

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 66 22 11 33BB 66 00 44 55 22CC 22 44 00 11 11DD 11 55 11 00 22EE 33 22 11 22 00

M{A,B,C,D}

Page 18: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Final step, where can we go if we Final step, where can we go if we

use all the vertices as use all the vertices as intermediates.intermediates.

► You tell me!You tell me!

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 66 22 11 33BB 66 00 44 55 22CC 22 44 00 11 11DD 11 55 11 00 22EE 33 22 11 22 00

M{A,B,C,D,E}

Page 19: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► Final step, where can we go if we Final step, where can we go if we

use all the vertices as use all the vertices as intermediates.intermediates.

► You tell me!You tell me!

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

A B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 55 22 11 33BB 55 00 33 55 22CC 22 33 00 11 11DD 11 55 11 00 22EE 33 22 11 22 00

M{A,B,C,D,E}

Page 20: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm►So how do we actually compute So how do we actually compute

(update) the matrix?(update) the matrix?►First of all…First of all…►Given N vertices how many steps are Given N vertices how many steps are

there?there?► i.e., how many times must the matrix i.e., how many times must the matrix

be updated?be updated?

Page 21: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► for (int k = 0; k < N; k++)for (int k = 0; k < N; k++)

Update the matrix, i.e. compute MUpdate the matrix, i.e. compute M{k}{k}

►Next…Next…►How would you actually update each How would you actually update each

matrix cell?matrix cell?► i.e., how would you iterate through the i.e., how would you iterate through the

matrix?matrix?

Page 22: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s AlgorithmA B

C

D E

8

21

8

3 4

11

AA BB CC DD EEAA 00 88 33 11

BB 88 00 44 22CC 33 44 00 11 11DD 11 11 00 88EE 22 11 88 00

Allowing no hops…M[D][E] = 8What if we allow a hop through C?M[D][C] = 1 and M[C][E] = 1If we allow a hop through C, what can we compare to see if M[D][E] needs to be updated?

Page 23: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm►for (int k = 0; k < N; k++)for (int k = 0; k < N; k++)

for (int i = 0; i < N; i++)for (int i = 0; i < N; i++)►for (int j = 0; j < N; j++)for (int j = 0; j < N; j++)

update Mupdate M{k}{k}[i][j][i][j]

► What do we have to compare to see if MWhat do we have to compare to see if M{k}{k}[i][i][j] needs to be updated?[j] needs to be updated?

Page 24: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm►for (int k = 0; k < N; k++)for (int k = 0; k < N; k++)

for (int i = 0; i < N; i++)for (int i = 0; i < N; i++)►for (int j = 0; j < N; j++)for (int j = 0; j < N; j++)

if (M[i][k]+M[k][j] < M[i][j]) thenif (M[i][k]+M[k][j] < M[i][j]) then

► How do we do the update?How do we do the update?

Page 25: Floyd’s Algorithm (shortest-path problem)

Floyd’s AlgorithmFloyd’s Algorithm► for (int k = 0; k < N; k++)for (int k = 0; k < N; k++)

for (int i = 0; i < N; i++)for (int i = 0; i < N; i++)►for (int j = 0; j < N; j++)for (int j = 0; j < N; j++)

if (M[i][k]+M[k][j] < M[i][j]) thenif (M[i][k]+M[k][j] < M[i][j]) then►M[i][j] = M[i][k]+M[k][j] M[i][j] = M[i][k]+M[k][j]