-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26.greedy_01_knapsack.cpp
69 lines (57 loc) · 1.53 KB
/
26.greedy_01_knapsack.cpp
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
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int weight[]={10,20,30};
int prices[]={60,100,120};
int maximum(int a, int b){return a>b? a : b;}
void display(int arr[],int items){
for(int i=0;i<items;i++){
cout<<arr[i]<<" ";
}
cout<<"\n";
}
int knapRecursive(int items, int knapsackWeight){
int i;
int max=0;
if(knapsackWeight==0 || items==0){
return 0;
}
if(weight[items-1]>knapsackWeight){ // do not pick
return knapRecursive(items-1,knapsackWeight);
}
else{
return maximum(prices[items-1]+
knapRecursive(items-1,knapsackWeight-weight[items-1]),
knapRecursive(items-1,knapsackWeight));
}
}
int knapIterative(int items, int W){
int i, j;
int k[items + 1][W + 1];
for (i = 0; i <= items; i++){
for (j = 0; j <= W; j++){
if (i == 0 || j == 0){
k[i][j] = 0;
}
else if(weight[i - 1]<=j){
k[i][j] = max(prices[i - 1] + k[i-1][j-weight[i-1]], k[i-1][j]);
}
else{
k[i][j]=k[i-1][j];
}
}
}
return k[items][W];
}
int main(){
int items=3;
int knapsackWeight=50;
cout<<"weight: ";
display(weight,items);
cout<<"prices: ";
display(prices,items);
cout<<knapRecursive(items,knapsackWeight)<<"\n";
cout<<knapIterative(items,knapsackWeight);
return 0;
}