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 Dynamic Programming and Tree Folder #26

Open
wants to merge 2 commits 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 not shown.
39 changes: 39 additions & 0 deletions Data Structures/Dynamic Programming/0:1 Knapsack/0:1Knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// A Dynamic Programming based
// solution for 0-1 Knapsack problem
#include <bits/stdc++.h>

using namespace std;

// Returns the maximum value that
// can be put in a knapsack of capacity W
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n + 1][W + 1];

// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(
val[i - 1] + K[i - 1][w - wt[i - 1]],
K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}

return K[n][W];
}

int main()
{
int val[] = { 60, 100, 120 };
int wt[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(val) / sizeof(val[0]);
printf("%d\n", knapSack(W, wt, val, n));
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// A Dynamic Programming based
// C++ program to partition problem
#include <bits/stdc++.h>
using namespace std;

// Returns true if arr[] can be partitioned
// in two subsets of equal sum, otherwise false
bool findPartiion (int arr[], int n)
{
int sum = 0;
int i, j;

// Calculate sum of all elements
for (i = 0; i < n; i++)
sum += arr[i];

if (sum % 2 != 0)
return false;

bool part[sum / 2 + 1][n + 1];

// initialize top row as true
for (i = 0; i <= n; i++)
part[0][i] = true;

// initialize leftmost column,
// except part[0][0], as 0
for (i = 1; i <= sum / 2; i++)
part[i][0] = false;

// Fill the partition table in botton up manner
for (i = 1; i <= sum / 2; i++)
{
for (j = 1; j <= n; j++)
{
part[i][j] = part[i][j - 1];
if (i >= arr[j - 1])
part[i][j] = part[i][j] ||
part[i - arr[j - 1]][j - 1];
}
}

/* // uncomment this part to print table
for (i = 0; i <= sum/2; i++)
{
for (j = 0; j <= n; j++)
cout<<part[i][j];
cout<<endl;
} */

return part[sum / 2][n];
}

// Driver Code
int main()
{
int arr[] = {3, 1, 1, 2, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
if (findPartiion(arr, n) == true)
cout << "Can be divided into two subsets of equal sum";
else
cout << "Can not be divided into"
<< " two subsets of equal sum";
return 0;
}

// This code is contributed by rathbhupendra
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// A Recursive C program to solve minimum sum partition
// problem.
#include <bits/stdc++.h>
using namespace std;

// Returns the minimum value of the difference of the two sets.
int findMin(int arr[], int n)
{
// Calculate sum of all elements
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];

// Create an array to store results of subproblems
bool dp[n+1][sum+1];

// Initialize first column as true. 0 sum is possible
// with all elements.
for (int i = 0; i <= n; i++)
dp[i][0] = true;

// Initialize top row, except dp[0][0], as false. With
// 0 elements, no other sum except 0 is possible
for (int i = 1; i <= sum; i++)
dp[0][i] = false;

// Fill the partition table in bottom up manner
for (int i=1; i<=n; i++)
{
for (int j=1; j<=sum; j++)
{
// If i'th element is excluded
dp[i][j] = dp[i-1][j];

// If i'th element is included
if (arr[i-1] <= j)
dp[i][j] |= dp[i-1][j-arr[i-1]];
}
}

// Initialize difference of two sums.
int diff = INT_MAX;

// Find the largest j such that dp[n][j]
// is true where j loops from sum/2 t0 0
for (int j=sum/2; j>=0; j--)
{
// Find the
if (dp[n][j] == true)
{
diff = sum-2*j;
break;
}
}
return diff;
}

// Driver program to test above function
int main()
{
int arr[] = {3, 1, 4, 2, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "The minimum difference between 2 sets is "
<< findMin(arr, n);
return 0;
}
Binary file not shown.
59 changes: 59 additions & 0 deletions Data Structures/Dynamic Programming/0:1 Knapsack/subset_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// A Dynamic Programming solution
// for subset sum problem
#include <bits/stdc++.h>

using namespace std;

// Returns true if there is a subset of set[]
// with sun equal to given sum
bool isSubsetSum(int set[], int n, int sum)
{
// The value of subset[i][j] will be true if
// there is a subset of set[0..j-1] with sum
// equal to i
bool subset[n + 1][sum + 1];

// If sum is 0, then answer is true
for (int i = 0; i <= n; i++)
subset[i][0] = true;

// If sum is not 0 and set is empty,
// then answer is false
for (int i = 1; i <= sum; i++)
subset[0][i] = false;

// Fill the subset table in botton up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (j < set[i - 1])
subset[i][j] = subset[i - 1][j];
if (j >= set[i - 1])
subset[i][j] = subset[i - 1][j]
|| subset[i - 1][j - set[i - 1]];
}
}

/* // uncomment this code to print table
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= sum; j++)
printf ("%4d", subset[i][j]);
printf("\n");
}*/

return subset[n][sum];
}

// Driver program to test above function
int main()
{
int set[] = { 3, 34, 4, 12, 5, 2 };
int sum = 9;
int n = sizeof(set) / sizeof(set[0]);
if (isSubsetSum(set, n, sum) == true)
printf("Found a subset with given sum");
else
printf("No subset with given sum");
return 0;
}
// This code is contributed by Arjun Tyagi.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// A Dynamic Programming solution
// for subset sum problem
#include <bits/stdc++.h>

using namespace std;

// Returns true if there is a subset of set[]
// with sun equal to given sum
int isSubsetSum(int set[], int n, int sum)
{
// The value of subset[i][j] will be true if
// there is a subset of set[0..j-1] with sum
// equal to i
int subset[n + 1][sum + 1];

// If sum is 0, then answer is true
for (int i = 0; i <= n; i++)
subset[i][0] = true;

// If sum is not 0 and set is empty,
// then answer is false
for (int i = 1; i <= sum; i++)
subset[0][i] = false;

// Fill the subset table in botton up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (j < set[i - 1])
subset[i][j] = subset[i - 1][j];
if (j >= set[i - 1])
subset[i][j] = subset[i - 1][j]
+ subset[i - 1][j - set[i - 1]];
}
}

/* // uncomment this code to print table
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= sum; j++)
printf ("%4d", subset[i][j]);
printf("\n");
}*/

return subset[n][sum];
}

// Driver program to test above function
int main()
{
int set[] = { 2, 3, 5, 8, 10};
int sum = 10;
int n = sizeof(set) / sizeof(set[0]);
cout<<isSubsetSum(set, n, sum)<<endl;


return 0;
}
// This code is contributed by Arjun Tyagi.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Dynamic Programming C++ implementation of LCS problem */
#include<bits/stdc++.h>
using namespace std;

// int max(int a, int b);

/* Returns length of LCS for X[0..m-1], Y[0..n-1] */
int lcs( string X, string Y, int m, int n )
{
int L[m + 1][n + 1];
int i, j;

/* Following steps build L[m+1][n+1] in
bottom up fashion. Note that L[i][j]
contains length of LCS of X[0..i-1]
and Y[0..j-1] */
for (i = 0; i <= m; i++)
{
for (j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;

else if (X[i - 1] == Y[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;

else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}

/* L[m][n] contains length of LCS
for X[0..n-1] and Y[0..m-1] */
return L[m][n];
}

/* Utility function to get max of 2 integers */
// int max(int a, int b)
// {
// return (a > b)? a : b;
// }

// Driver Code
int main()
{
string X = "AGGTAB";
string Y = "GXTXAYB";

int m = X.length();
int n = Y.length();

cout << "Length of LCS is "
<< lcs( X, Y, m, n );

return 0;
}

// This code is contributed by rathbhupendra
Binary file not shown.
Loading