分支限界法的0-1背包问题Python实现

<pre name="code" class="python">重量为[3,5,2,1]
值为[9,10,7,4]
背包的总重为7,主要运用的queue的特性
#coding : utf-8
import numpy as np
import Queue
import math
w = [3,5,2,1]#weight
v = [9,10,7,4]#value
def test(capacity):
    vec_len = 2**(len(v)+1) - 1#tree `s size
    vec_v = np.zeros(vec_len)
    vec_w = np.zeros(vec_len)
    vec_w[0]=capacity
    que = Queue.Queue();
    que.put(0)
    best = 0
    while(not que.empty()):
        current = que.get()
        level = int(math.log(current+1,2))
        if(vec_v[current] > vec_v[best]):
            best = current

        left = 2*current+1#left child   index
        right = 2*current+2#right child index

        if(left < vec_len and vec_w[current]-w[level] > 0 and vec_v[current]+v[level] > vec_v[best] ):
            vec_v[left] = int(vec_v[current]+v[level])
            vec_w[left] = vec_w[current]-w[level]
            que.put(left)
        if(right < vec_len and sum(v[level+1:-1])+vec_v[current] > vec_v[best]):
            vec_v[right] = vec_v[current]
            vec_w[right] = vec_w[current]
            que.put(right)
    print vec_w[best],vec_v[best]

if  __name__ == '__main__':
    test(7)

    原文作者:分支限界法
    原文地址: https://blog.csdn.net/han784851198/article/details/52966038
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞