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

Dynamic_Programming

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

Dynamic_Programming

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

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

Notice that each expansion has n + 1 terms.


Example: (x + y)10 will have 10 + 1, or 11 terms.

Copyright © by Houghton Mifflin Company, Inc. All 13


Consider the patterns formed by expanding (x + y)n.

(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

1. The exponents on x decrease from n to 0.


The exponents on y increase from 0 to n.
2. Each term is of degree n.
Example: The 5th term of (x + y)10 is a term with x6y4.”

Copyright © by Houghton Mifflin Company, Inc. All 14


The coefficients of the binomial expansion are called binomial
coefficients. The coefficients have symmetry.
(x + y)5 = 1x5 + 5x4y + 10x3y2 + 10x2y3 + 5xy4 + 1y5

The first and last coefficients are 1.


The coefficients of the second and second to last terms
are equal to n.
Example: What are the last 2 terms of (x + y)10 ? Since n = 10,
the last two terms are 10xy9 + 1y10.

The coefficient of xn–ryr in the expansion of (x + y)n is written  n 


or nCr . So, the last two terms of (x + y)10 can be expressed  r 
as 10C9 xy9 + 10C10 y10 or as 10 xy 9 + 10  y10.
9  10 
   

Copyright © by Houghton Mifflin Company, Inc. All 15


The triangular arrangement of numbers below is called Pascal’s
Triangle.
1 0th row
1 1 1st row
1+2=3 1 2 1 2nd row
1 3 3 1 3rd row
6 + 4 = 10 1 4 6 4 1 4th row
1 5 10 10 5 1 5th row

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 .

Copyright © by Houghton Mifflin Company, Inc. All 16


Contd…
Proof
Proof
n
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
6 1 6 15 20 15 6 1
7 1 7 21 35 35 21 7 1
Table: Pascal's triangle.
Algorithm
Analysis Addition is the
operation
C(12,5)
k
n 0 1 2 3 4 5
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
6 1 6 15 20 15 6
7 1 7 21 35 35 21
8 1 8 28 56 70 56
9 1 9 36 84 126 126
10 1 10 45 120 210 252
11 1 11 55 165 330 462
12 1 12 66 220 495 792
Use Excel for DEMO

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

You might also like