Download presentation
Presentation is loading. Please wait.
Published byDaniel Perkins Modified over 9 years ago
1
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Dynamic Programming
2
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Development of a Dynamic Programming Algorithm 1.Characterize the structure of a solution 2.Recursively define the value of a solution 3.Compute the value of a solution in a bottom-up fashion 4.Construct a solution from computed information
3
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Definition –Algorithmic Pattern: (example - Trees) Solution DP( Tree t ) //Compute the base cases (leaves) for all leaves l of tree t table[l] = process(l); //Compute the recursive cases bottom-up for each node n of tree t value = for each child c of n value += table[c] table[n] = value + process(n); return table(root)
4
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Applicability –Use the dynamic programming algorithmic pattern when ALL of the following are true: The problem lends itself to division into sub- problems of the same type The sub-problems have considerable overlap in their solution An acceptable solution to the problem can be constructed from acceptable solutions to sub- problems Extra memory is readily available
5
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Well-Known Uses –Academic “n” Coins –Mathematics Fibonacci sequence Binomial Coeficient Matrix Multiplication –Graphs Shortest Path Binary Search Trees Traveling Salesman –String Edit distance (similarity) String alignment
6
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: NCoins –How many coins will be given in change for a purchase of up to $1.00 –Structure of Optimal Solution: The set of coins can be broken into 2 piles each of which is optimal –Recursive Definition
7
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: NCoins –Build a table starting at the base case Work from the bottom sub-problems to the top sub-problems –This will work as long as when we need to compute the value for N we already have the following values in our table: N-1, N-5, N- 10, N-12, N-25 0¢0¢ 1¢1¢ 2¢2¢ 3¢3¢ 4¢4¢ 5¢5¢ 6¢6¢ 7¢7¢ 0 1 2 3 4 1 2 3
8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: NCoins –However, this table gives only the minimum number of coins needed This is the thing being optimized –Also want the actual coins used To do this we build a second table as we are building the first table –The second table contains, at each step, the coins that produced the optimal number of coins
9
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: NCoins –We can then recursively reconstruct the list of coins used to produce the minimum number of coins We stop when we reach a base case (0 in this example) –The second table is constructed during the construction of the first table, but is never used to determine values in the first table And the first table is never used during the recursive reconstructing of the coin list 0¢0¢ 1¢1¢ 2¢2¢ 3¢3¢ 4¢4¢ 5¢5¢ 6¢6¢ 7¢7¢ 0 1 1 1 1 5 1 or 5 0 1 2 3 4 1 2 3
10
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Development of a Dynamic Programming Algorithm 1.Characterize the structure of a solution 2.Recursively define the value of a solution 3.Compute the value of a solution in a bottom-up fashion 4.Construct a solution from computed information
11
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication 1x2 + 2x5 + 3x8 = 2 + 10 + 24 = 36 123 456 123 456 789 303642 668196 rows from the first matrix columns from the second matrix
12
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication –“inner” dimensions must match –result is “outer” dimension –Examples: 2 3 X 3 3 = 2x3 3 4 X 4 5 = 3x5 2 3 X 4 3 = cannot multiply –Question: Does AB = BA? Hint: let A be a 2 3 matrix and B be 3 2 matrix
13
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication public static Matrix mult(Matrix m1, Matrix m2) { Matrix result = new Matrix(); for (int i=0; i<m1.numRow(); i++) { for (int j=0; j<m2.numCol(); j++) { double total = 0.0; for (int k=0; k<m1.numCol(); k++) { total += (m1.m[i][k]*m2.m[k][j]); } result.m[i][j] = total; } return result; } How many multiplications of matrix elements are performed?
14
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication –Given the following matrices: A is 20 2 B is 2 30 C is 30 12 D is 12 8 –Specifically, how many multiplications does it take to compute A B C D ? First thing you should ask is “can it even be done”?
15
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication –Matrix multiplication is an associative operation meaning that the order in which we multiply doesn’t matter A(B(C D)) or (A B)(C D) or A((B C)D)) or ((A B)C)D or(A(B C))D –However, each of these has a different number of multiplications: A(B(CD))=(30 12 8)+(2 30 8)+(20 2 8)=3,680 (AB)(CD)=(20 2 30)+(30 12 8)+(20 30 8)=8,880 A((BC)D)=(2 30 12)+(2 12 8)+(20 2 8)=1,232 ((AB)C)D=(20 2 30)+(20 30 12)+(20 12 8)=10,320 (A(BC))D=(2 30 12)+(20 2 12)+(20 12 8)=3,120 –Obviously, there is an optimal solution A((BC)D)) = (2 30 12) + (2 12 8) + (20 2 8) = 1,232 How do we figure out that this one is the best?
16
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication –At the top level, given 4 matrices, there are 3 ways of parenthesizing this set into 2 subsets: (A 1 ) (A 2 A 3 A 4 ) or (A 1 A 2 ) (A 3 A 4 ) or (A 1 A 2 A 3 ) (A 4 ) –The best way parenthesizing for this set of 4 is given by: Best(firstSet) + Best(secondSet) + amount to multiply resulting 2 matrices –This is simply a recursive definition of the problem
17
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication –As an example: A 1 A 2 A 3 A 4 A 5 A 6 5 2 2 3 3 4 4 6 6 7 7 8 d 0 d 1 d 1 d 2 d 2 d 3 d 3 d 4 d 4 d 5 d 5 d 6 –There are 5 possible ways to parenthesize this expression, each one defined as: Best(1, k) + Best(k+1, 6) + d 0 d k d 6 for k [1, 5] –We need to take the min of these: Best(1, 6) = Min(Best(1, k) + Best(k+1, 6) + d 0 d k d 6 ) for k [1, 5]
18
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication –There was nothing in the previous work that forced the first matrix to be A 1 and the last to be A 6 –Thus, we can generalize this to be: Best(i, j) = Min(Best(i, k) + Best(k+1, j) + d i-1 d k d j ) for k [i, (j-1)] Best(i, i) = 0 // Base case
19
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication –We could develop a Divide-and-Conquer approach to solving this problem: public int best(int i, int j) { int result; if (i==j) { result = 0; } else { int min = Integer.MAX_VALUE; for (int k=i; k<j; k++) { int next = best(i,k) + best(k+1,j) + d[i]*d[k]*d[j]; min = Math.min(min,next); } result = min; } return result; }
20
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication –This approach will compute the correct answer, but it has tons of repeated work: Best(2, 5) takes the min of –Best(2,2) + Best(3,5) and –Best(2,3) + Best(4,5) and –Best(2,4) + Best(5,5) But then Best(2,4) needs: –Best(2,2) + Best(3,4) and –Best(2,3) + Best(4,4) You can see the repeated work (in red) and this is just the tip of the iceberg –Turns out that this is an exponential algorithm because of the repeated work
21
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication So we can try Dynamic Programming –We start with the base cases Best(i,i) = 0 for i [1, n] –Then we can use the recursive part to generate the rest of the Best values from the bottom up The question is what values need to be previously computed in order to solve for a particular Best(i,j) This is always the question in dynamic programming!
22
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication 0 0 0 0 0 0 123456 1 2 3 4 5 6 Each number in the 2D table represents the min # of mults from Ai to Aj We are trying to get a value for the entire thing A1 to A6 so we want a value in the upper right triangular matrix No values in the bottom left triangular matrix because they are not possible Start with filling in the base cases (when i==j) the diagonal
23
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication 123456 1 2 3 4 5 6 What can we fill in next? –Best(1,2) requires values for Best(1,1) and Best(2,2) We have those values –So Best(1,2) = Best(1,1) + Best(2,2) + d0*d1*d2 = 0 + 0 + (5*2*3) = 30 030 0 0 0 0 0 A1 A2 A3 A4 A5 A6 5 x 2 2 x 3 3 x 4 4 x 6 6 x 7 7 x 8 d0 d1 d1 d2 d2 d3 d3 d4 d4 d5 d5 d6 Best(i, j) = Min(Best(i, k) + Best(k+1, j) + d i-1 d k d j ) for k [i, (j-1)]
24
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication 123456 1 2 3 4 5 6 Similar for the other values along that diagonal 030 024 072 0168 0336 0 A1 A2 A3 A4 A5 A6 5 x 2 2 x 3 3 x 4 4 x 6 6 x 7 7 x 8 d0 d1 d1 d2 d2 d3 d3 d4 d4 d5 d5 d6 Best(i, j) = Min(Best(i, k) + Best(k+1, j) + d i-1 d k d j ) for k [i, (j-1)]
25
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication 123456 1 2 3 4 5 6 Now we have enough values to fill in the next diagonal –Best(1,3) is the min of 2 possible values: A 1 (A 2 A 3 ) (A 1 A 2 ) A 3 So for each value in the table, we need the values to its left and below it to be previously computed 03064 024 072 0168 0336 0 A1 A2 A3 A4 A5 A6 5 x 2 2 x 3 3 x 4 4 x 6 6 x 7 7 x 8 d0 d1 d1 d2 d2 d3 d3 d4 d4 d5 d5 d6 Best(i, j) = Min(Best(i, k) + Best(k+1, j) + d i-1 d k d j ) for k [i, (j-1)]
26
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication 123456 1 2 3 4 5 6 Now we have enough values to fill in the next diagonal –Best(1,3) is the min of 2 possible values: A 1 (A 2 A 3 ) (A 1 A 2 ) A 3 So for each value in the table, we need the values to its left and below it to be previously computed Filling in the next diagonal 03064 02472 0 198 0168392 0336 0 A1 A2 A3 A4 A5 A6 5 x 2 2 x 3 3 x 4 4 x 6 6 x 7 7 x 8 d0 d1 d1 d2 d2 d3 d3 d4 d4 d5 d5 d6 Best(i, j) = Min(Best(i, k) + Best(k+1, j) + d i-1 d k d j ) for k [i, (j-1)]
27
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication 123456 1 2 3 4 5 6 Add the other diagonals There there were 5 different possible ways to parenthesize (A 1 ) (A 2 A 3 A 4 A 5 A 6 ) (A 1 A 2 ) (A 3 A 4 A 5 A 6 ) (A 1 A 2 A 3 ) (A 4 A 5 A 6 ) (A 1 A 2 A 3 A 4 ) (A 5 A 6 ) (A 1 A 2 A 3 A 4 A 5 ) (A 6 ) 03064132226348 02472156268 072198366 0168392 0336 0 Best(i, j) = Min(Best(i, k) + Best(k+1, j) + d i-1 d k d j ) for k [i, (j-1)]
28
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication –The Best table we just built tells us that the optimal number of multiplications is 348 –But it doesn’t tell us the correct way of producing this optimal Much like the first table in Ncoins told us the optimal number of coins to use, but not which ones they were –We need a second table in order to determine the optimal factorization of the matrices We will store the “winner” at each stage –Much like the second table we needed in Ncoins
29
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication 123456 1 2 3 4 5 6 03064132226348 02472156268 072198366 0168392 0336 0 123456 1 2 3 4 5 6 11111 2345 345 45 5
30
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication A 1 A 2 A 3 A 4 A 5 A 6 (A 1 ) (A 2 A 3 A 4 A 5 A 6 ) (A 1 ) ((A 2 A 3 A 4 A 5 ) ( A 6 )) (A 1 ) (((A 2 A 3 A 4 )(A 5 )) ( A 6 )) (A 1 ) ((((A 2 A 3 )(A 4 ))(A 5 )) ( A 6 )) (A 1 ) (((((A 2 )(A 3 ))(A 4 ))(A 5 )) ( A 6 )) 11111 2345 345 45 5 123456 1 2 3 4 5 6
31
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Matrix Multiplication public int minMult(int n, int[] d) { for(int i = 1; i <= n; i++) { //base case: Middle Diagonal M[i][i] = 0;//M is optimal table } for(int dia = 1; dia < n; dia++) {//iterate through each diagonal for(int i = 1; i <= n-dia; i++) {//Fill in M & P int j = i + dia; int minM = inf; int p = -1; for(int k = i; k < j; k++) {//Find M[i][j] = min... int Mij = M[i][k] + M[k+1][j] + d[i-1]*d[k]*d[j]; if(Mij < minM) { minM = Mij; p = k; } M[i][j] = minM;//M is optimal table P[i][j] = p;//P tells you where to break } return M[1][n]; }
32
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Development of a Dynamic Programming Algorithm 1.Characterize the structure of a solution 2.Recursively define the value of a solution 3.Compute the value of a solution in a bottom-up fashion 4.Construct a solution from computed information
33
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance –Edit distance is a measure of how far a particular word is away from another word The number of character edits one needs to make the two words match –A single “character edit” consists of either: Insertion of a single character –sort sport (insertion of p) Deletion of a single character –sport sort (deletion of p) Changing a single character –computer commuter (change p to m) Can anyone think of an application for this idea?
34
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance –There are many sequences of edits that can change one word into another We want the optimal (minimal in this case) –And since we are going to accomplish this using Dynamic Programming the first thing we will need is a recursive definition of the problem Edist(str1, str2) –Here is the recursive definition: Edist(ε, ε) = 0 Edist(str, ε) = Edist(ε, str) = | str | Edist(str1+ch1, str2+ch2) = Min( Edist(str1, str2) + (0 if ch1==ch2, 1 otherwise), Edist(str1+ch1, str2) + 1, Edist(str1, str2+ch2) + 1 )
35
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance –In every recursive case, we recurse with at least one string shorter by 1 character Both are shorter in the change case That means it will keep calling itself until at least 1 string reaches the empty string at which time our base case kicks in However, implementing this with a recursive divide and conquer approach would lead to an exponential running time because of all the repeated work –So we try a Dynamic Programming approach instead Start by filling in a table with the base case information Fill in the rest of the table bottom up until you reach your goal solution –We will again have a 2D table The dimensions will be the length of the first string +1 by the length of the second string +1 –The +1s are there because we need a spot in the table for ε
36
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance 0123 1 2 3 4 εCAT ε C A K E First fill in the base cases Our goal is to find the edit distance for the entire string “cake” to the entire string “cat” –So the number we want to find is in the lower right corner
37
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance 0123 1x 2 3 4 εCAT ε C A K E The 3 recursive cases are: –Edist(i, j) uses Edist(i-1, j-1) Edist(i, j-1) Edist(i-1, j) –Where i represents a substring of “cake” from character 1 up to character i (1 indexed) –And j is similar, but for “cat” So this tells us that for each cell, we need the values from the cells to the upper left, to the left, and above Edist(ε, ε) = 0 Edist(str, ε) = Edist(ε, str) = | str | Edist(str1+ch1, str2+ch2) = Min( Edist(str1, str2) + (0 if ch1==ch2, 1 otherwise), Edist(str1+ch1, str2) + 1, Edist(str1, str2+ch2) + 1 )
38
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance 0123 10 2 3 4 εCAT ε C A K E Fill in the table row by row Edist(ε, ε) = 0 Edist(str, ε) = Edist(ε, str) = | str | Edist(str1+ch1, str2+ch2) = Min( Edist(str1, str2) + (0 if ch1==ch2, 1 otherwise), Edist(str1+ch1, str2) + 1, Edist(str1, str2+ch2) + 1 )
39
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance 0123 1012 2101 3211 4322 εCAT ε C A K E Fill in the table row by row Edist(ε, ε) = 0 Edist(str, ε) = Edist(ε, str) = | str | Edist(str1+ch1, str2+ch2) = Min( Edist(str1, str2) + (0 if ch1==ch2, 1 otherwise), Edist(str1+ch1, str2) + 1, Edist(str1, str2+ch2) + 1 )
40
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance 0123 1012 2101 3211 4322 εCAT ε C A K E Fill in the table row by row Edist(ε, ε) = 0 Edist(str, ε) = Edist(ε, str) = | str | Edist(str1+ch1, str2+ch2) = Min( Edist(str1, str2) + (0 if ch1==ch2, 1 otherwise), Edist(str1+ch1, str2) + 1, Edist(str1, str2+ch2) + 1 )
41
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance 0123 1012 2101 3211 4322 εCAT ε C A K E Fill in the table row by row Edist(ε, ε) = 0 Edist(str, ε) = Edist(ε, str) = | str | Edist(str1+ch1, str2+ch2) = Min( Edist(str1, str2) + (0 if ch1==ch2, 1 otherwise), Edist(str1+ch1, str2) + 1, Edist(str1, str2+ch2) + 1 )
42
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance 0123 1012 2101 3211 4322 εCAT ε C A K E Fill in the table row by row Edist(ε, ε) = 0 Edist(str, ε) = Edist(ε, str) = | str | Edist(str1+ch1, str2+ch2) = Min( Edist(str1, str2) + (0 if ch1==ch2, 1 otherwise), Edist(str1+ch1, str2) + 1, Edist(str1, str2+ch2) + 1 )
43
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance 0123 1012 2101 3211 4322 εCAT ε C A K E Fill in the table row by row Edist(ε, ε) = 0 Edist(str, ε) = Edist(ε, str) = | str | Edist(str1+ch1, str2+ch2) = Min( Edist(str1, str2) + (0 if ch1==ch2, 1 otherwise), Edist(str1+ch1, str2) + 1, Edist(str1, str2+ch2) + 1 )
44
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance 0123 1012 2101 3211 4322 εCAT ε C A K E Until we end up with the final table
45
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance –Still need to find actual sequence of edits that results in the minimum cost –We can do this without creating a new table –Trace back from optimal value to find where it must have originated 0123 1012 2101 3211 4322 εCAT ε C A K E Edit CAKE to CAT
46
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance –Still need to find actual sequence of edits that results in the minimum cost –We can do this without creating a new table –Trace back from optimal value to find where it must have originated 0123 1012 2101 3211 4322 εCAT ε C A K E Edit CAKE to CAT - Change E to T
47
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance –Still need to find actual sequence of edits that results in the minimum cost –We can do this without creating a new table –Trace back from optimal value to find where it must have originated 0123 1012 2101 3211 4322 εCAT ε C A K E Edit CAKE to CAT - Change E to T - Delete K
48
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance –Still need to find actual sequence of edits that results in the minimum cost –We can do this without creating a new table –Trace back from optimal value to find where it must have originated 0123 1012 2101 3211 4322 εCAT ε C A K E Edit CAKE to CAT - Change E to T - Delete K - A Remains
49
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance –Still need to find actual sequence of edits that results in the minimum cost –We can do this without creating a new table –Trace back from optimal value to find where it must have originated 0123 1012 2101 3211 4322 εCAT ε C A K E Edit CAKE to CAT - Change E to T - Delete K - A Remains - C Remains
50
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance –Pseudocode: Your Turn! –Recursive Definition Edist(ε, ε) = 0 Edist(str, ε) = Edist(ε, str) = | str | Edist(str1+ch1, str2+ch2) = Min( Edist(str1, str2) + (0 if ch1==ch2, 1 otherwise), Edist(str1+ch1, str2) + 1, Edist(str1, str2+ch2) + 1 ) –Table s1s1 …snsn t1t1 : tmtm
51
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Edit Distance - Pseudocode public int editDist(char[] s1, char[] s2) { for(int i = 0; i < s1.size()+1; i++) { //base case: First row of table Edist[i][0] = i; //Edist is optimal table } for(int j = 1; j < s2.size()+1; j++) { //base case: First col of table Edist[0][j] = j; } for(int i = 1; i < s1.size()+1; i++) { //iterate through each row for(int j = 1; j < s2.size()+1; j++) { //iterate through each col int val1 = Edist[i-1][j-1]; if(s1[i-1] != s2[j-1]) val1++; int val2 = Edist[i-1][j]+1; int val3 = Edist[i][j-1]+1; int min = Math.min(val1, Math.min(val2, val3)); Edist[i][j] = min; } Return Edist[s1.size()][s2.size()]; }
52
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Shortest Path –Find the shortest path from each vertex to all other vertices Travel Applications: air travel, shipping, driving directions Other Applications: networking, telecommunications, six degrees of separation –Graph Theory Basics VertexPath Edge▪ Simple Weighted▪Length Directed Complete Cycle / Acyclic 1 2 5 3 4
53
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Shortest Path –Shortest path is simple path with lowest cost –Paths from v 2 to v 4 : v 2,v 3, v 4 : 1 + 3 = 4 v 2,v 5, v 4 : 1 + 2 = 3 v 2,v 3, v 5, v 4 : 1 + 2 + 2 = 5 1 2 5 3 4 3 3 3 9 2 2 5 1 1 4 Shortest Path
54
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Shortest Path –Brute Force Solution Determine for each vertex the lengths of all paths from that vertex to each other vertex, and compute the minimum –Recursive Definition
55
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Shortest Path –Dynamic Programming Solution Need 3-d table to keep track of bottom-up data Stack several 2-d tables –D (k) [i][j] = length of shortest path from v i to v j using only vertices in the set {v 1, v 2, …, v k } –D (0) [i][j]= 0 if i = j = weight on edgeif edge (v i, v j ) exists = if edge (v i, v j ) does not exists
56
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 D (0) [i][j]= 0 if i = j = weight on edgeif edge (v i, v j ) exists = if edge (v i, v j ) does not exists
57
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 D (k) [i][j] = length of shortest path from v i to v j using only vertices in the set {v 1, v 2, …, v k } = minimum(D (k-1) [i][j], D (k-1) [i][k] + D (k-1) [k][j]) D 1 [5][2] = D 0 [5][1] + D 0 [1][2]
58
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 D (k) [i][j] = length of shortest path from v i to v j using only vertices in the set {v 1, v 2, …, v k } = minimum(D (k-1) [i][j], D (k-1) [i][k] + D (k-1) [k][j])
59
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 D (k) [i][j] = length of shortest path from v i to v j using only vertices in the set {v 1, v 2, …, v k } = minimum(D (k-1) [i][j], D (k-1) [i][k] + D (k-1) [k][j])
60
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 D (k) [i][j] = length of shortest path from v i to v j using only vertices in the set {v 1, v 2, …, v k } = minimum(D (k-1) [i][j], D (k-1) [i][k] + D (k-1) [k][j])
61
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 D (k) [i][j] = length of shortest path from v i to v j using only vertices in the set {v 1, v 2, …, v k } = minimum(D (k-1) [i][j], D (k-1) [i][k] + D (k-1) [k][j])
62
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Shortest Path –Once again, we have the cost of the shortest path but not the actual paths Sometimes this cost is sufficient –Need to build second table as we’re constructing first to determine path P[i][j] = highest index of an intermediate vertex on the shortest path from v i to v j, if at least one intermediate vertex exists. P[i][j] = 0, if no intermediate vertex exists
63
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 P[i][j] = highest index of an intermediate vertex on the shortest path from v i to v j, if at least one intermediate vertex exists. P[i][j] = 0, if no intermediate vertex exists
64
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 P[i][j] = highest index of an intermediate vertex on the shortest path from v i to v j, if at least one intermediate vertex exists. P[i][j] = 0, if no intermediate vertex exists
65
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 P[i][j] = highest index of an intermediate vertex on the shortest path from v i to v j, if at least one intermediate vertex exists. P[i][j] = 0, if no intermediate vertex exists
66
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 P[i][j] = highest index of an intermediate vertex on the shortest path from v i to v j, if at least one intermediate vertex exists. P[i][j] = 0, if no intermediate vertex exists
67
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 P[i][j] = highest index of an intermediate vertex on the shortest path from v i to v j, if at least one intermediate vertex exists. P[i][j] = 0, if no intermediate vertex exists
68
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Shortest Path 2 5 3 4 3 3 3 9 2 2 5 1 1 4 1 P[i][j] = highest index of an intermediate vertex on the shortest path from v i to v j, if at least one intermediate vertex exists. P[i][j] = 0, if no intermediate vertex exists
69
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Principle of Optimality –Although, at this point, it may seem that any optimization problem can be solved using Dynamic Programming – it is not the case The principle of optimality must apply in a given problem –The principle of optimality is said to apply in a problem if an optimal solution to an instance of a problem always contains optimal solutions to all sub- instances
70
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Principle of Optimality –For example: Shortest Path Problem If the optimal path from v i v j includes v k, then the sub- paths from v i v k and v k v j must also be optimal These were the sub-instances we used to form the solution for the larger instance –The Principle of Optimality is sometimes difficult to prove, but one must prove it in order to prove that a Dynamic Programming solution will work Finding a counter-example is often easier – to prove that a Dynamic Programming solution will NOT work
71
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Principle of Optimality –Consider the Longest Path Problem Restrict to simple paths –No cycles –The longest path from v 1 to v 4 is v 1 v 3 v 2 v 4 –However, the sub-path v 1 v 3 is not the optimal (longest) from v 1 to v 3 v 1 v 3 = 1, but v 1 v 2 v 3 = 4 –Thus, the principle of optimality doesn’t hold for the longest path problem 1 23 4
72
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Practice Problems –Foundations of Algorithms Chapter 3: 5, 6, 7, 9, 10, 11, 12, 14, 33, 34
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.