Dynamic_Programming
Dynamic_Programming
Presentation by
V. Balasubramanian
SSN College of Engineering
Dynamic Programming
• Dynamic Programming is a general
algorithm design technique for
solving problems defined by
recurrences with overlapping
subproblems
• Invented by American mathematician
Richard Bellman in the 1950s to
solve optimization problems and
later assimilated by CS
• The term "dynamic programming"
comes from control theory, and
• in this sense "programming" means
the use of an array (table) in which a
solution is constructed.
Dynamic Programming
• “Programming” here means
“planning”
• Main idea:
– set up a recurrence relating a solution to a
larger instance to solutions of some smaller
instances
• - solve smaller instances once
– record solutions in a table
– extract solution to the initial instance from that table
Permutations
• Suppose we have four balls marked
A, B, C, and D in an urn or container,
and two balls will be drawn
• AB & BA are different. 4(3) = 12
Strategy
Contd…
• if we have four balls and three are
• drawn, the first ball can be any one
of four; once the first ball is drawn,
the second ball can be any of three;
and once the second ball is drawn,
the third ball can be any of two.
• 4.3.2 = 24.
Contd…
• In general, if we have n balls, and
we are picking k of them,
• (n)(n - 1) • • • (n - k + 1).
• If k=n,
– (n)(n - 1) • • • (n - n + 1) = n!.
•
Combinations
• A and B, A and C, A and D, B and
C,B and D, C and D.
• 6 distinct outcomes.
• 4(3)/2 =6.
• 3 balls to be taken.
• 4(3)(2)/3! = 4.
Contd…
• if there are n balls and k balls are
drawn, then
Contd…
• Binomial coefficients are coefficients
of the binomial formula:
• (a + b)n = C(n,0)anb0 + . . . +
C(n,k)an-kbk + . . . + C(n,n)a0bn
• Recurrence:
• C(n,k) = C(n-1,k) + C(n-1,k-1) for n > k > 0
• C(n,0) = 1, C(n,n) = 1 for n >= 0
The binomial theorem provides a useful method for raising any
binomial to a nonnegative integral power.
Consider the patterns formed by expanding (x + y)n.
(x + y)0 = 1 1 term
(x + y)1 = x + y 2 terms
(x + y)2 = x2 + 2xy + y2 3 terms
(x + y)3 = x3 + 3x2y + 3xy2 + y3 4 terms
(x + y)4 = x4 + 4x3y + 6x2y2 + 4xy3 + y4 5 terms
(x + y)5 = x5 + 5x4y + 10x3y2 + 10x2y3 + 5xy4 + y5 6 terms
(x + y)0 = 1
(x + y)1 = x + y
(x + y)2 = x2 + 2xy + y2
(x + y)3 = x3 + 3x2y + 3xy2 + y3
(x + y)4 = x4 + 4x3y + 6x2y2 + 4xy3 + y4
(x + y)5 = x5 + 5x4y + 10x3y2 + 10x2y3 + 5xy4 + y5
Each number in the interior of the triangle is the sum of the two
numbers immediately above it.
The numbers in the nth row of Pascal’s Triangle are the binomial
coefficients for (x + y)n .
Fact(12) 479001600
Fact(5) 120
Fact(7) 5040
12c5 792
Longest Common Subsequence
• C(I,j) = max (c(i-1,j), c(I,j-1) + 1
/0
Gift collection
0 1 2 3 4 5 6
1 Gift gift
2 Gift
3 Gift Gift
4 Gift
5 Gift Gift
6 Gift Gift
Gift collection
0 1 2 3 4 5 6
0 0 0 0 0 0 0 0
1 0 0 0 1 1 1 2
2 0 0 0 1 2 2 2
3 0 0 1 1 2 3 3
4 0 0 1 1 3 3 3
5 0 1 1 1 3 4 4
6 0 1 1 2 3 4 5
Matrix Chain Multiplication
Example
5 different orders
Recurrence equation
P(1,6)=1, P(2,6)=5
P Matrix