简体   繁体   中英

python backtracking knapsack

This is the code I have thus far:

   def frac_knapsack(n,size, profit,K):
    if K <= 0:
       return 0
    for i in range(0,i):
        if profit[i]/size[i]>profit[i-1]/size[i-1]:
            profit.append[i] and size.append[i]
    s = 0
    p = 0
    for i in range(n):
        if s + size[i] <= K:
            p += profit[i]
            s += size[i]
        else: 
            p += (K-s) * (profit[i]/size[i])
            s = K
            break
        return p
def Knapsack(i, size):
    if i > n or size <= 0:
        print(x)
        return
    if x[j] == 1:
        for j in range(0,i-1):
           p+=P[j]
    if x[j] == 1:
        for j in range(0,i-1):
           s+=S[j]
    if x[i] == 1:
        if s+size[i] <= K and (p + profit[i] + B) > MaxProfit:
            B = fractional_Knapsack(n-(i+1), size[i+1:], profit[i+1:], T-size[i])
        if p+profit[i] > MaxProfit:
            MaxProfit=p+profit[i]
            x=solution
        Knapsack(i+1, T-size[i])
        if x[i] == 0:
            B = frac_Knapsack(n-(i+1), size[i+1:], profit[i+1:], T)
        if (p + B) > MaxProfit:
            Knapsack(i+1, T)
  1. I have a problem with sorting in line 4. I have to sort it as weight-efficiency. do i need to use quick sort algorithm?
  2. I want to make input for four things: n,size,profit and K do i need to use map? as size and profit is list?

You used

B = fractional_Knapsack(n-(i+1), size[i+1:], profit[i+1:], T)

your method is called

def frac_knapsack(n,size, profit,K):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM