0 1 knapsack Java Code

     /**
     *
     * @param w weight for items
     * @param v values for items
     * @param i number of items
     * @param W the max weight
     * @return what knapsack/0-1 should return
     */
    static int knapsack(int[]w, int []v, int i, int W){
        if(i < 0) return 0;         if(w[i] > W){
            return knapsack(w,v, i-1, W);
        }else {
            return Math.max(knapsack(w,v, i-1, W), knapsack(w,v, i-1, W-w[i])+ v[i] );
        }
    }
Advertisements

About yadab das
Software Developer{writing,debugging,documenting} source code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: