-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack_Greedy.cpp
More file actions
111 lines (99 loc) · 2.02 KB
/
Knapsack_Greedy.cpp
File metadata and controls
111 lines (99 loc) · 2.02 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
#include<stdio.h>
double in[10];
int ind[10];
double P[10],W[10];
void Insertion_Sort(double ratio[] , double profit[] , double weight[], int n)
{
double k=0.0,p=0.0,q=0.0;
int i,pos;
for(i=1 ; i<n ; i++)
{
k = ratio[i];
p = profit[i];
q = weight[i];
pos = i;
while(pos>0 && k > ratio[pos-1])
{
ratio[pos] = ratio[pos-1];
profit[pos] = profit[pos-1];
weight[pos] = weight[pos-1];
pos = pos -1;
}
ratio[pos] = k;
profit[pos] = p;
weight[pos] = q;
}
}
void GreedyStrategy(double* P,double* W,int n)
{
double ratio[10]={0.0};
int i,j,temp;
for (i = 0; i < n; i++)
{
ratio[i] = P[i] / W[i];
}
Insertion_Sort(ratio,P,W,n);
printf("\n\n");
printf("Ratio array is:\n");
for(i=0;i<n;i++)
printf("%lf\t",ratio[i]);
printf("\n\n");
printf("Profit array is:\n");
for(i=0;i<n;i++)
printf("%lf\t",P[i]);
printf("\n\n");
printf("Weight array is:\n");
for(i=0;i<n;i++)
printf("%lf\t",W[i]);
printf("\n\n");
}
void KnapSack(int n,double* pro,double* wei,int capacity)
{
int i,j,u;
double x[10],tot=0,c;
c=capacity;
for(i=0;i<n;i++)
x[i]=0.0;
for(i=0;i<n;i++)
{
if(c<wei[i])
break;
else
{
x[ind[i]]=1.0;
tot=tot+pro[i];
c=c-wei[i];
}
}
if(i<n)
{
x[ind[i]]=c/wei[i];
}
tot=tot+(x[ind[i]]*pro[i]);
printf("The solution set is:\n");
for(i=0;i<n;i++)
printf("%lf\t",x[i]);
printf("\nprofit:%lf",tot);
}
int main()
{
int i,j,n,c,choice;
printf("enter no of elements:\n");
scanf("%d",&n);
printf("enter profit :\n");
for(i=0;i<n;i++)
{
scanf("%lf",&P[i]);
ind[i]=i;
}
printf("enter weight:\n");
for(i=0;i<n;i++)
scanf("%lf",&W[i]);
for(i=0;i<n;i++)
in[i]=W[i];
printf("enter space :\n");
scanf("%d",&c);
GreedyStrategy(P,W,n);
KnapSack(n,P,W,c);
return 0;
}