Skip to content

Instantly share code, notes, and snippets.

@sdpatil
Created August 13, 2017 23:22
Show Gist options
  • Save sdpatil/c0a87b67273b5c2685cc5c79e5d09886 to your computer and use it in GitHub Desktop.
Save sdpatil/c0a87b67273b5c2685cc5c79e5d09886 to your computer and use it in GitHub Desktop.
The knapsack problem
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Problem: Write a program for knapsack problem that selects a subset of items that has maximum value and satisfies
* the weight constraint. All items have integer weights and value
*/
public class KnapsackProblem {
public static class Item {
public Integer value;
public Integer weight;
public Item(Integer weight, Integer value) {
this.value = value;
this.weight = weight;
}
}
public static void main(String[] argv) {
List<Item> itemList = Arrays.asList(
new Item(1, 1),
new Item(3, 4),
new Item(4, 5),
new Item(5, 7)
);
KnapsackProblem problem = new KnapsackProblem();
int result = problem.optimumSubjectToCapacity(itemList, 7);
System.out.println("Result " + result);
}
/**
If you had 4 items (weight:1,value:1),(weight:3,value:4),(weight:4,value:5),(weight:5,value:7) and want to pickup
max weight of 7
Create DP table of [numberofItems+1][capacity+1]
Now check what would get more value, choosing this weight or ignoring this weight using following formula
if(column < weight[row-1])
dptable[row][column] = dptable[row-1][column]
else
dptable[row][column] = Math.max( dpTable[row-1][column] ,without this weight
value[row-1] + dptable[row-1][column-weight[row-1] with this weight
)
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 1, 1, 1, 1]
[0, 1, 1, 4, 5, 5, 5, 5]
[0, 1, 1, 4, 5, 6, 6, 9]
[0, 1, 1, 4, 5, 7, 8, 9]
*/
public int optimumSubjectToCapacity(List<Item> itemList, int capacity) {
int[][] value = new int[itemList.size() + 1][capacity + 1];
for (int row = 0; row <= itemList.size(); row++) {
for (int column = 0; column <= capacity; column++) {
if (row == 0 || column == 0) {
value[row][column] = 0;
} else {
if (column < itemList.get(row-1).weight) {
value[row][column] = value[row - 1][column];
} else {
int withoutItem = value[row - 1][column];
int remainingItemValue = column - itemList.get(row-1).weight;
int withItem = itemList.get(row-1).value + value[row - 1][remainingItemValue];
value[row][column] = Math.max(withoutItem, withItem);
}
}
}
}
List<Item> pickedItemList = new ArrayList<>();
return value[itemList.size() - 1][capacity];
}
private void printDPTable(int[][] dpTable) {
for (int[] row : dpTable) {
System.out.println(Arrays.toString(row));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment