Skip to content

Commit

Permalink
Merge pull request #102 from souravbhunia07/sourav
Browse files Browse the repository at this point in the history
Added Dynamic Programming Hacktoberfest
  • Loading branch information
debrajrout authored Oct 3, 2023
2 parents 0502935 + 8856d98 commit 925e740
Show file tree
Hide file tree
Showing 42 changed files with 1,936 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Data Structures and Algorithm/DP/Cutting_rod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <bits/stdc++.h>
using namespace std;

// Top up method:- O(n ^ 2)

int memoTopUpCutRod(vector<int> p, int n, vector<int> r)
{
int q;
if(r[n] >= 0)
{
return r[n];
}
if(n == 0)
{
q = 0;
}
else
{
q = INT_MIN;
}
for (int i = 1; i <= n; ++i)
{
q = max(q, p[i - 1] + memoTopUpCutRod(p, n - i, r));
}
r[n] = q;
return q;
}

int topUpCutRod(vector<int> p, int n)
{
vector<int> r(n + 1, INT_MIN);
return memoTopUpCutRod(p, n, r);
}

// Bottom up method:- O(n ^ 2)

int bottomUpCutRod(vector<int> p, int n)
{
vector<int> r(n + 1, INT_MIN);
r[0] = 0;
for (int i = 1; i <= n; ++i)
{
int q = -1;
for (int j = 1; j <= i; ++j)
{
q = max(q, p[j - 1] + r[i - j]);
}
r[i] = q;
}
return r[n];
}

// Optimal Bottom up solution:-

// int printOptimalSol(vector<int> p, int n)
// {
// auto [r, s] = OptimalBottomUp(p, n);
// while(n > 0)
// {
// cout << s[n] << " ";
// n -= s[n];
// }
// }

// OptimalBottomUp(vector<int> p, int n)
// {
// vector<int> r(n + 1);
// vector<int> s(n);
// r[0] = 0;
// for (int j = 1; j <= n; ++j)
// {
// int q = INT_MIN;
// for (int i = 1; i <= j; ++i)
// {
// if(q < p[i - 1] + r[j - i])
// {
// q = p[i - 1] + r[j - i];
// s[j - 1] = i;
// }
// }
// r[j] = q;
// }
// return {move(r), move(s)};
// }

int main()
{
vector<int> p{1, 5, 8, 9, 10, 17, 17, 20, 24, 30};
// vector<int> p{1,3,4,5};
int n = 4;
// int n = p.size();
cout << bottomUpCutRod(p, n) << endl;
cout << topUpCutRod(p, n) << endl;
// cout << printOptimalSol(p, n) << endl;
return 0;
}
36 changes: 36 additions & 0 deletions Data Structures and Algorithm/DP/DP_on_tree/Diameter_of_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int res;
public:
int Solve(TreeNode* root) {
if (root == NULL)
return 0;

int l = Solve(root->left);
int r = Solve(root->right);

int temp = 1 + max(l, r);
int ans = max(temp, l + r + 1);
res = max(res, ans);
return temp;
}
int diameterOfBinaryTree(TreeNode* root) {
if (root == NULL)
return 0;

res = INT_MIN + 1;
Solve(root);
return res - 1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
using namespace std;

const int D = 1000; // DP - matrix dimension

int t[D][D]; // DP matrix

int Knapsack(int wt[], int val[], int W, int n) {
// base case
if (n == 0 || W == 0)
return 0;

// if already calculated


if (t[n][W] != -1)
return t[n][W];

// else calculate
else {
if (wt[n - 1] <= W)
t[n][W] = max(val[n - 1] + Knapsack(wt, val, W - wt[n - 1], n - 1),Knapsack(wt, val, W, n - 1));
else if (wt[n - 1] > W)
t[n][W] = Knapsack(wt, val, W, n - 1);

return t[n][W];
}
}

signed main() {
int n; cin >> n; // number of items
int val[n], wt[n]; // values and wts array
for (int i = 0; i < n; i++)
cin >> wt[i];
for (int i = 0; i < n; i++)
cin >> val[i];
int W; cin >> W; // capacity

// matrix initialization
for (int i = 0; i <= n; i++)
for (int j = 0; j <= W; j++)
t[i][j] = -1; // initialize matrix with -1

cout << Knapsack(wt, val, W, n) << endl;
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
using namespace std;

int Knapsack(int wt[], int val[], int W, int n) {
int t[n + 1][W + 1]; // DP matrix

for (int i = 0; i <= n; i++) {
for (int j = 0; j <= W; j++) {
if (i == 0 || j == 0) // base case // filling 1st row and 1st column of the matrix with zero as per the base condition of the recursive solution
t[i][j] = 0;
else if (wt[i - 1] <= j) { // current wt can fit in bag // this is for the choice diagram of the recursive solution
int val1 = val[i - 1] + t[i - 1][j - wt[i - 1]]; // take current wt // and after taking weight substract the inserted weight from the final weight
int val2 = t[i - 1][j]; // skip current wt
t[i][j] = max(val1, val2);
}
else if (wt[i - 1] > j) // current wt doesn't fit in bag
t[i][j] = t[i - 1][j]; // move to next
}
}

return t[n][W];
}

int main() {
int n; cin >> n; // number of items
int val[n], wt[n]; // values and wts array
for (int i = 0; i < n; i++)
cin >> wt[i];
for (int i = 0; i < n; i++)
cin >> val[i];
int W; cin >> W; // capacity

cout << Knapsack(wt, val, W, n) << endl;
return 0;
}

/* Complexity Analysis:
Time Complexity: O(N*W).
where ‘N’ is the number of weight element and ‘W’ is capacity. As for every weight element we traverse through all weight capacities 1<=w<=W.
Auxiliary Space: O(N*W).
The use of 2-D array of size ‘N*W’.
*/
// https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// https://www.geeksforgeeks.org/subset-sum-problem-dp-25/
#include <iostream>
using namespace std;

bool isSubsetPossible(int arr[], int n, int sum) {
bool t[n + 1][sum + 1]; // DP - matrix
// initialization
// here we are setting 1st row and 1st column
// i denotes the size of the array
// j denotes the target sum (subset sum)
for (int i = 0; i <= n; i++) { // itereate as long it is less then length of the array
for (int j = 0; j <= sum; j++) {
if (i == 0)// when array(i) is empty than there is no meaning of sum of elements so return false
t[i][j] = false;
if (j == 0) // when sum(j) is zero and there is always a chance of empty subset so return it as true;
t[i][j] = true;
}
}
// start from 1 since 1st row and column is already considerd
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (arr[i - 1] <= j)
// after taking and element substract from the (sum) i.e -> in {3,8} 3 is taken then we want 11-3=8in the array
t[i][j] = t[i - 1][j - arr[i - 1]] || t[i - 1][j]; // either take or(||) do not take
else // if sum is less than array size just leave and increment
t[i][j] = t[i - 1][j];
}
}

return t[n][sum]; // at last return T/F
}

int main() {
int n; cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
int sum; cin >> sum;

isSubsetPossible(arr, n, sum) ? cout << "Yes\n" : cout << "No\n";
return 0;
}
/*
Complexity Analysis:
Time Complexity: O(sum*n), where sum is the ‘target sum’ and ‘n’ is the size of array.
Auxiliary Space: O(sum*n), as the size of 2-D array is sum*n.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// https://www.geeksforgeeks.org/partition-problem-dp-18/
#include <bits/stdc++.h>
using namespace std;

bool isSubsetPossible(int arr[], int n, int sum) {
bool t[n + 1][sum + 1]; // DP - matrix
// initialization
// here we are setting 1st row and 1st column
// i denotes the size of the array
// j denotes the target sum (subset sum)
for (int i = 0; i <= n; i++) { // itereate as long it is less then length of the array
for (int j = 0; j <= sum; j++) {
if (i == 0)// when array(i) is empty than there is no meaning of sum of elements so return false
t[i][j] = false;
if (j == 0) // when sum(j) is zero and there is always a chance of empty subset so return it as true;
t[i][j] = true;
}
}
// start from 1 since 1st row and column is already considerd
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (arr[i - 1] <= j)
// after taking and element substract from the (sum) i.e -> in {3,8} 3 is taken then we want 11-3=8in the array
t[i][j] = t[i - 1][j - arr[i - 1]] || t[i - 1][j]; // either take or(||) do not take
else // if sum is less than array size just leave and increment
t[i][j] = t[i - 1][j];
}
}

return t[n][sum]; // at last return T/F
}

bool EqualSumPartitionPossible(int arr[], int n) {
int sum = 0; // sum of all elements of arr
for (int i = 0; i < n; i++) // take the sum of array
sum += arr[i];

if (sum % 2 != 0) // if sum is odd --> not possible to make equal partitions
return false;

return isSubsetPossible(arr, n, sum / 2); // when even divide sum of array into two part and apply subset sum
}

int main() {
int n; cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];

EqualSumPartitionPossible(arr, n) ? cout << "YES\n" : cout << "NO\n";
return 0;
}

/*
Time Complexity: O(sum * n)
Auxiliary Space: O(sum)
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// https://www.geeksforgeeks.org/count-of-subsets-with-sum-equal-to-x/
#include <bits/stdc++.h>
using namespace std;

int CountSubsets(int arr[], int n, int sum) {
int t[n + 1][sum + 1]; // DP - matrix
// initialization
// here we are setting 1st row and 1st column
// i denotes the size of the array
// j denotes the target sum (subset sum)
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= sum; j++) {
if (i == 0) // when array(i) is empty than there is no meaning of sum of elements so return count of subset as 0;
t[i][j] = 0;
if (j == 0) // when sum(j) is zero and there is always a chance of empty subset so return count as 1;
t[i][j] = 1;
}
}

for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (arr[i - 1] <= j) // when element in the list is less then target sum
t[i][j] = t[i - 1][j - arr[i - 1]] + t[i - 1][j]; // either exclude or inxlude and add both of them to get final count
else
t[i][j] = t[i - 1][j]; // exclude when element in the list is greater then the sum
}
}

return t[n][sum]; // finally return the last row and last column element
}

signed main() {
int n; cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
int sum; cin >> sum;

cout << CountSubsets(arr, n, sum) << endl;
return 0;
}

/*
Time Complexity: O(sum*n), where the sum is the ‘target sum’ and ‘n’ is the size of the array.
Auxiliary Space: O(sum*n), as the size of the 2-D array, is sum*n.
*/
Loading

0 comments on commit 925e740

Please sign in to comment.