From 3082a46c77abe5532d1eb9513d24e80e44057428 Mon Sep 17 00:00:00 2001 From: Abhishek Nalla Date: Thu, 14 Dec 2017 13:25:29 +0530 Subject: [PATCH 1/2] Added DP problems --- .../Dynamic Programming/cutRod/Readme.md | 15 +++++ .../Dynamic Programming/cutRod/cutRod.c | 39 +++++++++++++ .../Dynamic Programming/minCostPath/Readme.md | 20 +++++++ .../minCostPath/minCostPath.cpp | 55 +++++++++++++++++++ .../Dynamic Programming/subsetSum/Readme.md | 21 +++++++ .../subsetSum/subsetSum.cpp | 39 +++++++++++++ .../Dynamic Programming/uglyNumbers/Readme.md | 24 ++++++++ .../uglyNumbers/uglyNumbers.cpp | 24 ++++++++ 8 files changed, 237 insertions(+) create mode 100644 Competitive Coding/Dynamic Programming/cutRod/Readme.md create mode 100644 Competitive Coding/Dynamic Programming/cutRod/cutRod.c create mode 100644 Competitive Coding/Dynamic Programming/minCostPath/Readme.md create mode 100644 Competitive Coding/Dynamic Programming/minCostPath/minCostPath.cpp create mode 100644 Competitive Coding/Dynamic Programming/subsetSum/Readme.md create mode 100644 Competitive Coding/Dynamic Programming/subsetSum/subsetSum.cpp create mode 100644 Competitive Coding/Dynamic Programming/uglyNumbers/Readme.md create mode 100644 Competitive Coding/Dynamic Programming/uglyNumbers/uglyNumbers.cpp diff --git a/Competitive Coding/Dynamic Programming/cutRod/Readme.md b/Competitive Coding/Dynamic Programming/cutRod/Readme.md new file mode 100644 index 000000000..ee35b19c0 --- /dev/null +++ b/Competitive Coding/Dynamic Programming/cutRod/Readme.md @@ -0,0 +1,15 @@ +Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6) + +length | 1 2 3 4 5 6 7 8 +-------------------------------------------- +price | 1 5 8 9 10 17 17 20 + +1) Optimal Substructure: +We can get the best price by making a cut at different positions and comparing the values obtained after a cut. We can recursively call the same function for a piece obtained after a cut. + +Let cutRoad(n) be the required (best possible price) value for a rod of lenght n. cutRod(n) can be written as following. + +cutRod(n) = max(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1} + +2) Overlapping Subproblems +Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array val[] in bottom up manner. diff --git a/Competitive Coding/Dynamic Programming/cutRod/cutRod.c b/Competitive Coding/Dynamic Programming/cutRod/cutRod.c new file mode 100644 index 000000000..0b0aa2b1f --- /dev/null +++ b/Competitive Coding/Dynamic Programming/cutRod/cutRod.c @@ -0,0 +1,39 @@ +#include +#include + +int max(int a, int b) + { + return (a > b)? a : b; + } + +int DPcutRod(int price[], int n) +{ + int val[n+1]; + val[0] = 0; + int i, j; + for (i = 1; i<=n; i++) + { + int max_val = INT_MIN; + for (j = 0; j < i; j++) + max_val = max(max_val, price[j] + val[i-j-1]); + val[i] = max_val; + } + + return val[n]; +} + +int main() +{ + int n; + printf("Enter rod length\n"); + scanf("%d",&n); + int arr[n],i; + printf("Enter prices for lengths 1 to n\n"); + for(i=0;i +#include + +using namespace std; + +int main(){ + std::ios::sync_with_stdio(false); + /* + We can easily observe that the optimum solution of the minimum + cost at any row i is only dependent on the cost at row i-1 and + the current row elements. Thus if we only store these two arrays + of length c, We have reduced space from n*m to 2*m. + */ + + int i, j, r, c; + printf("Enter no of rows and no of cols\n"); + cin>>r>>c; //take input of the rows and columns from user. + int cost[c], dp[c]; + int x; + /* + Since for the first row the minimum cost is the only cost possible + required to get there, we directly calculate it. + */ + printf("Enter a R X C array\n"); + + for(i=0; i>cost[i]; + if(i>0) cost[i] = cost[i] + cost[i-1]; + } + /* + For the subsequent rows, the optimised cost for each cell in the + previous row has been stored in the array cost[] and the optimised + cost for the present row is stored in the array dp[]. + */ + for(i=1; i>x; + if(j==0) + dp[j] = cost[j] + x; + else{ + dp[j] = x + min(dp[j-1], min(cost[j-1], cost[j])); + } + } + /* + After dp[] has been found entirely, we copy its elements to + cost[] and continue the iteration. + */ + for(j=0; (j 0 and n == 0 +isSubsetSum(set, n, sum) = true, if sum == 0 + +We can solve the problem in Pseudo-polynomial time using Dynamic programming. We create a boolean 2D table subset[][] and fill it in bottom up manner. The value of subset[i][j] will be true if there is a subset of set[0..j-1] with sum equal to i., otherwise false. Finally, we return subset[sum][n] diff --git a/Competitive Coding/Dynamic Programming/subsetSum/subsetSum.cpp b/Competitive Coding/Dynamic Programming/subsetSum/subsetSum.cpp new file mode 100644 index 000000000..3dcf20c08 --- /dev/null +++ b/Competitive Coding/Dynamic Programming/subsetSum/subsetSum.cpp @@ -0,0 +1,39 @@ +#include + +bool DP(int set[], int n, int sum) +{ + bool subset[n+1][sum+1]; + for (int i = 0; i <= n; i++) + subset[i][0] = true; + for (int i = 1; i <= sum; i++) + subset[0][i] = false; + 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] || + subset[i - 1][j-set[i-1]]; + } + } + return subset[n][sum]; +} + +int main() +{ + int n; + int sum; + printf("Enter no of elements of array and desired sum\n"); + scanf("%d%d",&n,&sum); + int i,a[n]; + printf("Enter the array\n"); + for(i=0;i +using namespace std; +int main() +{ + printf("Enter no of test cases\n"); + int t; cin >> t; + int i2, i3, i5; + i2 = i3 = i5 = 0; + int ugly[500]; + ugly[0] = 1; + for (int i = 1; i < 500; ++i) { + ugly[i] = min(ugly[i2]*2, min(ugly[i3]*3, ugly[i5]*5)); + ugly[i] != ugly[i2]*2 ? : i2++; + ugly[i] != ugly[i3]*3? : i3++; + ugly[i] != ugly[i5]*5 ? : i5++; + } + int n; + while(t--) { + printf("Enter 'n' for the nth ugly number\n"); + cin >> n; + cout << ugly[n-1] << endl; + } + return 0; +} From 8f0483d62dab98e10966c892acb8cb74197f50a4 Mon Sep 17 00:00:00 2001 From: Abhishek Nalla Date: Thu, 14 Dec 2017 15:38:53 +0530 Subject: [PATCH 2/2] Dpcommit 2 --- Competitive Coding/Dynamic Programming/cutRod/cutRod.c | 7 ++++++- .../Dynamic Programming/subsetSum/subsetSum.cpp | 6 ++++++ .../Dynamic Programming/uglyNumbers/uglyNumbers.cpp | 10 +++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Competitive Coding/Dynamic Programming/cutRod/cutRod.c b/Competitive Coding/Dynamic Programming/cutRod/cutRod.c index 0b0aa2b1f..6d7fed571 100644 --- a/Competitive Coding/Dynamic Programming/cutRod/cutRod.c +++ b/Competitive Coding/Dynamic Programming/cutRod/cutRod.c @@ -1,11 +1,15 @@ #include #include +// A utility function to get the maximum of two integers int max(int a, int b) { return (a > b)? a : b; } + + /* Returns the best obtainable price for a rod of length n and + price[] as prices of different pieces */ int DPcutRod(int price[], int n) { int val[n+1]; @@ -14,11 +18,12 @@ int DPcutRod(int price[], int n) for (i = 1; i<=n; i++) { int max_val = INT_MIN; + // Recursively cut the rod in different pieces and compare different + // configurations for (j = 0; j < i; j++) max_val = max(max_val, price[j] + val[i-j-1]); val[i] = max_val; } - return val[n]; } diff --git a/Competitive Coding/Dynamic Programming/subsetSum/subsetSum.cpp b/Competitive Coding/Dynamic Programming/subsetSum/subsetSum.cpp index 3dcf20c08..58de312a1 100644 --- a/Competitive Coding/Dynamic Programming/subsetSum/subsetSum.cpp +++ b/Competitive Coding/Dynamic Programming/subsetSum/subsetSum.cpp @@ -1,12 +1,18 @@ #include +// Returns true if there is a subset of a[] with sun equal to given sum bool DP(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++) diff --git a/Competitive Coding/Dynamic Programming/uglyNumbers/uglyNumbers.cpp b/Competitive Coding/Dynamic Programming/uglyNumbers/uglyNumbers.cpp index e0e6ea1cd..7deef84c9 100644 --- a/Competitive Coding/Dynamic Programming/uglyNumbers/uglyNumbers.cpp +++ b/Competitive Coding/Dynamic Programming/uglyNumbers/uglyNumbers.cpp @@ -6,13 +6,13 @@ int main() int t; cin >> t; int i2, i3, i5; i2 = i3 = i5 = 0; - int ugly[500]; + int ugly[500];// To store ugly numbers ugly[0] = 1; for (int i = 1; i < 500; ++i) { - ugly[i] = min(ugly[i2]*2, min(ugly[i3]*3, ugly[i5]*5)); - ugly[i] != ugly[i2]*2 ? : i2++; - ugly[i] != ugly[i3]*3? : i3++; - ugly[i] != ugly[i5]*5 ? : i5++; + ugly[i] = min(ugly[i2]*2, min(ugly[i3]*3, ugly[i5]*5));//get minimum of possible solutions + ugly[i] != ugly[i2]*2 ? : i2++; // next_mulitple_of_2 = ugly[i2]*2; + ugly[i] != ugly[i3]*3? : i3++; // next_mulitple_of_3 = ugly[i3]*3 + ugly[i] != ugly[i5]*5 ? : i5++; // next_mulitple_of_5 = ugly[i5]*5; } int n; while(t--) {