Python Based on the backtracking algorithm subset tree template to solve the problem of 0-1 backpack instance, python0-1

Source: Internet
Author: User

Python Based on the backtracking algorithm subset tree template to solve the problem of 0-1 backpack instance, python0-1

This article describes how Python solves the 0-1 backpack problem based on the subset tree template of the Backtracking Method. We will share this with you for your reference. The details are as follows:

Problem

N items and a backpack are given. The Weight of item I is Wi, and its value is Vi. The size of the backpack is C. Q: How should I select the items to be loaded into the backpack so that the total value of the items to be put into the backpack is the largest?

Analysis

Apparently, items placed in a backpack are one of all the subsets of N items. Each item in N items has two statuses: select or not. Therefore, you only need to traverse the two States of each item.

The solution is a fixed length array of N elements, 1 and 1.

Apply the Backtracking Method to the subset tree template. Do not do it too well !!!

Code

'''0-1 backpack question ''' n = 3 # number of items c = 30 # package carrying capacity w = [20, 15, 15] # item weight v = [45, 25, 25] # item value maxw = 0 # maximum load that can be loaded in combination with conditions maxv = 0 # maximum load value that can be loaded in combination with conditions bag = [0, 0, 0] # the length of a solution (n element 0-1 array) is fixed to nbags = [] # A group of solutions bestbag = None # optimal solution # conflict Detection def conflict (k): global bag, if the first k items in w, c # bag are overweight, the conflict is if sum ([y [0] for y in filter (lambda x: x [1] = 1, zip (w [: k + 1], bag [: k + 1])> c: return True return False # apply the subset tree template def backpack (k ): # global bag, maxv, maxw, bestbag if k = n: # if the last item is exceeded, determine whether the result is optimal. cv = get_a_pack_value (bag) cw = get_a_pack_weight (bag) if cv> maxv: # value first maxv = cv bestbag = bag [:] if cv = maxv and cw <maxw: # the same value, lightweight priority maxw = cw bestbag = bag [:] else: for I in [1, 0]: # traverse two States [select 1, do not select 0] bag [k] = I # because the solution length is fixed if not conflict (k): # pruning backpack (k + 1) # according to a solution bag, calculate weight def get_a_pack_weight (bag): global w return sum ([y [0] for y in filter (lambda x: x [1] = 1, zip (w, bag)]) # calculate the value of def get_a_pack_value (bag): global v return sum ([y [0] for y in filter (lambda x: x [1] = 1, zip (v, bag)]) # test backpack (0) print (bestbag, get_a_pack_value (bestbag ))

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.