Java Program to Solve the Knapsack Problem

«
»
This is a Java Program to Implement Knapsack Algorithm. This algorithm is used to solve knapsack problem : Given a set of items, each with a mass 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.

Here is the source code of the Java Program to Implement Knapsack Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to Implement Knapsack Algorithm
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class Knapsack **/
  8. public class Knapsack
  9. {
  10.     public void solve(int[] wt, int[] val, int W, int N)
  11.     {
  12.         int NEGATIVE_INFINITY = Integer.MIN_VALUE;
  13.         int[][] m = new int[N + 1][W + 1];
  14.         int[][] sol = new int[N + 1][W + 1];
  15.  
  16.         for (int i = 1; i <= N; i++)
  17.         {
  18.             for (int j = 0; j <= W; j++)
  19.             {
  20.                 int m1 = m[i - 1][j];
  21.                 int m2 = NEGATIVE_INFINITY; 
  22.                 if (j >= wt[i])
  23.                     m2 = m[i - 1][j - wt[i]] + val[i];
  24.                 /** select max of m1, m2 **/
  25.                 m[i][j] = Math.max(m1, m2);
  26.                 sol[i][j] = m2 > m1 ? 1 : 0;
  27.             }
  28.         }        
  29.         /** make list of what all items to finally select **/
  30.         int[] selected = new int[N + 1];
  31.         for (int n = N, w = W; n > 0; n--)
  32.         {
  33.             if (sol[n][w] != 0)
  34.             {
  35.                 selected[n] = 1;
  36.                 w = w - wt[n];
  37.             }
  38.             else
  39.                 selected[n] = 0;
  40.         }
  41.         /** Print finally selected items **/
  42.         System.out.println("\nItems selected : ");
  43.         for (int i = 1; i < N + 1; i++)
  44.             if (selected[i] == 1)
  45.                 System.out.print(i +" ");
  46.         System.out.println();
  47.     }
  48.     /** Main function **/
  49.     public static void main (String[] args) 
  50.     {
  51.         Scanner scan = new Scanner(System.in);
  52.         System.out.println("Knapsack Algorithm Test\n");
  53.         /** Make an object of Knapsack class **/
  54.         Knapsack ks = new Knapsack();
  55.  
  56.         System.out.println("Enter number of elements ");
  57.         int n = scan.nextInt();
  58.  
  59.         int[] wt = new int[n + 1];
  60.         int[] val = new int[n + 1];
  61.  
  62.         System.out.println("\nEnter weight for "+ n +" elements");
  63.         for (int i = 1; i <= n; i++)
  64.             wt[i] = scan.nextInt();
  65.         System.out.println("\nEnter value for "+ n +" elements");
  66.         for (int i = 1; i <= n; i++)
  67.             val[i] = scan.nextInt();
  68.  
  69.         System.out.println("\nEnter knapsack weight ");
  70.         int W = scan.nextInt();
  71.  
  72.         ks.solve(wt, val, W, n);
  73.     }
  74. }

advertisement
Knapsack Algorithm Test
 
Enter number of elements
5
 
Enter weight for 5 elements
50 10 20 40 30
 
Enter value for 5 elements
300 60 90 100 240
 
Enter knapsack weight
60
 
Items selected :
2 3 5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.