Python is an example of solving a knapsack problem based on greedy algorithms.

Source: Internet
Author: User

Python is an example of solving a knapsack problem based on greedy algorithms.

This article describes how to solve the problem of backpacking using the greedy algorithm in Python. We will share this with you for your reference. The details are as follows:

Greedy Algorithm(Also called greedy algorithms) means that when solving a problem, it is always the best choice for the moment. That is to say, what he makes is a local optimal solution in a certain sense without considering the overall optimization.

The greedy algorithm does not obtain the overall optimal solution for all problems. The key is the selection of greedy policies. The greedy strategy selected must be ineffective, that is, the process before a certain State will not affect the future State, it is only related to the current status.

Complete backpack Problems: Given n items and a C-sized backpack, the weight of item I is Wi, and its value is Vi. The question about how to choose an item that is included in the backpack is, this maximizes the total value of a backpack. The difference from a 0-1 backpack is that part of an item can be loaded into a backpack but cannot be loaded repeatedly in a full backpack.

The idea of designing algorithms is very simple: Calculate the unit value of an item, and then put as many items with the unit weight value as possible into a backpack.

The python implementation code is as follows:

# Coding = gbk # Full knapsack problem, greedy algorithm import time _ author _ = 'ice' class goods: def _ init _ (self, goods_id, weight = 0, value = 0): self. id = goods_id self. weight = weight self. value = value # Not applicable to 0-1 backpacks def knapsack (capacity = 0, goods_set = []): # Sort goods_set.sort (key = lambda obj: obj. value/obj. weight, reverse = True) result = [] for a_goods in goods_set: if capacity <a_goods.weight: break result. append (a_goods) Capacity-= a_goods.weight if len (result) <len (goods_set) and capacity! = 0: result. append (goods (a_goods.id, capacity, a_goods.value * capacity/a_goods.weight) return resultsome_goods = [goods (0, 2, 4), goods (1, 8, 6 ), goods (2, 5, 3), goods (3, 2, 8), goods (4, 1, 2)] start_time = time. clock () res = knapsack (6, some_goods) end_time = time. clock () print ('time spent: '+ str (end_time-start_time) for obj in res: print ('item No.:' + str (obj. id) + ', put the weight:' + str (obj. weight) + ', put value:' + str (obj. value), end = ',') print ('unit value: '+ str (obj. value/obj. weight) # Spend Time: 2.2807240614677942e-05 # item No.: 3. Put in weight: 2. Put in value: 8. unit value: 4.0 # item No.: 0, put weight: 2, put value: 4, unit value: 2.0 # item number: 4, put weight: 1, put value: 2, unit value: 2.0 # item No.: 1, put in weight: 1, put in value: 0.75, unit value: 0.75

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.