From 5305eda7f96200966d28f571cc7a1834916a0ed9 Mon Sep 17 00:00:00 2001 From: sweety98 Date: 2017年12月13日 23:26:13 +0530 Subject: [PATCH 1/2] Java program for 0-1 knapsack --- .../0-1Knapsack/knapsack.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Competitive Coding/Dynamic Programming/0-1Knapsack/knapsack.java diff --git a/Competitive Coding/Dynamic Programming/0-1Knapsack/knapsack.java b/Competitive Coding/Dynamic Programming/0-1Knapsack/knapsack.java new file mode 100644 index 000000000..c7bae600d --- /dev/null +++ b/Competitive Coding/Dynamic Programming/0-1Knapsack/knapsack.java @@ -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 Date: 2017年12月30日 12:05:07 +0530 Subject: [PATCH 2/2] Create readme.md Created readme.md for 0-1 explaining Knapsack time complexity using dynamic programming. --- .../Dynamic Programming/0-1Knapsack/readme.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Competitive Coding/Dynamic Programming/0-1Knapsack/readme.md diff --git a/Competitive Coding/Dynamic Programming/0-1Knapsack/readme.md b/Competitive Coding/Dynamic Programming/0-1Knapsack/readme.md new file mode 100644 index 000000000..7e82b11cc --- /dev/null +++ b/Competitive Coding/Dynamic Programming/0-1Knapsack/readme.md @@ -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 によって変換されたページ (->オリジナル) /