-
Notifications
You must be signed in to change notification settings - Fork 162
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
Competitive Coding/Dynamic Programming/0-1Knapsack/knapsack.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.