Skip to content

Commit 0430099

Browse files
authored
Unbounded knapsack added
Unbounded Knaspsack Coin Change I Coin Chnage II
1 parent 24b1d76 commit 0430099

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int t[100][100];
4+
5+
// Rod Cutting Problem
6+
int Unbknapsack(int *wt, int *val, int W, int n) {
7+
8+
for (int i = 0; i <= W; i++) t[0][i] = 0;
9+
for (int i = 0; i <= n; i++) t[i][0] = 0;
10+
11+
12+
for (int i = 1; i <= n; i++) {
13+
for (int j = 1; j <= W; j++) {
14+
15+
if (wt[i - 1] <= j) {
16+
t[i][j] = max(val[i - 1] + t[i][j - wt[i - 1]] , t[i - 1][j]);
17+
}
18+
else {
19+
t[i][j] = t[i - 1][j];
20+
21+
}
22+
}
23+
24+
}
25+
return t[n][W];
26+
}
27+
28+
int main() {
29+
30+
#ifndef ONLINE_JUDGE
31+
freopen("input.txt", "r", stdin);
32+
freopen("output.txt", "w", stdout);
33+
#endif
34+
35+
int wt[] = {4, 9, 2, 2};
36+
int val[] = {1, 2, 5, 4};
37+
int W = 10;
38+
int n = sizeof(val) / sizeof(int);
39+
cout << Unbknapsack(wt, val, W, n) << endl;
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int t[100][100];
4+
5+
// maximum number of ways
6+
7+
int Unbknapsack(int *wt, int W, int n) {
8+
9+
for (int i = 0; i <= W; i++) t[0][i] = 0;
10+
for (int i = 0; i <= n; i++) t[i][0] = 1;
11+
12+
13+
for (int i = 1; i <= n; i++) {
14+
for (int j = 1; j <= W; j++) {
15+
16+
if (wt[i - 1] <= j) {
17+
t[i][j] = t[i][j - wt[i - 1]] + t[i - 1][j];
18+
}
19+
else {
20+
t[i][j] = t[i - 1][j];
21+
22+
}
23+
}
24+
25+
}
26+
return t[n][W];
27+
}
28+
29+
int main() {
30+
31+
#ifndef ONLINE_JUDGE
32+
freopen("input.txt", "r", stdin);
33+
freopen("output.txt", "w", stdout);
34+
#endif
35+
36+
int coins[] = {1, 2, 3};
37+
int W = 5;
38+
int n = sizeof(coins) / sizeof(int);
39+
cout << Unbknapsack(coins, W, n) << endl;
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int t[100][100];
4+
5+
// minimum number of coins to make sum
6+
7+
int Unbknapsack(int *wt, int W, int n) {
8+
9+
for (int i = 0; i <= n; i++) t[i][0] = 0;
10+
for (int i = 0; i <= W; i++) t[0][i] = INT_MAX - 1;
11+
for (int i = 1; i <= W; i++) {
12+
if (i % wt[0] == 0) t[1][i] = i / wt[0];
13+
else t[1][i] = INT_MAX - 1;
14+
}
15+
16+
for (int i = 2; i <= n; i++) {
17+
for (int j = 1; j <= W; j++) {
18+
19+
if (wt[i - 1] <= j) {
20+
t[i][j] = min( 1 + t[i][j - wt[i - 1]] , t[i - 1][j]);
21+
}
22+
else {
23+
t[i][j] = t[i - 1][j];
24+
}
25+
}
26+
27+
}
28+
return t[n][W];
29+
30+
}
31+
32+
int main() {
33+
34+
#ifndef ONLINE_JUDGE
35+
freopen("input.txt", "r", stdin);
36+
freopen("output.txt", "w", stdout);
37+
#endif
38+
39+
int coins[] = {1, 2, 3};
40+
int W = 5;
41+
int n = sizeof(coins) / sizeof(int);
42+
cout << Unbknapsack(coins, W, n) << endl;
43+
44+
}

0 commit comments

Comments
 (0)