-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicProgramming.java
More file actions
200 lines (168 loc) · 5.54 KB
/
Copy pathDynamicProgramming.java
File metadata and controls
200 lines (168 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.Arrays;
public class DynamicProgramming {
public static int fibo(int n , int f[]){
if(n == 0 || n == 1){
return n;
}
if(f[n] != 0){
return f[n];
}
f[n] = fibo(n - 1 , f) + fibo(n - 2 , f);
return f[n];
}
public static int fiboTabulation(int n){
int dp[] = new int [n + 1];
dp[1] = 1;
for(int i = 2; i<= n; i++){
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
// Memoization ==> O(n) |||| rec = O(2^n)
public static int countWays(int n , int ways[]){
if(n == 0){
return 1;
}
if(n < 0){
return 0;
}
if(ways[n] != -1){
return ways[n];
}
ways[n] = countWays(n - 1 , ways) + countWays(n - 2 , ways);
return ways[n];
}
public static int countWaysTab(int n){
int dp[] = new int[n + 1];
dp[0] = 1;
for(int i = 1; i<= n; i++){
if(i == 1){
dp[i] = dp[i - 1];
}else{
dp[i] = dp[i - 1] + dp[i - 2];
}
}
return dp[n];
}
public static int zeroOneKnapsack(int val[] , int wt[] , int W , int n , int dp[][]){
if(W == 0 || n == 0){
return 0;
}
if(dp[n][W] != -1){
return dp[n][W];
}
if(W >= wt[n - 1]){ // Valid condition
int ans1 = val[n - 1] + zeroOneKnapsack(val, wt, W - wt[n - 1], n - 1 , dp); // Include
int ans2 = zeroOneKnapsack(val, wt, W, n - 1 , dp); // Exclude
dp[n][W] = Math.max(ans1, ans2);
return dp[n][W];
}else{
dp[n][W] = zeroOneKnapsack(val, wt, W, n - 1 , dp);
return dp[n][W];
}
}
public static void print(boolean dp[][]){
for(int i = 0; i < dp.length; i++){
for(int j = 0; j < dp[0].length; j++){
System.out.print(dp[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
public static int knapsackTab(int val[] , int wt[] , int W){
int n = val.length;
int dp[][] = new int[n + 1][W + 1];
for(int i = 0; i< dp.length; i++){
dp[i][0] = 0;
}
for(int j = 0; j < dp[0].length; j++){
dp[0][j] = 0;
}
for(int i = 1; i < n + 1; i++){
for(int j = 1; j < W + 1; j++){
int v = val[i - 1];
int w = wt[i - 1];
if(w <= j){ // Valid
int incAns = v + dp[i - 1][j - w];
int excAns = dp[i - 1][j];
dp[i][j] = Math.max(incAns, excAns);
}else{
int excAns = dp[i - 1][j];
dp[i][j] = excAns;
}
}
}
// print(dp);
return dp[n][W];
}
public static boolean targetSumSubset(int arr[] , int sum){
int n = arr.length;
boolean dp[][] = new boolean[n + 1][sum + 1];
for(int i = 0; i< n + 1; i++){
// i = items
dp[i][0] = true;
}
for(int i = 1; i< n + 1; i++){
for(int j = 1; j< sum + 1; j++){
int v = arr[i - 1];
if(v <= j && dp[i - 1][j - v] == true){
dp[i][j] = true;
}else if(dp[i - 1][j] == true){
dp[i][j] = true;
}
}
}
print(dp);
return dp[n][sum];
}
public static int unboundedknapsackTab(int val[] , int wt[] , int W){
int n = val.length;
int dp[][] = new int[n + 1][W + 1];
for(int i = 0; i< dp.length; i++){
dp[i][0] = 0;
}
for(int j = 0; j < dp[0].length; j++){
dp[0][j] = 0;
}
for(int i = 1; i < n + 1; i++){
for(int j = 1; j < W + 1; j++){
int v = val[i - 1];
int w = wt[i - 1];
if(w <= j){ // Valid
dp[i][j] = Math.max(val[i - 1] + dp[i][j - wt[i - 1]], dp[i - 1][j]);
}else{
int excAns = dp[i - 1][j];
dp[i][j] = excAns;
}
}
}
// print(dp);
return dp[n][W];
}
public static void main(String[] args) {
// int n = 5;
// int ways[] = new int[n + 1];
// Arrays.fill(ways, -1);
// System.out.println(fibo(n , f));
// System.out.println(fiboTabulation(n));
// System.out.println(countWays(n , ways));
// System.out.println(countWaysTab(n));
int val[] = {15,14,10,45,30};
int wt[] = {2,5,1,3,4};
int W = 7;
// int dp[][] = new int[val.length + 1][W + 1];
// for(int i = 0; i< dp.length; i++){
// for(int j = 0; j < dp[0].length; j++){
// dp[i][j] = -1;
// }
// }
// System.out.println(zeroOneKnapsack(val, wt, W, val.length , dp));
// System.out.println(knapsackTab(val, wt, W));
// int arr[] = {4,2,7,1,3};
// int sum = 5;
// System.out.println(targetSumSubset(arr, sum));
System.out.println(unboundedknapsackTab(val, wt, W));
System.out.println(knapsackTab(val, wt, W));
}
}