0% found this document useful (0 votes)
18 views

Experiment 4

The document describes an experiment to implement a program for the 0-1 knapsack problem using dynamic programming. It provides background on the knapsack problem and 0-1 knapsack, describes a dynamic programming approach and pseudocode for the algorithm, and includes C++ source code implementing the solution.

Uploaded by

Tanishka Mayekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Experiment 4

The document describes an experiment to implement a program for the 0-1 knapsack problem using dynamic programming. It provides background on the knapsack problem and 0-1 knapsack, describes a dynamic programming approach and pseudocode for the algorithm, and includes C++ source code implementing the solution.

Uploaded by

Tanishka Mayekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment 4

Aim: Implementation of a program for Knapsack problem.


Required Software/ Software Tool
- Windows Operating System
-C/C++

Theory:
In 0-1 Knapsack, items cannot be broken which means the thief should take the
item as a whole or should leave it. This is reason behind calling it as 0-1 Knapsack.
Hence, in case of 0-1 Knapsack, the value of xi can be either 0 or 1, where other
constraints remain the same.0-1 Knapsack cannot be solved by Greedy approach.
A greedy approach does not ensure an optimal solution. In many instances, Greedy
approach may give an optimal solution. To solve 0-1 Knapsack, Dynamic
Programming approach is required.
Problem Statement
A thief is robbing a store and can carry a maximal weight of W into his knapsack.
There are n items and weight of ith item is wi and the profit of selecting this item
is pi. What items should the thieftake?

Dynamic-Programming Approach
Let ibe the highest-numbered item in an optimal solution S for W dollars. Then S'
= S - {i} is an optimal solution for W - wi dollars and the value to the solution S is
Vi plus the value of the sub- problem.

We can express this fact in the following formula: define c[i, w] to be the solution for
items 1,2,
… ,i and the maximum weight w.

The algorithm takes the following inputs

The maximum weight W

The number of items n

The two sequences v = <v1, v2, …, vn> and w = <w1, w2, …, wn>

Dynamic-0-1-knapsack (v, w, n, W)
for w = 0 to W do
c[0, w] = 0
fori = 1 to n do
c[i, 0] = 0
for w = 1 to W do
ifwi ≤ w then
if vi + c[i-1, w-wi] then
c[i, w] = vi + c[i-1, w-wi]
else c[i, w] = c[i-1, w]
else
c[i, w] = c[i-1, w]
The set of items to take can be deduced from the table, starting at c[n, w]and tracing
backwards where the optimal values came from.

If c[i, w] = c[i-1, w], then item i is not part of the solution, and we continue
tracing with c[i-1, w]. Otherwise, item i is part of the solution, and we continue
tracing with c[i-1, w-W].
Analysis
This algorithm takes θ(n, w) times as table c has (n + 1).(w + 1) entries, where
each entry requires θ(1) time to compute.

Implementing the Solution Writing Source Code:


#include<stdio.h> if(v[i][j]!=v[i-1][j]){
int w[10],p[10],v[10][10],n,i,j,cap; x[i]=1;
int x[10]={0}; j=j-w[i];
int max(int i,int j){ i--;
return ((i>j)?i:j); }
} else
int knap(int i,int j){ i--;
int value; }
if(v[i][j]<0){ printf("object included are \n ");
if(j<w[i]) printf("Sl.no\tweight\tprofit\n");
value=knap(i-1,j); for(i=1;i<=n;i++)
else if(x[i])
value=max(knap(i-1,j),p[i]+knap(i-1,j- printf("%d\t%d\t%d\n",++count,w[i],p[i]);
w[i])); printf("Total profit = %d\n",profit);
v[i][j]=value; }
}
return(v[i][j]);
}
int main(){
int profit,count=0;
printf("\nEnter the number of objects ");
scanf("%d",&n);
printf("Enter the profit and weights of the
elements \n ");
for(i=1;i<=n;i++){
printf("\nEnter profit and weight For object
no %d :",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity ");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0){
Input

Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy