简体   繁体   中英

How to implement backtracking to print the items taken in knapsack(repetition of items allowed)?

I have this list of lists:

[['0', 'Cool Blue Marella Jug', '33', '15'],
 ['1', 'Weight Loss Pack', '55', '16'],
 ['2', 'Natural Black Vogue Lashes', '10', '6'],
 ['3', 'Paris Age Perfect Intense Nutrition Serum', '45', '22']​
  ...]
  • The first number is the product id,
  • The second number (55,10,...) is the price and
  • The third number (16,6...)is the profit.

Using my code below, if I enter a price limit I should get the highest profit by best combinations of items(items can be sold more than once).

This is my code:

def dp_pricelimit(product_list, price_limit):
    #to store results
    chosenProduct=[0]*(price_limit+1)
    memo=[0]*(price_limit+1)
    memo[0]=0
    for price in range(1, price_limit+1):
        for item in product_list:#go through the items
            if item[2]<=price:
                balance=price-item[2]
                profit=item[3] + memo[balance]
                if profit>memo[price]:#if found new optimal
                    memo[price]=profit
                    chosenProduct[price]=item[0]

    return memo[price_limit],chosenProduct

Now I need to modify this so that it also returns a list, chosenProduct , representing the chosen product ID to be sold.

Eg ​If products cool blue marella jug were chosen twice and weight loss pack chosen once the list should be ,chosenProduct=[0,0,1]

I tried storing the product chosen in a list whenever I found a new optimum but it stores every optimum it finds from value 1 to price_limit. I want it to store ONLY the latest product chosen and use backtracking from there to list all product chosen that make up the profit. How can I do this?

def dp_pricelimit(product_list, price_limit):
    #to store results
    chosenProduct=[None]*(price_limit+1)
    memo=[0]*(price_limit+1)
    memo[0]=0
    for price in range(1, price_limit+1):
        for item in product_list:#go through the items
            if item[2]<=price:
                balance=price-item[2]

                profit=item[3] + memo[balance]
                if profit>memo[price]:#if found new optimal
                    memo[price]=profit
                    chosenProduct[price]=item[0]

    #use list to store list of item
    items = []
    #set i, the current price, to max price
    i = price_limit

    #while i is not a negative price
    while i >= 0:
        if chosenProduct[i] == None:
            break
        #append the item that was last added to make the optimal profit at price i.
        items.append(chosenProduct[i])
        #then jump back to before we added this item by decrementing the i, the current price, by the price of the last item that was added.
        i-=product_list[items[-1]][2]




    return memo[price_limit],items


print(dp_pricelimit([[0, 'Cool Blue Marella Jug', 33, 15],[1, 'Weight Loss Pack', 55, 16], [2, 'Natural Black Vogue Lashes', 10, 2], [3, 'Paris Age Perfect Intense Nutrition Serum', 45, 22]],88))

Basically, use the chosenproduct array to iterate backwards. The last item added to create the optimal; its cost can be subtracted to get the optimal value at the price before it was added. Then at the next price we have the last item added to get that current price inside the chosenproduct array. Good luck ;)

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