Java implementation knapsack algorithm (0-1 knapsack Problem)

Source: Internet
Author: User
Tags addall

0-1 questions about Backpacks

Knapsack problem (knapsack Problem) is a np-complete problem of combinatorial Optimization. The problem can be described as: given a set of items, each item has its own weight and price, within a limited total weight, we choose how to make the total price of the item the Highest. The name of the problem comes from how to choose the most suitable item to be placed in a given backpack.

This is the most basic knapsack problem, characterized by: only one piece of each item, you can choose to put or not Put.

Define the state with Sub-question: f[i][v] indicates the maximum value that the first I item can get in a backpack with a capacity of V. The state transfer equation is:

f[i][v]=max{f[i-1][v], f[i-1][v-w[i]]+v[i]}.

1  public classBag {2 3     Static classItem {//Define an item4String id;//Item ID5         intSize = 0;//space occupied by items6         intValue = 0;//Item Value7 8         StaticItem NewItem (String id,intSizeintValue) {9Item item =NewItem ();TenItem.id =id; oneItem.size =size; aItem.value =value; -             returnitem; -         } the  -          publicString toString () { -             return  this. id; -         } +     } -  +     Static classOkbag {//define a packaging method aList<item> Items =NewArraylist<item> ();//collection of items in the bag at  - okbag () { -         } -  -         intGetValue () {//total value of items in package -             intValue = 0; in              for(Item Item:items) { -Value + =item.value; to             } +             returnvalue; -         }; the  *         intGetSize () {//total size of items in package $             intSize = 0;Panax Notoginseng              for(Item Item:items) { -Size + =item.size; the             } +             returnsize; a         }; the  +          publicString toString () { -             returnString.valueof ( this. GetValue ()) + ""; $         } $     } -  -     //alternative items that can be placed in a package the     Staticitem[] sourceitems = {item.newitem ("4th ball", 4, 5), item.newitem ("5th ball", 5, 6), item.newitem ("6th ball", 6, 7) }; -     Static intBagsize = 10;//the space of the packageWuyi     Static intItemCount = sourceitems.length;//Number of items the  -     //Save optimal packaging in all situations the first dimension is the number of items from 0 to itemcount, the second dimension is the parcel size from 0 to Bagsize wu     Staticokbag[][] okbags =NewOkbag[itemcount + 1][bagsize + 1]; -  about     Static voidInit () { $          for(inti = 0; I < Bagsize + 1; i++) { -okbags[0][i] =Newokbag (); -         } -  a          for(inti = 0; I < ItemCount + 1; i++) { +okbags[i][0] =Newokbag (); the         } -     } $  the     Static voiddobag () { the Init (); the          for(intIItem = 1; IItem <= itemCount; iitem++) { the              for(intCurbagsize = 1; Curbagsize <= bagsize; curbagsize++) { -okbags[iitem][curbagsize] =Newokbag (); in                 if(sourceitems[iitem-1].size > Curbagsize) {//the current item is larger than the package space. must not be placed in the Package. theOkbags[iitem][curbagsize]. Items.addall (okbags[iitem-1][curbagsize]. Items); the}Else { about                     intNotincludevalue = Okbags[iitem-1][curbagsize].getvalue ();//the value of the current item pack is not released the                     intFreesize = curbagsize-sourceitems[iitem-1].size;//put the remaining space of the current item pack the                     intIncludevalue = Sourceitems[iitem-1].value + okbags[iitem-1][freesize].getvalue ();//the value of the current item + after putting the current item the remaining package space can put the item the                     if(notincludevalue < Includevalue) {//put a greater value on it. +Okbags[iitem][curbagsize]. Items.addall (okbags[iitem-1][freesize]. Items); -Okbags[iitem][curbagsize]. Items.Add (sourceitems[iitem-1]); the}Else{//otherwise, do not put the current itemBayiOkbags[iitem][curbagsize]. Items.addall (okbags[iitem-1][curbagsize]. Items); the                     } the                 } -  -             } the         } the     } the  the      public Static voidmain (string[] Args) { - bag.dobag (); the          for(inti = 0; I < Bag.itemcount + 1; I++) {//Print all items included in the scheme the              for(intj = 0; J < Bag.bagsize + 1; J + +) { the System.out.print (bag.okbags[i][j]. Items);94             } theSystem.out.println (""); the         } the 98          for(inti = 0; I < Bag.itemcount + 1; I++) {//Print the total value of packages in all scenarios about              for(intj = 0; J < Bag.bagsize + 1; J + +) { - System.out.print (bag.okbags[i][j]);101             }102System.out.println ("");103         }104  theOkbag Okbagresult =bag.okbags[bag.itemcount][bag.bagsize];106System.out.println ("end result is:" + okBagResult.Items.toString () +okbagresult);107 108     }109  the}

Java implementation knapsack algorithm (0-1 knapsack Problem)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.