Professional Documents
Culture Documents
0
0
#include<stdio.h>
#include<stdlib.h>
int b[100];
int bruteforce(int p[], int w[], int size, int Weight, int val)
int i,l;
for (i = 0; ; i++) {
int j = size;
int tempWeight = 0;
int tempValue = 0;
int k;
k = 1;
A[j] += k;
k = A[j] / 2;
A[j] = A[j] % 2;
if (k) break;
if (A[k] == 1) {
}
if (tempValue > val && tempWeight <= Weight) {
val = tempValue;
for(l=0;l<size;l++)
b[l]=A[l];
free(A);
return val;
int main()
int m,n,i,j,v[20],w[20],a[100],knap[100],k;
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&w[i]);
printf("Enter Profits:");
for (i=0;i<n;i++)
scanf("%d",&v[i]);
printf("Enter capacity:");
scanf("%d",&m);
k=bruteforce(v,w,n,m,k);
printf("%d\n",k);
for(i=0;i<n;i++)
printf("%d\t",b[i]);
}
}
Fractional Knapsack using Greedy Technique
#include<time.h>
#include<conio.h>
int i,j;
y=capacity;
totalprofit=0;
x[i]=0.0;
if(weight[i] > y)
break;
else
x[i]=1.0;
totalprofit=totalprofit+profit[i];
y=y-weight[i];
if(i < n)
x[i]=y/weight[i];
totalprofit=totalprofit+(x[i]*profit[i]);
int main()
int n;
time_t start,stop;
float capacity;
int i,j;
scanf("%d", &n);
scanf("%f", &capacity);
scanf("%f", &profit[i]);
scanf("%f", &weight[i]);
ratio[i]=profit[i]/weight[i];
start=time(NULL);
t1=ratio[i];
ratio[i]=ratio[j];
ratio[j]=t1;
t2=weight[i];
weight[i]=weight[j];
weight[j]=t2;
t3=profit[i];
profit[i]=profit[j];
profit[j]=t3;
knapsack(capacity,n,weight,profit);
stop=time(NULL);
getch();
}
0/1 knapsack using Branch and Bound Technique
#include <bits/stdc++.h>
// value of Item
struct Item
float weight;
int value;
};
// tree
struct Node
// in arr[]
// of this node/
float weight;
};
// 0 as expected bound
if (u.weight >= W)
return 0;
// item index
int j = u.level + 1;
// condition
totweight += arr[j].weight;
profit_bound += arr[j].value;
j++;
if (j < n)
arr[j].weight;
return profit_bound;
// weight.
queue<Node> Q;
Node u, v;
u.level = -1;
u.profit = u.weight = 0;
Q.push(u);
int maxProfit = 0;
while (!Q.empty())
// Dequeue a node
u = Q.front();
Q.pop();
if (u.level == -1)
v.level = 0;
if (u.level == n-1)
continue;
v.level = u.level + 1;
// update maxprofit
maxProfit = v.profit;
// Get the upper bound on profit to decide
// consideration
Q.push(v);
v.weight = u.weight;
v.profit = u.profit;
Q.push(v);
return maxProfit;
int main()
return 0;
Output :
#include<stdio.h>
#include<stdlib.h>
return (a>b?a:b);
int i,j;
for (i=0;i<=n;i++)
for (j=0;j<=m;j++)
if (i==0||j==0)
{
v[i][j]=0;
else if (w[i]>j)
v[i][j]=v[i-1][j];
else
v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
printf("%d\t",v[i][j]);
printf("\n");
int i,j,x[20];
if (v[n][m]==0)
printf("Not possible!");
return;
for (i=0;i<n;i++)
x[i]=0;
i=n;
j=m;
printf("Objects selected: ");
if (v[i][j]!=v[i-1][j])
x[i]=1;
printf("\n%d",i);
j=j-w[i];
i=i-1;
printf("\n");
int main()
int m,n,i,j,p[20],w[20],v[20][20];
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("Enter Profits:");
for (i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("Enter capacity:");
scanf("%d",&m);
knap(n,m,w,v,p);
optimal(n,m,w,v);