Dynamic Programming Made Simpler
Dynamic Programming Made Simpler
Programming
Made Easy
Programming
example.
What is 2+2+2+2?
→8
→10
1
This can be represented by what is the sum
of 2 taken n times?
To solve this we can consider an array f
For Example:
So if we have say n=4,
f(n-1)=f(3)=6
And f(4)=f(3)+2=6+2=8
2
Let's solve this question through code
f(1)=2
int count2(int n)
int f[n+1];
f[0]=0;
f[1]=2;
//iterate through 2 to n
for(int i=2;i<=n;i++)
f[i]=f[i-1]+2;
return f[n];
Complexity Analysis:
Time Complexity: O(n).
3
Dynamic programming
vs Recursion
The basic concepts of dynamic programming are similar to
recursion
Dynamic programming trades space for time.
It uses more space to store the results of sub-problems to
reduce time taken rather than taking a lot of time to
calculate sub-problem solutions and saving space.
Example:
Consider the problem of finding out the nth Fibonacci
number
A fibonacci series is defined as a series of numberB
Each number is the sum of the previous two numbers.
Starting off with 0 and 1, the next number in the series would
be 1, since 0+1=1
0,1,1,2,3,5,8……
4
To calculate the nth number of Fibonacci series, we can
either use a recursive approach or dynamic programming
approach.
int fib(intn)
if (n <= 1)
return;
Recursion : Exponential
return fib(n-1) + fib(n-2);
f[0] = 0;
f[1] = 1;
return f[n];
5
Consider the same example of calculating the nth fibonacci
number.
6
In Dynamic Programming, the subproblems are dependent on
each other and they overlap
Complexity Analysis
It is a bottom up approach.
int f[MAXN];
f[i] = f[i-1] * i;
7
Memoization
Memoization is a form of caching and is used to optimise
recursion
If n== 0,
return 1
Else
Calculate (n−1)!×n
Return result
8
Dynamic Programming algorithm is designed
using the following four steps
% Characterise the structure of an optimal solution%
A% Recursively define the value of an optimal solution%
% Compute the value of an optimal solution, typically in a
bottom-up fashion%
% Construct an optimal solution from the computed
information.
9
Stairs problem
A person when running up a staircase with n steps and can
hop either 1 step, 2 steps, or 3 steps at a time. Find out how
many possible ways the person can run up the stairs.
Example:
If there are 3 stairs in the staircase, the person can run up in
4 ways.
1 step + 2 step
2 step + 1 step
3 step
10
Algorithm:
, Create a 1d array dp of size n+2
Initialise the array with base cases as following
dp[0]=1, dp[1]=1, dp[2]="
Run a loop from 3 to n
* For each index i, compute the value of ith position as
dp[i] = dp[i-1] + dp[i-2] + dp[i-3].
That is to reach the ith stair, we count the number of ways
to reach (i-1)th stair + (i-2)th stair + (i-3)th stair 5
Print the value of dp[n], as the Count of the number of
ways to run up the staircase.
Complexity Analysis:
11
Dynamic programming can only be
mastered by practice. So, just keep
practicing and you will start solving
problems on your own in no time.
Happy Coding!
12
Why
Bosscoder?
1000+ Alumni placed at Top
Product-based companies.
Explore More