200 likes | 293 Views
Algorithm design Knapsack Problem. Dr. O.Bushehrian. signs telling us that a node is nonpromising 1) Weight > W 2) Bound < MaxProfit Bound = ( Profit + ∑ J=i+1 to J=k P J ) + ( W – Weight – Total ) * p k+1 / W k+1. Suppose that n = 4, W = 16, and we have the following:.
E N D
Algorithm designKnapsack Problem Dr. O.Bushehrian
signs telling us that a node is nonpromising 1) Weight > W 2) Bound < MaxProfit Bound = ( Profit + ∑J=i+1 toJ=k PJ ) + ( W – Weight – Total ) * pk+1 / Wk+1
Profit=0 Weight=0 Bound=$115 Item1 $40 2
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Item3 $50 10 Profit=120 Weight=17
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Item3 $50 10 Profit=120 Weight=17
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Item3 $50 10 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Item3 $50 10 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80 Profit=80 Weight=12 Bound=$80 Item4 $10 5
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Item3 $50 10 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80 Profit=80 Weight=12 Bound=$80 Profit=70 Weight=7 Bound=$70 Item4 $10 5
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Profit=40 Weight=2 Bound=$98 Item3 $50 10 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80 Profit=80 Weight=12 Bound=$80 Profit=70 Weight=7 Bound=$7 Item4 $10 5
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Profit=40 Weight=2 Bound=$98 Item3 $50 10 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80 Profit=90 Weight=12 Bound=$98 Profit=80 Weight=12 Bound=$80 Profit=70 Weight=7 Bound=$7 Item4 $10 5
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Profit=40 Weight=2 Bound=$98 Item3 $50 10 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80 Profit=90 Weight=12 Bound=$98 Profit=80 Weight=12 Bound=$80 Profit=70 Weight=7 Bound=$7 Item4 $10 5
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Profit=40 Weight=2 Bound=$98 Item3 $50 10 Profit=40 Weight=2 Bound=$50 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80 Profit=90 Weight=12 Bound=$98 Profit=80 Weight=12 Bound=$80 Profit=70 Weight=7 Bound=$7 Item4 $10 5
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Profit=40 Weight=2 Bound=$98 Item3 $50 10 Profit=40 Weight=2 Bound=$50 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80 Profit=90 Weight=12 Bound=$98 Profit=80 Weight=12 Bound=$80 Profit=70 Weight=7 Bound=$7 Item4 $10 5
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Profit=40 Weight=2 Bound=$98 Item3 $50 10 Profit=40 Weight=2 Bound=$50 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80 Profit=90 Weight=12 Bound=$98 Profit=80 Weight=12 Bound=$80 Profit=70 Weight=7 Bound=$7 Item4 $10 5
Profit=0 Weight=0 Bound=$115 Item1 $40 2 Profit=40 Weight=2 Bound=$115 Profit=0 Weight=0 Bound=$82 Item2 $30 5 Profit=70 Weight=7 Bound=$115 Profit=40 Weight=2 Bound=$98 Item3 $50 10 Profit=40 Weight=2 Bound=$50 Profit=120 Weight=17 Profit=70 Weight=7 Bound=$80 Profit=90 Weight=12 Bound=$98 Profit=80 Weight=12 Bound=$80 Profit=70 Weight=7 Bound=$7 Item4 $10 5
Backtracking Algorithm for the 0–1 Knapsack Problem void knapsack (index i, intprofit, int weight) { if (weight <= W&& profit > maxprofit){ maxprofit = profit; numbest = i; bestset = include; } if (promising(i)){ include [i + 1] = 1; knapsack(i + 1, profit + p[i + 1], weight + w[i + 1]); include [i + 1] = 0; knapsack (i + 1, profit, weight); } }
booleanpromising (index i) { index j, k; inttotweight; float bound; if (weight >= W) return false; else { j = i + 1; bound = profit; totweight = weight; while (j <= n && totweight + w[j] < = W){ totweight = totweight + w[j]; bound = bound + p[j]; j++; } k = j; if (k <=n) bound = bound + (W - totweight) * p[k]/w[k]; return bound > maxprofit; } }