-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnapsackMain.java
42 lines (35 loc) · 1.57 KB
/
KnapsackMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* FILENAME : KnapsackMain.java
* Problem Statement:
* Knapsack problem solution using
* (a) Dynamic Programming method (b) Memory Function.
* ------------------------------------------------------------------------------
* AUTHOR : GANESH PAI, Dept. of CS&E, NMAMIT, Nitte
* YEAR : 2021
* E-mail : ganesh.pai@nitte.edu.in
* ------------------------------------------------------------------------------
*/
import java.util.Scanner;
public class KnapsackMain {
public static void main(String[] args) {
Scanner ip = new Scanner(System.in);
//Read weights, values and knapsack capacity
System.out.print("Enter the number of items in the knapsack: ");
int noOfItems = ip.nextInt();
int weight[] = new int[noOfItems + 1];
int value[] = new int[noOfItems + 1];
System.out.println("Enter the weight & value of " + noOfItems + " items");
for(int i = 1; i <= noOfItems; i++)
{
System.out.print(" Item " + i + ": ");
weight[i] = ip.nextInt();
value[i] = ip.nextInt();
}
System.out.print("Enter the Knapsack capacity: ");
int capacity = ip.nextInt();
System.out.println("\nOptimal Knapsack value using Dynamic Approach: " +
new Knapsack(noOfItems, weight, value, capacity).computeDynamicSolution());
System.out.println("Optimal Knapsack value using Memory Function: " +
new Knapsack(noOfItems, weight, value, capacity).computeMemorySolution());
}
}