Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some more Dynamic Programming codes #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Dynamic Programming/Coin_change/a.exe
Binary file not shown.
43 changes: 43 additions & 0 deletions Dynamic Programming/Coin_change/max_ways.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//Program to count how many ways exist that an amount can be broken into a number of denominations.
#include<bits/stdc++.h>
using namespace std;
int t[100][1000];
int counting(int arr[], int sum,int n) //unbounded subset sum
{
for(int i=0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(j==0)
t[i][j]=1;

if(i==0)
t[i][j]=0;

}
}
t[0][0]=1;
for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(arr[i-1]<=j)
{
t[i][j]= (t[i][j-arr[i-1]])+ t[i-1][j];
}
else if (arr[i-1]>j)
t[i][j]=t[i-1][j];

}
}
return t[n][sum];
}
int main()
{ int coin[] = {1,2,3}; //example cases
int amount= 10;
int n = sizeof(coin) / sizeof(coin[0]);
memset(t,0,sizeof(t));
int count= counting(coin,amount,n);
cout<<count<<endl;
return 0;
}
55 changes: 55 additions & 0 deletions Dynamic Programming/Coin_change/minimum_coins.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//Program to fin dthe minimum number of coins required to add up to a given denomination.
#include<bits/stdc++.h>
using namespace std;
int t[100][1000];
int counting(int arr[], int sum,int n) //unbounded subset sum
{
for(int i=0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(i==0)
t[i][j]=INT_MAX-1;
if(j==0)
t[i][j]=0;

}
}
int j=1;
for(int i =1;j<sum+1;j++) //twist in the initialization
{
if(j%arr[0]==0)
{
t[i][j]=j/arr[0];
}
else
{
t[i][j]=INT_MAX-1;
}

}

for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(arr[i-1]<=j)
{
t[i][j]= min(1+t[i][j-arr[i-1]],t[i-1][j]);
}
else if (arr[i-1]>j)
t[i][j]=t[i-1][j];

}
}
return t[n][sum];
}
int main()
{ int coin[] = {9, 6, 5, 1}; //example cases
int amount= 11;
int n = sizeof(coin) / sizeof(coin[0]);
memset(t,0,sizeof(t));
int count= counting(coin,amount,n);
cout<<count<<endl;
return 0;
}
Binary file added Dynamic Programming/Count_of_subset_sum/a.exe
Binary file not shown.
43 changes: 43 additions & 0 deletions Dynamic Programming/Count_of_subset_sum/count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//Program to count how many subsets exist which adds up to the given sum.
#include<bits/stdc++.h>
using namespace std;
int t[100][1000];
int counting(int arr[], int sum,int n)
{
for(int i=0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(j==0)
t[i][j]=1;

if(i==0)
t[i][j]=0;

}
}
t[0][0]=1;
for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(arr[i-1]<=j)
{
t[i][j]= (t[i-1][j-arr[i-1]])+ t[i-1][j];
}
else if (arr[i-1]>j)
t[i][j]=t[i-1][j];

}
}
return t[n][sum];
}
int main()
{ int arr[] = { 2,3,4,7}; //example cases
int sum= 5;
int n = sizeof(arr) / sizeof(arr[0]);
memset(t,0,sizeof(t));
int count= counting(arr,sum,n);
cout<<count<<endl;
return 0;
}
Binary file added Dynamic Programming/Equal_sum_partition/a.exe
Binary file not shown.
57 changes: 57 additions & 0 deletions Dynamic Programming/Equal_sum_partition/equal_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//Program to check whether 2 partitions exist which have equal sum.
#include<bits/stdc++.h>
using namespace std;
bool t[100][1000]; //t[n+1][sum+1]
bool equalsum(int arr[], int n)
{ int sum=0;
for(int i=0;i<n;i++)
{
sum+=arr[i];
}
if(sum%2!=0)
return false;

sum=sum/2;

//same code as subset sum
for(int i=0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(j==0)
t[i][j]=true;

if(i==0)
t[i][j]=false;

}
}
t[0][0]=true;
for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(arr[i-1]<=j)
{
t[i][j]= (t[i-1][j-arr[i-1]])|| t[i-1][j];
}
else if (arr[i-1]>j)
t[i][j]=t[i-1][j];

}
}
return t[n][sum];
}
int main()
{ int arr[] = { 2,4 }; //example cases
int n = sizeof(arr) / sizeof(arr[0]);
memset(t,-false,sizeof(t));
if(equalsum( arr,n))
printf("YES");
else
{
printf("NO");
}

return 0;
}
Binary file added Dynamic Programming/Knapsack_zero_one/a.exe
Binary file not shown.
39 changes: 39 additions & 0 deletions Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_dp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//dp code maximum profit
#include<bits/stdc++.h>
using namespace std;
int t[100][1000];
int max(int a, int b) { return (a > b) ? a : b; }
int knapsack(int weight[],int val[], int c,int n)
{
for(int i=0;i<n+1;i++)
{
for(int j=0;j<c+1;j++)
{
if(i==0||j==0)
t[i][j]=0;
}
}
for(int i=1;i<n+1;i++)
{
for(int j=1;j<c+1;j++)
{
if(weight[i-1]<=j)
{
t[i][j]= max((val[i-1]+ t[i-1][j-weight[i-1]]), t[i-1][j]);
}
else if (weight[i-1]>j)
t[i][j]=t[i-1][j];

}
}
return t[n][c];
}
int main()
{ int val[] = { 60, 100, 120 }; //example cases
int wt[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(val) / sizeof(val[0]);
memset(t,-1,sizeof(t));
cout << knapsack( wt, val,W, n);
return 0;
}
34 changes: 34 additions & 0 deletions Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_mem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//memoization code
#include<bits/stdc++.h>
using namespace std;
int t[100][1000];
int max(int a, int b) { return (a > b) ? a : b; }
int knapsack(int weight[],int val[], int c,int n)
{
if(n==0||c==0)
{
return 0;
}
if(t[n][c]!=-1)
{
return t[n][c];
}


if(weight[n-1]<=c)
{
return t[n][c]= max((val[n-1]+ knapsack(weight,val,c-weight[n-1],n-1)), knapsack(weight,val,c, n-1));
}
else if (weight[n-1]>c)
return t[n][c]=knapsack(weight,val,c,n-1);
return 0;
}
int main()
{ int val[] = { 60, 100, 120 }; //example cases
int wt[] = { 10, 20, 30 };
int W = 50;
memset(t,-1,sizeof(t));
int n = sizeof(val) / sizeof(val[0]);
cout << knapsack( wt, val,W, n);
return 0;
}
26 changes: 26 additions & 0 deletions Dynamic Programming/Knapsack_zero_one/zero_one_knapsack_rec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//recursive code maximum profit
#include<bits/stdc++.h>
using namespace std;
int max(int a, int b) { return (a > b) ? a : b; }
int knapsack(int weight[],int val[], int c,int n)
{
if(n==0||c==0)
{
return 0;
}
if(weight[n-1]<=c)
{
return max((val[n-1]+ knapsack(weight,val,c-weight[n-1],n-1)), knapsack(weight,val,c, n-1));
}
else if (weight[n-1]>c)
return knapsack(weight,val,c,n-1);
return 0;
}
int main()
{ int val[] = { 60, 100, 120 }; //example cases
int wt[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(val) / sizeof(val[0]);
cout << knapsack( wt, val,W, n);
return 0;
}
Binary file not shown.
49 changes: 49 additions & 0 deletions Dynamic Programming/Largest_Palindromic_Subsequence/pal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*Longest Palindromic Subsequence
Given a sequence, find the length of the longest palindromic subsequence in it.
Example :
Input:"bbbab"
Output:4*/
#include<bits/stdc++.h>
#include<string.h>
#include<string>
using namespace std;
int t[100][1000];

int LCS(string X,string Y, int m,int n)
{
for (int i = 0; i < m+1; ++i)
{
for (int j = 0; j < n+1; ++j)
{
if(i==0||j==0)
t[i][j]=0;
}
}

for (int i = 1; i < m+1; ++i)
{
for(int j = 1; j < n+1; ++j)
{
if(X[i-1]==Y[j-1])
{
t[i][j]=1+t[i-1][j-1];
}
else
{
t[i][j]=max(t[i-1][j],t[i][j-1]);
}
}
}
return t[m][n];
}
int main()
{
string X="bbbab";
string Y=X;
reverse(Y.begin(), Y.end());
int m=X.length();
int n = Y.length();
memset(t,-1,sizeof(t));
cout<<LCS(X,Y,m,n)<<endl;
return 0;
}
Binary file not shown.
Binary file not shown.
Loading