You are on page 1of 62

DYNAMIC PROGRAMMING

Topics to be discussed
Importance Of Data
Structures And Algorithms

Introduction Of Recursion Course outcome


And Its Complexity
Optimize the Solution Of Problem.

Dynamic Programming
Concepts And Problems
Data Structures And Algorithms(DSA)
Why DSA
" I don't like DSA but I want to be a SDE in product based company."

Consider a scenario where you want to make a pizza for yourself.

Can you make your pizza without base?

Answer is No, because without base no one can make pizza.


Contd…
Contd…
Contd…

If base of pizza is not good then there is no meaning of toppings.

Same thing happens with our Professional and Personal Life.


Problem Solving Steps
Problem Statement(Given either in technical form or In story form)

Example-N Queen Problem

Problem Statement -
The N Queen is the problem of placing N chess queens on a N×N
chessboard so that no two queens attack each other. For example, following is a
solution for 4 Queen problem. Q

Q
Problem Solving Steps
Step1-Identifying Problem Statement(problem reframing stage)
in this problem reframed in technical and simple words

Example-N Queen Problem


Problem Statement -
The N Queen is the problem of placing N chess queens on a N×N
chessboard(4 by 4 array) so that no two queens attack each other. For example, following is a
solution for 4 Queen problem.
Q

Q
Problem Solving Steps
Step2-Identifying Constraints-

In the above problem the constraints are-

1-no two queens in same row


2-no two queens in same column
3-no two queens in same diagonal
Problem Solving Steps
Step-3-Design Logic-(may be more than one design logic (approach) used)(Algorithm or pseudo
code in this section also mentioned) (logic building)

Depends on the characteristics of the problem we can choose any one of the following
design strategy for design logic.(In above use backtracking)

a)Devide & Conquer For Solving or implement these algorithms


b)Greedy Method we can use Recursion or Iteration
c)Dynamic Programming
d)Branch & Bound
e)Backtracking
f) Memoization
Problem Solving Steps
Step-4-Validation-(not necessary in every problem)
Most of the algorithm validated by mathematical induction.

Step-5-Analysis-(important step)
Process of comparing two algorithms with respect to time,space,number of register,network
bandwidth etc is called analysis.

Step-6- Implementation-(user choice)


Based on students choice (C / C++ / Java / Python)

Step7-Testing And Debugging(test cases provided like boundary value at least)


Dynamic Programming
“Energy Of Nation Is Like A Sap Of Tree, Its Rises Bottom Up”

Wikipedia Defines Dynamic Programming as-


1-Dynamic programming is a method of solving problem in computer science, Mathematics and Economics .

2-Using this method, a complex problem is split into simpler problems, which are then solved just once and store
their solution into any of the data structure like an array.
3-At the end, the solutions of the simpler problems are used to find the solution of the original complex problem.
4-From the above definition it is clear that there is no meaning of dynamic or even Programming in Dynamic
Programming just like there is no meaning of Apple in Apple Ink because Apple is fruit.
Dynamic Programming
Steps To Learn Dynamic Programming
1-Memory
2-Recursion
3-Optimal Substructure
4-Overlapping Sub-problems
5-Top Down And Bottom Up Approach
6-Memoization
7-Dynamic Programming
Recursion
Recursion-
As we know that maximum computer concept(programming concept) comes from mathematics, Recursion is also one of them.

int main()
{
printf("can not stop programming till i die");
main();
return 0;
}
When a function call itself either directly or indirectly then it is called recursive function and process is called
recursion
Points To Remember-
1- Recursion is problem solving technique where solution of larger problems defined in terms of smaller instances of
itself.
2- Recursion always have terminating condition otherwise infinite recursion.
3- Recursive function performs some part of task and deligate rest of it to recursive call.
Recursion
Program
int fib(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return fib(n-1)+fib(n-2);
}

For n=5
total function call=15
=16-1
=2*8-1
=2*term(6)-1
In general total function call for size n=2*term(n+1)-1
Optimal Substructure
Optimal Substructure (Am I Just Recursion)
Optimal substructure means optimal solution to a problem size of n is based on optimal
solution to the same problem but in smaller size ie while building a solution of the problem
of size n, define it in terms of similar problem of smaller size (say k<n). Find optimal
solution of less elements and combined it to get final optimal solution .
Overlapping Sub Problem
Recursive Solution
int fib(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return fib(n-1)+fib(n-2);
}
Overlapping Sub Problem
Iterative solution n 2 3 4 5 20 40
Recursive 1 3 5 9 13529 204668309
int fib(int n)
{
int a = 0, b = 1, c, i; Iterative 1 1 1 1 1 1
if( n == 0)
return a;
for (i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}
Overlapping Sub Problem
If we Call fib(20)

Then Fib(3) called 2584 times and fib(10) called 89 times.


For n=20
Recursive solution takes 65.218 micro second
Iterative solution takes 0.109 microsecond

Fib(80)
Recursive solution takes an hour or may be hang
Iterative solution takes approx. 1 second.

The main culprit is overlapping subproblems.


Top Down Approach (Memoization)
Memoization
I wish we could choose which memory to remember
Memoization = Recursion + Cache - Recomputing Overlapping Subproblems

Works on top down fashion and also called top down dynamic programming or memorized
dynamic programming.

n 2 3 4 5 20 40
recursive 1 3 5 9 13529 204668309
iterative 1 1 1 1 1 1
memo 1 3 5 7 37 77
Top Down Approach (Memoization)
#include <stdio.h> int fib(int n)
int fib(int n); {
#define N 20 int a = 0, b = 1, c, i;
int memo[N]={0}; if( memo[n]!= 0)
return memo[n];
int main(void) if(n==0)
{ memo[n]=0;
int n=75,x; if (n==1 || n==2)
x=fib(n); memo[n]=1;
printf("%d",x); else
return 0; memo[n]=fib(n-1)+fib(n-2);
} return memo[n];
}
Bottom Up Approach (DP)
1-Dynamic programming unroll the recursion and move to the
opposite direction of memorization.

2-Both memoization and dynamic programming solve the problem


only once.

3-Memoization uses recursion and works in top down manner where


as dynamic programming moves in opposite direction solving the
problem bottom up.

4-Dynamic programming use the concept of recursive formula and


uses iteration.
Bottom Up Approach (DP)
int fib(int n)
#include<stdio.h> {
int main () int f[n+1];
{ int i;
int n = 75;
printf("%d", fib(n)); f[0] = 0;
return 0; f[1] = 1;
}
for (i = 2; i <= n; i++)
{
f[i] = f[i-1] + f[i-2];
}

return f[n];
}
Subset Sum Problem
Given a set of non negative integers and a positive number X, determine if
there exist a subset of elements of an array with sum equal to X.
Input {3,2,7,1}
X=6
Output true
Subset Sum Problem(Recursive)
#include<stdio.h>
#define true 1
#define false 0
int issubset(int *arr,int n,int x)
{
if(x==0)
return true;
if(n==0)
return false;

if(arr[0]>x)
return issubset(arr+1,n-1,x);
else
return issubset(arr+1,n-1,x) ||issubset(arr+1,n-1,x-arr[0]);
}
Subset Sum Problem
int main()
{
int arr[4]={3,2,7,1},x=6,n=4,res;
res=issubset(arr,n,x);
if(res==true)
printf("subset exist");
else
printf("not exist");
return 0;
}
Subset Sum Problem(DP)
#include<stdio.h>
int subset(int arr[],int n, int sum);
int main()
{
int arr[]={3,2,7,1};
int x;
x=subset(arr,4,6);
if(x==1)
printf("sum is possible");
if(x==0)
printf("sum is not possible");
return(0);
}
Subset Sum Problem
int subset(int *arr,int n,int sum)
{
int subsetsum[n+1][sum+1],i,j;
for(i=0;i<=n;i++)
subsetsum[i][0]=1;
for(i=1;i<=sum;i++)
subsetsum[0][i]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=sum;j++)
{
if(j<arr[i-1])
subsetsum[i][j]=subsetsum[i-1][j];
if(j>=arr[i-1])
subsetsum[i][j]=subsetsum[i-1][j] || subsetsum[i-1][j-arr[i-1]];
}
}
return(subsetsum[n][sum]);
}
Coin Change Problem
Coin Change Problem
Coin Change Problem (Recursion)
int count(int coins[], int n, int sum)
{
if (sum == 0)
return 1;

if (sum < 0)
return 0;
if (n <= 0)
return 0;
return count(coins, n - 1, sum)
+ count(coins, n, sum - coins[n - 1]);
} int main()
{
int i, j;
int coins[] = { 1, 2, 3 };
printf("%d ", count(coins, n, 5));
return 0;
}
Coin Change Problem (Recursion)
Coin Change Problem (DP)
Coin Change Problem
Total Path Count
Total Path Count
Technical Training
Technical Training
Technical Training
Technical Training
Technical Training
Technical Training
Coin Change Problem
Coin Change Problem
Coin Change Problem
Coin Change Problem
Coin Change Problem
KnapSack(DP)
#include<stdio.h>
int knapsack(int ,int*,int*,int);
int max(int x,int y);
int main()
{
int weight[]={2,3,4,5},value[]={3,4,5,6},profit,capacity=5;
profit=knapsack(capacity,weight,value,4);
printf("total profit=%d",profit);
return(0);
}
KnapSack(DP)
int knapsack(int capacity,int *weight,int *value,int n)
{

int arr[n+1][capacity+1],i,j,x;
for(i=0;i<=n;i++)
arr[i][0]=0;
for(j=1;j<=capacity;j++)
arr[0][j]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=capacity;j++)
{
if(weight[i-1]<=j)
{
x=j-weight[i-1];
arr[i][j]=max(value[i-1]+arr[i-1][x],arr[i-1][j]);
}
else
arr[i][j]=arr[i-1][j];

}
}
return(arr[n][capacity]);
}
Edit Distance Problem
Edit Distance Problem
Edit Distance Problem
Edit Distance Problem
Edit Distance Problem
Edit Distance Problem
Edit Distance Problem
Permutation Problem
LPS Problem
Permutation Problem
Permutation Problem
Share your thoughts

Questions ?
Answer

You might also like