CSC325 · 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.
Find all possible subsets of the integers that sum to 21 in the array {5, 6, 10, 11, 15} using back tracking technique. [5]
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$.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.