Showing posts with label 0/1 Knapsack – Dynamic Programming. Show all posts
Showing posts with label 0/1 Knapsack – Dynamic Programming. Show all posts

Thursday, April 14, 2011

0/1 Knapsack – Dynamic Programming


import  java.util.*;
class knapsack_dynamic
{
                int n,p[],w[],capacity;
                void read()
                {
                                Scanner k=new Scanner(System.in);
                                System.out.println("Enter no. of objects:");
                                n=k.nextInt();
                                System.out.println("Enter capacity of knapsack:");
                                capacity=k.nextInt();
                                //create p and w array of size n+1
                               
                                p=new int[n+1];
                                w=new int[n+1];
                               
                                //read array w and p from 1 to n
                                for(int i=1;i<=n;i++)
                                {
                                                System.out.println("Enter weight of object " +i);
                                                w[i]=k.nextInt();
                                                System.out.println("Enter profit of object "+i);
                                                p[i]=k.nextInt();
                                                }
                                }//end read
                               
                                void fill()