Dynamic Programming: - A Session by Infero
Dynamic Programming: - A Session by Infero
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
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)
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
2 S =S
i i-1
+S
i-2
11
Exercise
18
What if 1<= N <= 10 ?
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.
13
Time & Space Complexity
Usually in DP problems, time 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).
For example:
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?
(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
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.
● Generalize,
27
5 DP Approach
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.
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?
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
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.
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.
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
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