0% found this document useful (0 votes)
4 views

Lecture 7 Dynamic Programming

Dynamic programming

Uploaded by

sknasimuddin179
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 7 Dynamic Programming

Dynamic programming

Uploaded by

sknasimuddin179
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Design and Analysis of

Computer Algorithm

Lecture 7 Dynamic
Programming
Dynamic Programming

⚫ An algorithm design method that can be used when


the solution to a problem may be viewed as the result
of a sequence of decision.
⚫ Dynamic Programming algorithm store results, or
solutions, for small subproblems and looks them up,
rather than recomputing them, when it needs later to
solve larger subproblems
⚫ Typically applied to optimiation problems

204512 Design and Analysis of Computer Algorithm November 8, 2024 2


Topics Cover

⚫ Matrix-Chain Multiplication
⚫ Longest Common Subsequence
⚫ Optimal Binary Search Tree

204512 Design and Analysis of Computer Algorithm November 8, 2024 3


Principle of Optimalilty

⚫ An optimal sequence of decisions has the property


that whatever the initial state a decision is in, the
remaining decisions must constitute an optimal
decision sequence with regard to the state resulting
from the first decision.
⚫ Essentially, this principles states that the optimal
solution for a larger subproblem contains an optimal
solution for a smaller subproblem.

204512 Design and Analysis of Computer Algorithm November 8, 2024 4


Dynamic Programming VS. Greedy
Method

⚫ Greedy Method
– only one decision sequence is ever genertated.
⚫ Dynamic Programming
– Many decision sequences may be genertated.

204512 Design and Analysis of Computer Algorithm November 8, 2024 5


Dynamic Programming VS. Divide-
and Conquer

⚫ Divide-and-Conquer
– partition the problem into independent
subproblems, solve the subproblems recursively,
and then combine their solutions to solve the
original problem
⚫ Dynamic Programming
– applicable when the subproblems are not
independent, that is, when subproblems share
subsubproblems.

204512 Design and Analysis of Computer Algorithm November 8, 2024 6


4-Steps of Developing Dynamic
Programming Algorithm

⚫ Characterize the structure of an optimal solution


⚫ Recursively define the value of an optimal solution
⚫ Compute the value of an optimal solution in a bottom-
up fashion
⚫ Construct an optimal solution from computed
information

204512 Design and Analysis of Computer Algorithm November 8, 2024 7


Dynamic programming

⚫ It is used, when the solution can be recursively


described in terms of solutions to subproblems
(optimal substructure)
⚫ Algorithm finds solutions to subproblems and stores
them in memory for later use
⚫ More efficient than “brute-force methods”, which
solve the same subproblems over and over again

204512 Design and Analysis of Computer Algorithm November 8, 2024 8


Example : C(n,k)


C(n-1,k-1) + C(n-1,k) if o<k<n
C(n,k) = 1 if k =0 or k = n
0 otherwise

C(n,k)
{
if k = 0 or k =n then return 1
if k < 0 or k >n then return 0
return( C(n-1,k-1) + C(n-1,k) )
}

204512 Design and Analysis of Computer Algorithm November 8, 2024 9


Top-Down Recursive


C(n-1,k-1) + C(n-1,k) if o<k<n
C(n,k) = 1 if k =0 or k = n
0 0 1 2 3
otherwise
0
1
2
C(6,3)
3
4
5
6

204512 Design and Analysis of Computer Algorithm November 8, 2024 10


Top-Down Recursive : Tree
Structure

C(6,3) Repeat

C(5,2) C(5,3)

C(4,1) C(4,2) C(4,2) C(4,3)

C(3,0) C(3,1)

C(2,0) C(2,1)

C(1,0) C(1,1)

204512 Design and Analysis of Computer Algorithm November 8, 2024 11


Top-Down Recursive +
Memorization


C(n-1,k-1) + C(n-1,k) if o<k<n
C(n,k) = 1 if k =0 or k = n
0 0 1 2 3
otherwise
0
1 1 1

2 1 2 1
C(6,3) 1 3 3 1
3
4 4 6 4

5 10 10

6 20

204512 Design and Analysis of Computer Algorithm November 8, 2024 12


Top-Down + Memorization : Tree
Structure

C(6,3)

C(5,2) C(5,3)

C(4,1) C(4,2) C(4,2) C(4,3)

C(3,0) C(3,1)

C(2,0) C(2,1)
No need to recalculate

C(1,0) C(1,1)
Memory every value

204512 Design and Analysis of Computer Algorithm November 8, 2024 13


Pseudo Code of Top-Down +
Memorization

Lookup_C( n, k )
{
if k = 0 or k = n then return 1
if k < 0 or k > n then return 0
if c[n,k] < 0 then
c[n,k] = Lookup_C(n-1,k-1) + Lookup_C(n-1,k) )
return c[n,k]
}

204512 Design and Analysis of Computer Algorithm November 8, 2024 14


Bottom-Up Dynamic Programming


C(n-1,k-1) + C(n-1,k) if o<k<n
C(n,k) = 1 if k =0 or k = n
0 0 1 2 3
otherwise
0 1

1 1 1

2 1 2 1
C(6,3) 1 3 3 1
3
4 1 4 6 4

5 1 5 10 10

6 1 6 15 20

204512 Design and Analysis of Computer Algorithm November 8, 2024 15


Bottom-Up Dynamic Programming

7 8
C(3,1) C(3,2)
4 5 6
C(2,0) C(2,1) C(2,2)
2 3
C(1,0) C(1,1)
1
C(0,0)

204512 Design and Analysis of Computer Algorithm November 8, 2024 16


Top Down VS Bottom Up

⚫ Bottom-up dynamic programming


– all subproblems must be solved
– regular pattern of table access can be exploited to
reduce time or space
⚫ Top-down + memoization
– solve only subproblems that are definitely required
– recursion overhead

204512 Design and Analysis of Computer Algorithm November 8, 2024 17


Dynamic Programming

⚫ Generally used for solving optimization problem


⚫ Two key ingredients
– optimal substructure (principle of optimality)
– overlapping subproblems

204512 Design and Analysis of Computer Algorithm November 8, 2024 18


Overlapping Subproblems

⚫ Space of subproblems must be “small” (polynomial in


the input size)
⚫ Recursive algorithm solves the same subproblems
over and over.
⚫ Dynamic prog. solves all subproblems but solves
each once and then stores the solution in some data
structure.

204512 Design and Analysis of Computer Algorithm November 8, 2024 19


Matrix-Chain Multiplication

Input: Given Matrices A1, A2,…, An


where Ai has dimensions d i-1 x di
Goal: Determine the order of multiplication to minimize
the number of scalar multiplication.
⚫ Assumption: The multiplication of a p x q matrix by a
q x r matrix requires pqr scalar multiplications.

204512 Design and Analysis of Computer Algorithm November 8, 2024 20


Longest Common Subsequence
Subsequence

⚫ X = < s, o, m, c, h, a, i >
subsequences of X
– < s, o, m, c, h, a, i > → < s, o, m >
– < s, o, m, c, h, a, i > → <c, h, a, i>
– < s, o, m, c, h, a, i > → <s, o, h, a, i>

204512 Design and Analysis of Computer Algorithm November 8, 2024 22


Longest Common Subsequence

Instance: Two sequences X and Y


Question: What is a longest common subsequence of X
and Y
Example
If X = <A, B, C, B, D, A, B>
and
Y = <B, D, C, A, B, A>
then a longest common subsequence is either
<B, C, B, A>
or
<B, D, A, B>

204512 Design and Analysis of Computer Algorithm November 8, 2024 23


What is the LCS?

Brute-force algorithm: For every subsequence of x,


check if it’s a subsequence of y
– How many subsequences of x are there?
– What will be the running time of the brute-force
alg?

204512 Design and Analysis of Computer Algorithm November 8, 2024 24

You might also like