Lecture 5
Lecture 5
PROGRAMMING
Principles
Identify small number of sub-problems
solve them
Assumption: solution to subproblem is sufficient to compute solution to larger
sub-problem
Can now solve larger sub-problems (given the solutions of smaller sub-
problems)
This leads to a table filling algorithm, where each entry in table corresponds
to the optimal solution to the sub-problem
• Key is to get/identify the sub-problems right
Steps in Dynamic Programming
1. Characterize structure of an optimal solution.
2. Define value of optimal solution recursively.
3. Compute optimal solution values either top-down with
caching or bottom-up in a table.
4. Construct an optimal solution from computed values.
Dynamic Programming..
• DP reduces computation by
• Solving subproblems in a bottom-up fashion.
• Storing solution to a subproblem the first time it is
solved.
• Look-up the solution when subproblem is encountered
again.
• Key: determine structure of optimal solutions
Finding Cost to convert a substring to another
Levenshtein distance
Operations (cost)
Insert m[i, j-1] + 1
Delete m[i-1, j] + 1
7
Levenshtein distance: Algorithm
8
Levenshtein distance: Algorithm
9
Levenshtein distance: Algorithm
10
Levenshtein distance: Algorithm
11
Creating change using Dynamic Programming
Suppose we have denominations Re1, Rs 4, Rs 5.
We want to pay Rs 8
Pay using greedy approach which picks up the maximum feasible
denomination at every iteration.
j= 0 j=1 2 3 4 5 6 7 8
i=0 0 0 0 0 0 0 0 0 0
i=1 d1 0 1 2 3 4 5 6 7 8
i=2 d4 0 1 2 3 1 2 3 4 2
i=3 d6 0 1 2 3 1 2 1 2 2
0/1 Knapsack Problem: Formal description
i=0 0 0 0 0 0 0 0 0
i=1 w1 = 2 v1 = 4 0
i=2 w2 = 3 v2 = 5 0
i=3 w3 = 4 v3 = 6 0
Activity
Solve the problems given to you and arrive at the optimal
solution
e.g. For three items: W= {2,3,4}, V={4,5,6}
M (knapsack capacity)= 7
wi vi j= 0 j=1 2 3 4 5 6 7
i=0 0 0 0 0 0 0 0 0
i=1 w1 = 2 v1 = 4 0
i=2 w2 = 3 v2 = 5 0
i=3 w3 = 4 v3 = 6 0
Activity
Solve the problems given to you and arrive at the optimal
solution
e.g. For three items: W= {2,3,4}, V={4,5,6}
M (knapsack capacity)= 7
wi vi j= 0 j=1 2 3 4 5 6 7
i=0 0 0 0 0 0 0 0 0
i=1 w1 = 2 v1 = 4 0
i=2 w2 = 3 v2 = 5 0
i=3 w3 = 4 v3 = 6 0
i=0 0 0 0 0 0 0 0 0
i=1 w1 = 2 v1 = 4 0 0
i=2 w2 = 3 v2 = 5 0 0
i=3 w3 = 4 v3 = 6 0 0
i=0 0 0 0 0 0 0 0 0
i=1 w1 = 2 v1 = 4 0 0
i=2 w2 = 3 v2 = 5 0 0
i=3 w3 = 4 v3 = 6 0 0
Construct a matrix L
L = Matrix, which gives the length of each edge
L[i, j] = w (i, j), if i ≠ j and (i, j) ∈ E // w(i, j) is the weight of the edge (i, j)
Example
Graph G(V,E)
2 Construct L
1
2
3 7
1
3 4
6
Example
Optimal Solution: Updating the matrix
While considering kth vertex as intermediate vertex, there are two possibilities :
If k is not part of shortest path from i to j, we keep the distance D[i, j] as it is.
2 Construct L
1
2
3 7
1
3 4
Solution:
Optimal substructure formula for Floyd’s algorithm,
Iteration 1 : k = 1 :
D1[1, 2] = min { Do [1, 2], Do [1, 1] + Do [1, 2] }
= min {∞, 0 + ∞}
= ∞
Note : Path distance for highlighted cell is improvement over original matrix.
Iteration 2 (k = 2) :
D3[1, 3] =
min { D2 [1, 3], D2 [1, 3] + D2 [3, 3] }
= min {3, 3 + 0}
= 3
D3[1, 4] =
min { D2 [1, 4], D2 [1, 3] + D2 [3, 4] }
= min {∞, 3 + 1}
=4
Iteration 3 (k = 3) :
D3[2, 1]
= min { D2 [2, 1], D2 [2, 3] + D2 [3, 1] }
= min {2, 5 + 9}
=2
D3[2, 3]
= min { D2 [2, 3], D2 [2, 3] + D2 [3, 3 }
= min {5, 5 + 0}
=5
D3[3, 2]
= min { D2 [3, 2], D2 [3, 3] + D2 [3, 2] }
= min {7, 0 + 7}
=7
D3[3, 4] = D2 [3, 4] = 1
= min { D2 [3, 4], D2 [3, 3] + D2 [3, 4] }
= min {1, 0 + 1}
=1
Iteration 3 (k = 3) :
D3[4, 1]
= min { D2 [4, 1], D2 [4, 3] + D2 [3, 1] }
= min {6, 9 + 9}
=6
D3[4, 2]
= min { D2 [4, 2], D2 [4, 3] + D2 [3, 2] }
= min {∞, 9 + 7}
= 16
D3[4, 3]
= min { D2 [4, 3], D2 [4, 3] + D2 [3, 3] }
= min {9, 9 + 0}
= 9
Update Matrix
Iteration 4 (k = 4) :
D4[1, 2]
= min { D3 [1, 2], D3 [1, 4] + D3 [4, 2] }
= min {10, 4 + 16}
= 10
D4[1, 3]
= min { D3 [1, 3], D3 [1, 4] + D3 [4, 3] }
= min {3, 4 + 9}
=3
D4[1, 4] =
= min { D3 [1, 4], D3 [1, 4] + D3 [4, 4] }
= min {4, 4 + 0}
=4
Iteration 4 (k = 4) :
D4[2, 1]
= min { D3 [2, 1], D3 [2, 4] + D3 [4, 1] }
= min {2, 6 + 6}
=2
D4[2, 3]
= min { D3 [2, 3], D3 [2, 4] + D3 [4, 3] }
= min {5, 6 + 9}
=5
D4[2, 4]
= min { D3 [2, 4], D3 [2, 4] + D3 [4, 4] }
= min {6, 6 + 0}
= 6
Iteration 4 (k = 4) :
D4[3, 1]
= min { D3 [3, 1], D3 [3, 4] + D3 [4, 1] }
= min {9, 1 + 6}
=7
D4[3, 2]
= min { D3 [3, 2], D3 [3, 4] + D3 [4, 2] }
= min {7, 1 + 16}
=7
D4[3, 4]
= min { D3 [3, 4], D3 [3, 4] + D3 [4, 4] }
= min {1, 1 + 0}
= 1
Iteration 4 (k = 4) :
D4[4, 1]
= min { D3 [4, 1], D3 [4, 4] + D3 [4, 1] }
= min {6, 0 + 6}
=6
D4[4, 2]
= min { D3 [4, 2], D3 [4, 4] + D3 [4, 2] }
= min {16, 0+ 16}
= 16
D4[4, 3]
= min { D3 [4, 3], D3 [4, 4] + D3 [4, 3] }
= min {9, 0+ 9}
= 9
Final distance matrix
is,
1 2 3
1
4
2
4
Complexity
• For G(V,E) having n vertices, Complexity is n3