Design and Analysis of Algorithms ยท Unit 6 ยท 5 hrs
Backtracking
Exam-focused notes for Backtracking (Design and Analysis of Algorithms, CSC325): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
Backtracking in short
Backtracking systematically searches through every possible way of building a solution, abandoning ("backtracking out of") a partial solution the moment it can be proven not to lead anywhere valid, rather than continuing to extend it uselessly. This unit sets out the technique in general and then applies it to three classic combinatorial search problems.
What this unit covers
- Concept of Backtracking, Recursion vs Backtracking
- Backtracking Algorithms: Subset-sum Problem, Zero-one Knapsack Problem, N-queen Problem and their Analysis
Backtracking Algorithms
2082 ยท 5 marks
Find all possible subsets of the integers that sum to 21 in the array {5, 6, 10, 11, 15} using back tracking technique. [5]
Show solved answerHide answer
Subset Sum Using Backtracking
Given
- Set of integers: $W = {5, 6, 10, 11, 15}$ (already in ascending order)
- Number of elements: $n = 5$
- Target sum: $M = 21$
Goal: find all subsets whose elements sum to exactly $21$.
Solution
Method
For the sum-of-subsets backtracking algorithm, keep track of:
- $s$ = sum of elements included so far
- $r$ = sum of remaining elements not yet considered
Total sum $= 5+6+10+11+15 = 47$.
Bounding (pruning) conditions at element $w_k$:
- Prune if $s + w_k > M$ (including this element overshoots the target).
- Prune if $s + r < M$ (even including everything left cannot reach the target).
Manual enumeration of valid subsets
Since the total is only 47 and target 21, I enumerate feasible include/exclude branches.
Branch: include 5 (s=5)
- include 6 (s=11): include 10 โ $s=21$ โ
{5,6,10}
- exclude 10 (r left = 11+15=26, s+r=37 ok): +11 โ 22 > 21 prune; exclude 11: +15 โ 26 > 21 prune. No solution.
- exclude 6 (s=5): include 10 (s=15): +11โ26 prune; exclude 11: +15โ30 prune. No solution.
- exclude 10 (s=5): +11โ16, then +15โ31 prune, exclude 15โ16 no; exclude 11 (s=5): +15โ20 no. No solution.
Branch: exclude 5 (s=0)
- include 6 (s=6): include 10 (s=16): +11โ27 prune; +15โ31 prune. No solution.
- exclude 10 (s=6): +11โ17 then +15โ32 prune, excludeโ17 no; exclude 11 (s=6): +15 โ $21$ โ {6,15}
- exclude 6 (s=0): include 10 (s=10): +11 โ $21$ โ
{10,11}
- exclude 11 (s=10): +15โ25 prune. No solution.
- exclude 10 (s=0): +11โ11 then +15โ26 prune; +15 aloneโ15 no. No solution.
Verification of each solution
$$5+6+10 = 21 \checkmark \qquad 6+15 = 21 \checkmark \qquad 10+11 = 21 \checkmark$$
(Check no others: any subset with 15 needs 6 more โ only {6}; any with 11 needs 10 more โ only {10}; without 11 and 15, max useful combos {5,6,10}=21. Confirmed complete.)
Final Answer: All Subsets Summing to 21
| Subset | Sum |
|---|---|
| ${5, 6, 10}$ | $21$ |
| ${6, 15}$ | $21$ |
| ${10, 11}$ | $21$ |
There are 3 subsets of ${5,6,10,11,15}$ that sum to $21$.
2081 ยท 5 marks
Given a set $A={5,7,10,12,15,18,20}$, find the subset that sum to 35 using backtracking. [5]
2080 ยท 5 marks
Does backtracking give multiple solution? Trace subset sum algorithm for the set $(3, 5, 2, 4, 1)$ and sum = 8. [5]
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.
$$\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]
Show solved answerHide answer
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
| Item | Weight $w_i$ | Profit $p_i$ | Ratio $p_i/w_i$ |
|---|---|---|---|
| 1 | 2 | 3 | 1.50 |
| 2 | 3 | 5 | 1.67 |
| 3 | 4 | 6 | 1.50 |
| 4 | 5 | 10 | 2.00 |
Capacity $W = 10$ kg.
Sort by ratio (descending):
| Order | Item | $w$ | $p$ | Ratio |
|---|---|---|---|---|
| A | 4 | 5 | 10 | 2.00 |
| B | 2 | 3 | 5 | 1.67 |
| C | 1 | 2 | 3 | 1.50 |
| D | 3 | 4 | 6 | 1.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.
2076 ยท 10 marks
Explain in brief the Backtracking approach for algorithm design. How it differs with recursion? Explain the N-Queen problem and algorithm using backtracking and analyze its time complexity.[10]
Concept of Backtracking, Recursion vs Backtracking
2082 ยท 5 marks
Distinguish between recursion and backtracking. Using Miller-Rabin primality test, check whether 53 is prime or not? [5+0]
2081 ยท 5 marks
Discuss about recursion and backtracking. Analyze the complexity of Miller Rabin Randomized Primality test. [5]
2078 ยท 5 marks
Explain the concept of backtracking. How it differ with recursion? [5]
2076 ยท 5 marks
Write short notes on: a. Backtracking strategy b. Tractable and Intractable Problem [5]
Make Unit 6 stick
Practice CSC325 with flashcards & quizzes