2079 3

CSC325 ยท 2079 ยท 10 marks

What do you mean by Backtracking? Explain the backtracking algorithm for solving knapsack problem and find the solution for the problem given below and capacity of knapsack is 10 kg.

What do you mean by Backtracking? Explain the backtracking algorithm for solving knapsack problem and find the solution for the problem given below and capacity of knapsack is 10 kg.

$$\begin{bmatrix} \text{Items} & 1 & 2 & 3 & 4 \ \text{Weight (w}_i\text{)} & 2 & 3 & 4 & 5 \ \text{Profit (P}_i\text{)} & 3 & 5 & 6 & 10 \end{bmatrix}$$

[10]

Design and Analysis of AlgorithmsChapter 6: BacktrackingBacktracking AlgorithmsNumerical: backtracking knapsack problem

Backtracking and 0/1 Knapsack Solution

What is Backtracking?

Backtracking is a systematic algorithmic technique that builds a solution incrementally, one component at a time, and abandons a partial candidate ("backtracks") as soon as it determines that this candidate cannot lead to a valid or optimal complete solution.

Key points:

  • It explores a state-space tree of all possible states.
  • At each node a decision is made (for knapsack: include or exclude an item).
  • A bounding function prunes branches that cannot improve upon the best solution found so far.
  • More efficient than brute force since unpromising subtrees are cut.

Backtracking Algorithm for 0/1 Knapsack

  • Each item is either taken (1) or not (0), so the state-space tree is binary.
  • Items are usually sorted by profit/weight ratio (descending) so the bound is tight.
  • At each node compute an upper bound using the fractional knapsack relaxation on remaining items. If bound <= bestProfit, prune.
Algorithm BKnap(i, cw, cp):        // cw=current weight, cp=current profit
  if cw <= W and cp > maxProfit:
      maxProfit = cp
  if i > n: return
  if Bound(i, cw, cp) > maxProfit:
      // include item i
      BKnap(i+1, cw + w[i], cp + p[i])   // only if cw+w[i] <= W
  // exclude item i
  BKnap(i+1, cw, cp)

Numerical Solution

Given Data

ItemWeight $w_i$Profit $p_i$Ratio $p_i/w_i$
1231.50
2351.67
3461.50
45102.00

Capacity $W = 10$ kg.

Sort by ratio (descending):

OrderItem$w$$p$Ratio
A45102.00
B2351.67
C1231.50
D3461.50

Bound formula

$$\text{Bound}=cp+\text{(fractional knapsack value of remaining items in remaining capacity)}$$

State-Space Tree (order 4,2,1,3)

Node 1 (root): $cw=0,\ cp=0$ Bound $=10+5+3+\tfrac{0}{4}\cdot6 = 18$ (after 4,2,1 weight $=10$, capacity full). So overall root bound $=18$.

Include Item 4: $cw=5,\ cp=10$

  • Bound: add 2 ($p=5$) โ†’ cap 2 left, add 1 ($p=3$) โ†’ cap 0. Bound $=10+5+3=18$.

    Include Item 2: $cw=8,\ cp=15$

    • Bound $=15+3=18$ (item 1 fits, cap 2).

      Include Item 1: $cw=10,\ cp=18$ - feasible, cap $=0$. $$\boxed{maxProfit = 18,\quad {4,2,1}}$$

      Exclude Item 1: $cw=8,\ cp=15$, cap 2. Bound $=15+\tfrac{2}{4}\cdot6=18$. Item 3 (w=4) does not fit; best stays 15 < 18. Pruned / no improvement.

    Exclude Item 2: $cw=5,\ cp=10$, cap 5. Bound $=10+3+\tfrac{3}{4}\cdot6=17.5 < 18$. Pruned.

Exclude Item 4: $cw=0,\ cp=0$, cap 10. Bound $=5+3+6+\tfrac{2}{5}\cdot10$? Remaining items 2,1,3 total weight $=3+2+4=9\le10$, so all fit: profit $=5+3+6=14 < 18$. Pruned.

Verification (best combination)

Items 1, 2, 4: weight $=2+3+5=10 \le 10$, profit $=3+5+10=18$. โœ”

Any other feasible set:

  • ${2,3,1}$: w $=9$, p $=14$
  • ${4,3}$: w $=9$, p $=16$
  • ${4,2}$: w $=8$, p $=15$

Maximum is 18.

Final Answer

$$\textbf{Optimal profit} = 18,\qquad \textbf{Items selected} = {1,\ 2,\ 4}$$ with total weight $10$ kg exactly filling the knapsack.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.