0% found this document useful (0 votes)
22 views42 pages

Dynamic Programming: - A Session by Infero

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views42 pages

Dynamic Programming: - A Session by Infero

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Dynamic

Programming
-A session by Infero
Hello!
— Team Infero
Today’s session is conducted by Aayush,
Dikshant, Hemanth, Nimai and Tuhil –
Cores of Infero

2
Roadmap for Today
Understanding Time Classical Knapsack
What is DP?
Complexity of DP Algorithms problem

1 3 5

2 4 6

Learning DP by solving More DP problems Some questions for you


some problems guys to try after the
session.

3
What is
1 Dynamic
Programming?
What is
Dynamic
Programming?
1 Dynamic programming can be referred to a way
of optimization by storing intermediate states
that would need to be repetitively calculated if
we use plain brute force or recursion. Hence,
reducing the time complexity.
Tiling problem
You are give a tiling area of shape 1 x N

…….. (length N)

2 And also given 2 types of tiles of shape 1 x 1 and 1 x 2 as shown below.

Your objective is to find the no. of ways to fill the entire area by
using only these 2 types of tiles.
For example: N = 3, we have 3 ways as shown below.
Tiling problem
First lets see some more examples,
N = 1, only 1 way
N = 2, 2 ways
2 N = 3, we have seen 3 ways
N = 4, we have 5 ways

What do you observe?


Tiling problem
First lets see some more examples,
N = 1, only 1 way
N = 2, 2 ways
2 N = 3, we have seen 3 ways
N = 4, we have 5 ways

What do you observe?


(1),1,2,3,5….
Tiling problem
Let Si denote the no. of ways we can tile 1 x i
space, So, we have,

2 S =S
i i-1
+S
i-2

Now, lets see some coding approaches.


● Recursion
● DP
Recursive approach

The time complexity is exponential!


Approx O(1.62n), Hence, this method is only suitable for N upto around 25
10 for time limit of say 1 sec.
DP approach
The time complexity is O(n)
as we go through each state
only once (constant) and we
have n states.
Hence, this approach can be
used for given N upto 107.

11
Exercise
18
What if 1<= N <= 10 ?

Can you do it in O(log(N)) or


O(log2(N))?

12
States and transition
◇ A state of the problem usually
represents a sub-solution, i.e. a partial
solution or a solution based on a subset
of the given input. And the states are
built one by one, based on the
previously built states.

◇ Transition refers to the formula/steps


to find out the next state.

13
Time & Space Complexity
Usually in DP problems, time complexity is
proportional to

(Number of states) * (Number of transitions at each state)

3 Space complexity is proportional to

Number of states
No. of paths
You are given a N x M space and you are to go from
(1,1) to (N,M).
● At each movement, you can either go right
or down. What is the number of ways you
can reach (N,M).

3 Output the no.of ways modulo 1,000,000,007 (Why?)

For example:

N = 1, M = 10. We have 1 way.


N = 2, M = 2. We have 2 ways.
N = 5, M = 5. We have 70 ways.
3
1 1 1 1 1

16
3
1 1 1 1 1

1 2

1 3

17
3
1 1 1 1 1

1 2 3 4 5

1 3 6 10 15

1 4 10 20 35

1 5 15 35 70

18
DP approach

3
Time complexity
● So, what is the time complexity?
O(N*M). Hence, works within time for N*M<= 107.

● Exercise:
What if 108<=N*M<=1012?

3 Hint: Modify this problem as finding no of letter


sequences consisting of N ‘D’s and M ‘R’s.
D = down, R = right.

For example: for N =3, M=2, a possible sequence


is DDRDR.

(JEE PnC)
What if there are
some obstacles where
you can’t enter?

3 ◆


3 1 1 1 1 1

1 2 ◆ 1 2

1 3 3 4 6

1 4 7 ◆ 6

1 5 12 12 18

You can do the exact same approach


but fixing the obstacle positions as 0
and excluding them while finding DP
table.
22
Submatrix sum queries
You are given a N x M matrix of integers. 1<=
N,M <= 1000

You will be given Q queries of type h1,w1, h2,w2.


4 For each query, output the sum of the submatrix
with corners as h1,w1 and h2,w2.
For example: N = 3, M = 3, Q = 1, 1 -2 3
h1 = 1,w1 =2 , h2 = 2, w2 =3
The highlighted square is the 2 4 -3
queried region and answer = 7.
1 0 2
You are solve each query in O(1) after
pre-computation,
(a+b)2
We know that,
(a+b)2 = a2 + b2 + 2ab a
2 ab

4 Implies,
b2 = (a+b)2 + a2 - 2a2 -
2ab ab 2
b
Implies,
b2 = (a+b)2 + a2 - a(a+b) -
a(a+b)
Relate this to sum of elements in b2
region.
Matrix property
Observe that if we want the sum
of the blue section, it can be 0 0 0 0 0
found by
0 1 -2 1 3
sum(full matrix) + sum(yellow matrix)
-sum(yellow+blue) - sum(yellow+green)
4 So, if you could store the
Sum of elements from (1,1) to (i,j)
0

0
1

1
0

1
0

0
1

7
At each (i,j),
we can answer each query in O(1). 0 0 4 1 -2
Exercise: Compute the dp table.

Answer to the query: h1,w1,h2,w2 will be-


dp [h2][w2] + dp [h1-1][w1-1] - dp [h1-1][w2] - dp[h2][w1-1]
Minimizing coins
Consider a money system consisting of n coins.
Each coin has a positive integer value. Your task is
5 to produce a sum of money x using the available
coins in such a way that the number of coins is
minimal. Output this minimum number.

For example, if the coins are {1,5,7} and the desired


sum is 11, an optimal solution is 5+5+1 which
requires 3 coins.
5 How would you solve it?
● Let’s say, coins = {1, 5, 7} and sum = 9.
● Say we pick coin[0] = 1, now sum = 8 but the same problem
remains.

● Generalize,

Dp[sum] = 1 + min( Dp[ sum-coins[i]] )

● What is the base case?

● And the answer?

27
5 DP Approach

Can u find the Time and


Space Complexity of this
DP?

28
Coin combinations
Consider a money system consisting of n coins. Each
coin has a positive integer value. Your task is to
calculate the number of distinct ways you can
produce a money sum x using the available coins.

6 For example, if the coins are {2,3,5} and the desired


sum is 9, there are 8 ways:

2+2+5
2+5+2
5+2+2
3+3+3
2+2+2+3
2+2+3+2
2+3+2+2
3+2+2+2
6 How would you solve it?
● Can you generalize?

Dp[sum] = 1 + Σ (Dp[sum - coins[i]])

● What is the base case?

● And the answer?

30
Solve it here.
cses.fi/problemset/task/1635/
6
Hoping that you have understood the
approach, I suggest to try it yourself
again.
DP Approach

Can u find the Time and


Space Complexity of this
DP?

32
Book Shop (0-1 Knapsack)
You are in a book shop which sells n different books. You
know the price and number of pages of each book.
You have decided that the total price of your purchases
will be at most x. What is the maximum number of
pages you can buy? You can buy each book at most
once.

7 Constraints: 1<= n, cost[ i ], pages[ i ] <=1000, x<= 100000


Example:
n = 4, x = 10.

cost 4 8 5 3

pages 5 12 8 1
Book Shop (0-1 Knapsack)
You are in a book shop which sells n different books. You
know the price and number of pages of each book.
You have decided that the total price of your purchases
will be at most x. What is the maximum number of
pages you can buy? You can buy each book at most
once.

7 Constraints: 1<= n, cost[ i ], pages[ i ] <=1000, x<= 100000


Example:
n = 4, x = 10.

cost 4 8 5 3

pages 5 12 8 1

Ans: 13
Explanation: You can buy books 1 and 3. Their price is 4+5
= 9 and the number of pages is 5 + 8 = 13.
cost 4 8 5 3
7
pages 5 12 8 1

Value ( j ) 0 1 2 3 4 5 6 7 8 9 10
Cost,pages( i )

0,0 0 0 0 0 0 0 0 0 0 0 0

4,5 0 0 0 0 0 0 0 0 0 0 0

8,12 0 0 0 0 0 0 0 0 0 0 0

5,8 0 0 0 0 0 0 0 0 0 0 0

3,1 0 0 0 0 0 0 0 0 0 0 0
35
7
dp [ i ] [ j ] = max( dp[ i-1 ][ j ] ,dp[i-1] [ j - cost[i] ]+ pages[i]);
dp [ i ][ j ] = max( dp[ i ][ j ] , dp[ i ][ j - 1 ]);

Value ( j ) 0 1 2 3 4 5 6 7 8 9 10
Cost,pages( i )

0,0 0 0 0 0 0 0 0 0 0 0 0

4,5 0 0 0 0 0 0 0 0 0 0 0

8,12 0 0 0 0 0 0 0 0 0 0 0

5,8 0 0 0 0 0 0 0 0 0 0 0

3,1 0 0 0 0 0 0 0 0 0 0 0

36
7
dp [ i ] [ j ] = max( dp[ i-1 ][ j ] ,dp[i-1] [ j - cost[i] ]+ pages[i]);
dp [ i ][ j ] = max( dp[ i ][ j ] , dp[ i ][ j - 1 ]);

Value ( j ) 0 1 2 3 4 5 6 7 8 9 10
Cost,pages( i )

0,0 0 0 0 0 0 0 0 0 0 0 0

4,5 0 0 0 0 5 5 5 5 5 5 5

8,12 0 0 0 0 0 0 0 0 0 0 0

5,8 0 0 0 0 0 0 0 0 0 0 0

3,1 0 0 0 0 0 0 0 0 0 0 0

37
7
dp [ i ] [ j ] = max( dp[ i-1 ][ j ] ,dp[i-1] [ j - cost[i] ]+ pages[i]);
dp [ i ][ j ] = max( dp[ i ][ j ] , dp[ i ][ j - 1 ]);

Value ( j ) 0 1 2 3 4 5 6 7 8 9 10
Cost,pages( i )

0,0 0 0 0 0 0 0 0 0 0 0 0

4,5 0 0 0 0 5 5 5 5 5 5 5

8,12 0 0 0 0 5 5 5 5 12 12 12

5,8 0 0 0 0 5 8 8 8 12 13 13

3,1 0 0 0 1 5 8 8 8 12 13 13

38
It is always good to code the problem as it

“ will improve your implementation skills,


even though you have seen the solution.
In many cases, you do run into problems.

39
Book Shop (0-1
Knapsack) Solve it here.

cses.fi/problemset/task/1158/
7
Hoping that you have understood the
approach, I suggest to try it yourself
again.
Practice
Problems
8 ● https://cses.fi/problemset/task/1637/
● https://atcoder.jp/contests/dp/tasks/dp_c
● https://cses.fi/problemset/task/1097
● https://cses.fi/problemset/task/1639/
Thanks!
Any doubts?
You can reach out us at:

42

You might also like