floyd’s algorithm (shortest-path problem) section 8.2
Post on 15-Jan-2016
244 Views
Preview:
TRANSCRIPT
Floyd’s AlgorithmFloyd’s Algorithm(shortest-path (shortest-path
problem)problem)
Section 8.2Section 8.2
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
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
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
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 movie to watch if you want to…you want to…
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 EE
AA 00
BB 00
CC 00
DD 00
EE 00
8
8
3
3 4
4
2
2
1
1 8
81
1
1
1
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.
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.
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.
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 EE
AA 00
BB 00
CC 00
DD 00
EE 00
8
8
3
3 4
4
2
2
1
1 8
81
1
1
1
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 EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
A B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
M{}
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 EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
A B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 88 33 11
BB 88 00 44 99 22
CC 33 44 00 11 11
DD 11 99 11 00 88
EE 22 11 88 00
M{A}
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 EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
A B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 88 33 11 1010
BB 88 00 44 99 22
CC 33 44 00 11 11
DD 11 99 11 00 88
EE 1010 22 11 88 00
M{A,B}
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 EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
A B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 88 33 11 1010
BB 88 00 44 99 22
CC 33 44 00 11 11
DD 11 99 11 00 88
EE 1010 22 11 88 00
M{A,B,C}
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 EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
A B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 77 33 11 44
BB 77 00 44 55 22
CC 33 44 00 11 11
DD 11 55 11 00 22
EE 44 22 11 22 00
M{A,B,C}
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 EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
A B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 77 22 11 44
BB 77 00 44 55 22
CC 22 44 00 11 11
DD 11 55 11 00 22
EE 44 22 11 22 00
M{A,B,C,D}
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 EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
A B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 66 22 11 33
BB 66 00 44 55 22
CC 22 44 00 11 11
DD 11 55 11 00 22
EE 33 22 11 22 00
M{A,B,C,D}
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 EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
A B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 66 22 11 33
BB 66 00 44 55 22
CC 22 44 00 11 11
DD 11 55 11 00 22
EE 33 22 11 22 00
M{A,B,C,D,E}
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 EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
A B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 55 22 11 33
BB 55 00 33 55 22
CC 22 33 00 11 11
DD 11 55 11 00 22
EE 33 22 11 22 00
M{A,B,C,D,E}
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?
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?
Floyd’s AlgorithmFloyd’s AlgorithmA B
C
D E
8
21
8
3 4
11
AA BB CC DD EE
AA 00 88 33 11
BB 88 00 44 22
CC 33 44 00 11 11
DD 11 11 00 88
EE 22 11 88 00
Allowing no hops…M[D][E] = 8
What if we allow a hop through C?
M[D][C] = 1 and M[C][E] = 1
If we allow a hop through C, what can we compare to see if M[D][E] needs to be updated?
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?
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?
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]
top related