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

Java program for 0-1 knapsack #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
prateekiiest merged 2 commits into codeIIEST:master from ManognaMutyala:master
Dec 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Competitive Coding/Dynamic Programming/0-1Knapsack/knapsack.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.util.*;


class Knapsack
{

// A function to return maximum of two integers
static int maximum(int a, int b)
{
if(a>b)
return a;
else
return b;

}

// Returns the maximum value that can be put in a knapsack of capacity W
static int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[][] = new int[n+1][W+1];

// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = maximum(val[i-1] + K[i-1][w-wt[i-1]] , K[i1][w]);
else
K[i][w] = K[i-1][w];
}
}

Return K[n][W];
}



public static void main(String args[])
{

Scanner s=new Scanner(System.in);
System.out.println("Enter number of objects");
int n=s.nextInt();
int val[]=new int[n];
int wt[] = new int[n];
System.out.println("Enter values of objects");


for(int i=0;i<n;i++)
{
val[i]=s.nextInt();
}

System.out.println("Enter weights of object");
for(int i=0;i<n;i++)
{
wt[i]=s.nextInt();
}


System.out.println("Enter the maximum weight");
int W = s.nextInt();
System.out.println(knapSack(W, wt, val, n));
}
}

7 changes: 7 additions & 0 deletions Competitive Coding/Dynamic Programming/0-1Knapsack/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 0-1 Knapsack
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.

**Time Complexity**
Time Complexity: O(nW) where n is the number of items and W is the capacity of knapsack.
### Applications
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

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