A bad-looking branch is not necessarily impossible
Picture
Pruning is a claim: “No completion of this partial choice can give an answer we still need.” It can save enormous search work, but a wrong claim silently removes valid solutions. Every pruning rule should come with its condition and a counterexample when that condition is removed.
Predict
While choosing numbers to sum to 5, your current sum is 6. Is it always safe to stop exploring this branch?
Reveal the reasoning
Only with a suitable restriction, such as all remaining candidates being nonnegative. A remaining -1 could complete the target. “Already too large” is not sufficient by itself.
Go deeper: pruning and duplicate removal are different claims
For unique subsets of possibly repeated values, sort first. At one recursion depth, choose only the first occurrence of each equal value. Equal values at deeper depths remain available, allowing [1, 1].
def unique_subsets(values):
values = sorted(values)
answers, path = [], []
def visit(start):
answers.append(path.copy())
for i in range(start, len(values)):
if i > start and values[i] == values[i - 1]:
continue
path.append(values[i])
visit(i + 1)
path.pop()
visit(0)
return answers
assert unique_subsets([1, 1, 2]) == [[], [1], [1, 1], [1, 1, 2], [1, 2], [2]]
The condition i > start means “not the first option at this depth.” Removing it would forbid choosing a second equal value after the first one, incorrectly removing [1, 1].
For optimization, another kind of pruning uses a bound: if even the best possible completion cannot beat the best answer already found, the branch is unnecessary. The bound must be optimistic in the correct direction. A guessed future value is not enough.
Sorting costs O(n log n) here. Search is still exponential in the worst case, and copying outputs still counts. “Uses pruning” does not imply polynomial time.
Analogy limit: pruning invalid completions and choosing one representative of equivalent outputs have different proofs. Neither is the same as a greedy assertion that one locally attractive choice can be committed to globally.
Change one rule
Two equal-valued items have different serial numbers and the output must identify which physical items were selected. Should equal values be skipped at a depth?
Check the variation
No. The outputs are distinct under the revised identity rule. Value-based deduplication would erase required answers.
Connect
Tries provide a concrete impossibility test for prefixes. Knapsack compresses repeated optimization states. Greedy needs a different justification for committing to a choice.