Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | caldwell-campbell |
View: | 36 times |
Download: | 0 times |
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Dynamic Programming
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
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-upfor each node n of tree t
value = for each child c of n
value += table[c]table[n] = value + process(n);
return table(root)
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
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
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
:
0 if 0
min 1 if 0ii d p i
pC p
C p d p
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¢
1¢
2¢
3¢
4¢
5¢
6¢
7¢
0
1
2
3
4
1
2
3
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
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¢
1¢
2¢
3¢
4¢
5¢
6¢
7¢
0
1
1
1
1
5
1 or 5
1 or 5
0
1
2
3
4
1
2
3
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication
1x2 + 2x5 + 3x8 = 2 + 10 + 24 = 36
1 2 3
4 5 6
1 2 3
4 5 6
7 8 9
30 36 42
66 81 96
• rows from the first matrix• columns from the second matrix
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 23 matrix and B be 32 matrix
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplicationpublic 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 matrixelements are performed?
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”?
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?
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: (A1) (A2 A3 A4 ) or (A1 A2 ) (A3 A4) or (A1 A2 A3 ) (A4)
– 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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication– As an example:
A1 A2 A3 A4 A5 A6
5 2 2 3 3 4 4 6 6 7 7 8d0 d1 d1 d2 d2 d3 d3 d4 d4 d5 d5 d6
– There are 5 possible ways to parenthesize this expression, each one defined as:• Best(1, k) + Best(k+1, 6) + d0dkd6 for k [1, 5]
– We need to take the min of these:• Best(1, 6) = Min(Best(1, k) + Best(k+1, 6) + d0dkd6 )
for k [1, 5]
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 A1 and the last to be A6
– Thus, we can generalize this to be:• Best(i, j) = Min(Best(i, k) + Best(k+1, j) + di-1dkdj )
for k [i, (j-1)]• Best(i, i) = 0 // Base case
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;}
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
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 dynamicprogramming!
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication
0
0
0
0
0
0
1 2 3 4 5 6
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication
1 2 3 4 5 6
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
0 30
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) + di-1dkdj ) for k [i, (j-1)]
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication
1 2 3 4 5 6
1
2
3
4
5
6
• Similar for the other values along that diagonal
0 30
0 24
0 72
0 168
0 336
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) + di-1dkdj ) for k [i, (j-1)]
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication
1 2 3 4 5 6
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:A1 (A2 A3)(A1 A2) A3
• So for each value in the table, we need the values to its left and below it to be previously computed
0 30 64
0 24
0 72
0 168
0 336
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) + di-1dkdj ) for k [i, (j-1)]
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication
1 2 3 4 5 6
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:A1 (A2 A3)(A1 A2) A3
• 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
0 30 64
0 24 72
0 72 198
0 168 392
0 336
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) + di-1dkdj )
for k [i, (j-1)]
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication
1 2 3 4 5 6
1
2
3
4
5
6
• Add the other diagonals• There there were 5 different
possible ways to parenthesize
(A1) (A2 A3 A4 A5 A6)
(A1 A2) (A3 A4 A5 A6)
(A1 A2 A3) (A4 A5 A6)
(A1 A2 A3 A4) (A5 A6)
(A1 A2 A3 A4 A5) (A6)
0 30 64 132 226 348
0 24 72 156 268
0 72 198 366
0 168 392
0 336
0
Best(i, j) = Min(Best(i, k) + Best(k+1, j) + di-1dkdj ) for k [i, (j-1)]
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication
1 2 3 4 5 6
1
2
3
4
5
6
0 30 64 132 226 348
0 24 72 156 268
0 72 198 366
0 168 392
0 336
0
1 2 3 4 5 6
123456
1 1 1 1 1
2 3 4 5
3 4 5
4 5
5
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplication
A1 A2 A3 A4 A5 A6
(A1) (A2 A3 A4 A5 A6)
(A1) ((A2 A3 A4 A5) ( A6))
(A1) (((A2 A3 A4)(A5)) ( A6))
(A1) ((((A2 A3)(A4))(A5)) ( A6))
(A1) (((((A2)(A3))(A4))(A5)) ( A6))
1 1 1 1 1
2 3 4 5
3 4 5
4 5
5
1 2 3 4 5 6
1
2
3
4
5
6
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Matrix Multiplicationpublic int minMult(int n, int[] d) {
for(int i = 1; i <= n; i++) { //base case: Middle DiagonalM[i][i] = 0; //M is optimal table
}for(int dia = 1; dia < n; dia++) { //iterate through each
diagonalfor(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 tableP[i][j] = p; //P tells you where
to break}
}return M[1][n];
}
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
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?
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 )
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 ε
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance
0 1 2 31234
ε C A T
ε
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance
0 1 2 31 x234
ε C A T
ε
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(ε, ε) = 0Edist(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 )
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance
0 1 2 31 0234
ε C A T
ε
C
A
K
E
• Fill in the table row by row
Edist(ε, ε) = 0Edist(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 )
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
• Fill in the table row by row
Edist(ε, ε) = 0Edist(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 )
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
• Fill in the table row by row
Edist(ε, ε) = 0Edist(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 )
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
• Fill in the table row by row
Edist(ε, ε) = 0Edist(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 )
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
• Fill in the table row by row
Edist(ε, ε) = 0Edist(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 )
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
• Fill in the table row by row
Edist(ε, ε) = 0Edist(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 )
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
• Until we end up with the final table
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
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
Edit CAKE to CAT
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
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
Edit CAKE to CAT- Change E to T
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
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
Edit CAKE to CAT- Change E to T- Delete K
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
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
Edit CAKE to CAT- Change E to T- Delete K- A Remains
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
0 1 2 31 0 1 22 1 0 13 2 1 14 3 2 2
ε C A T
ε
C
A
K
E
Edit CAKE to CAT- Change E to T- Delete K- A Remains- C Remains
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 s1 … sn
t1
:
tm
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Edit Distance - Pseudocodepublic 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
tableEdist[0][j] = j;
}for(int i = 1; i < s1.size()+1; i++) { //iterate through each
rowfor(int j = 1; j < s2.size()+1; j++) { //iterate through
each colint 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()];
}
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• Vertex • Path• Edge ▪ Simple
Weighted ▪ Length Directed • Complete
• Cycle / Acyclic 1
2
5
3
4
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 v2 to v4:
• v2,v3, v4: 1 + 3 = 4
• v2,v5, v4: 1 + 2 = 3
• v2,v3, v5, v4 : 1 + 2 + 2 = 5
1
2
5
3
4
3
3
3
9
2
25
1
1
4
Shortest Path
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
all vertices
0 if SP ,
min SP , SP , otherwisei jk i k k j
i jv v
v v v v
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 vi to vj using only vertices in the set {v1 , v2, … , vk}
– D(0)[i][j] = 0 if i = j
= weight on edge if edge (vi, vj) exists
= if edge (vi, vj) does not exists
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
10
0 5
3 0 1 1
9 0 3 2
0 4
3 2 0
D
D(0)[i][j] = 0 if i = j= weight on edge if edge (vi, vj) exists= if edge (vi, vj) does not exists
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
11
0 5
3 0 1 1
9 0 3 2
0 4
3 2 08
D
D(k)[i][j] = length of shortest path from vi to vj using only vertices in the set {v1 , v2, … , vk}
= minimum(D(k-1)[i][j], D(k-1)[i][k] + D(k-1)[k][j])
D1[5][2] = D0[5][1] + D0[1][2]
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
12
0 5
3 0 1 1
9 0 3 2
0 4
3 8 2 0
6 6
12
9
D
D(k)[i][j] = length of shortest path from vi to vj using only vertices in the set {v1 , v2, … , vk}
= minimum(D(k-1)[i][j], D(k-1)[i][k] + D(k-1)[k][j])
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
13
0 5 6 6
3 0 1 1
12 9 0 3 2
0 4
3 8 9 2 0
9
4
D
D(k)[i][j] = length of shortest path from vi to vj using only vertices in the set {v1 , v2, … , vk}
= minimum(D(k-1)[i][j], D(k-1)[i][k] + D(k-1)[k][j])
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
14
0 5 6 9 6
3 0 1 4 1
12 9 0 3 2
0 4
3 8 9 2 0
D
D(k)[i][j] = length of shortest path from vi to vj using only vertices in the set {v1 , v2, … , vk}
= minimum(D(k-1)[i][j], D(k-1)[i][k] + D(k-1)[k][j])
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
15
0 5 6 6
3 0 1 1
9 0 3 2
0
8
3
5
7 12 1
0
3 4
3 8 9 2
D
D(k)[i][j] = length of shortest path from vi to vj using only vertices in the set {v1 , v2, … , vk}
= minimum(D(k-1)[i][j], D(k-1)[i][k] + D(k-1)[k][j])
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 vi to vj , if at least one intermediate vertex
exists.
P[i][j] = 0, if no intermediate vertex exists
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
10
0 5
3 0 1 1
9 0 3 2
0 4
3 2 0
D
P[i][j] = highest index of an intermediate vertex on the shortestpath from vi to vj , if at least one intermediate vertexexists.
P[i][j] = 0, if no intermediate vertex exists
0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
P
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
11
0 5
3 0 1 1
9 0 3 2
0 4
3 2 08
D
P[i][j] = highest index of an intermediate vertex on the shortestpath from vi to vj , if at least one intermediate vertexexists.
P[i][j] = 0, if no intermediate vertex exists
1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
P
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
12
0 5
3 0 1 1
9 0 3 2
0 4
3 8 2 0
6 6
12
9
D
P[i][j] = highest index of an intermediate vertex on the shortestpath from vi to vj , if at least one intermediate vertexexists.
P[i][j] = 0, if no intermediate vertex exists
2
0 0 2 0 2
0 0 0 0 0
2 0 0 0 0
0 0 0 0 0
0 1 2 0 0
P
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
13
0 5 6 6
3 0 1 1
12 9 0 3 2
0 4
3 8 9 2 0
9
4
D
P[i][j] = highest index of an intermediate vertex on the shortestpath from vi to vj , if at least one intermediate vertexexists.
P[i][j] = 0, if no intermediate vertex exists
3
0 0 2 3 2
0 0 0 3 0
2 0 0 0 0
0 0 0 0 0
0 1 2 0 0
P
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
14
0 5 6 9 6
3 0 1 4 1
12 9 0 3 2
0 4
3 8 9 2 0
D
P[i][j] = highest index of an intermediate vertex on the shortestpath from vi to vj , if at least one intermediate vertexexists.
P[i][j] = 0, if no intermediate vertex exists
4
0 0 2 3 2
0 0 0 3 0
2 0 0 0 0
0 0 0 0 0
0 1 2 0 0
P
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example Shortest Path
2
5
3
4
3
3
3
9
2
25
1
1
4
15
0 5 6 6
3 0 1 1
9 0 3 2
0
8
3
5
7 12 1
0
3 4
3 8 9 2
D
P[i][j] = highest index of an intermediate vertex on the shortestpath from vi to vj , if at least one intermediate vertexexists.
P[i][j] = 0, if no intermediate vertex exists
5
0 0 2 5 2
0 0 0 5 0
5 0 0 0 0
5 5 5 0 0
0 1 2 0 0
P
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
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 vi vj includes vk, then the sub-paths from vi vk and vk vj 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
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 v1 to v4 is v1v3v2v4
– However, the sub-path v1v3 is not the optimal (longest) from v1 to v3
• v1v3 = 1, but v1v2v3 = 4
– Thus, the principle of optimality doesn’t hold for the longest path problem
1
2 3
4
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