Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cc26632

Browse files
Merge pull request #200 from ManognaMutyala/master
Java program for 0-1 knapsack
2 parents 1509b3d + 41782a2 commit cc26632

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
3+
4+
class Knapsack
5+
{
6+
7+
// A function to return maximum of two integers
8+
static int maximum(int a, int b)
9+
{
10+
if(a>b)
11+
return a;
12+
else
13+
return b;
14+
15+
}
16+
17+
// Returns the maximum value that can be put in a knapsack of capacity W
18+
static int knapSack(int W, int wt[], int val[], int n)
19+
{
20+
int i, w;
21+
int K[][] = new int[n+1][W+1];
22+
23+
// Build table K[][] in bottom up manner
24+
for (i = 0; i <= n; i++)
25+
{
26+
for (w = 0; w <= W; w++)
27+
{
28+
if (i==0 || w==0)
29+
K[i][w] = 0;
30+
else if (wt[i-1] <= w)
31+
K[i][w] = maximum(val[i-1] + K[i-1][w-wt[i-1]] , K[i1][w]);
32+
else
33+
K[i][w] = K[i-1][w];
34+
}
35+
}
36+
37+
Return K[n][W];
38+
}
39+
40+
41+
42+
public static void main(String args[])
43+
{
44+
45+
Scanner s=new Scanner(System.in);
46+
System.out.println("Enter number of objects");
47+
int n=s.nextInt();
48+
int val[]=new int[n];
49+
int wt[] = new int[n];
50+
System.out.println("Enter values of objects");
51+
52+
53+
for(int i=0;i<n;i++)
54+
{
55+
val[i]=s.nextInt();
56+
}
57+
58+
System.out.println("Enter weights of object");
59+
for(int i=0;i<n;i++)
60+
{
61+
wt[i]=s.nextInt();
62+
}
63+
64+
65+
System.out.println("Enter the maximum weight");
66+
int W = s.nextInt();
67+
System.out.println(knapSack(W, wt, val, n));
68+
}
69+
}
70+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 0-1 Knapsack
2+
The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items.
3+
4+
**Time Complexity**
5+
Time Complexity: O(nW) where n is the number of items and W is the capacity of knapsack.
6+
### Applications
7+
Knapsack problems appear in real-world decision-making processes in a wide variety of fields, such as finding the least wasteful way to cut raw materials,[4] selection of investments and portfolios selection of assets for asset-backed securitization and generating keys for the Merkle–Hellman and other knapsack cryptosystems

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /