3.7.16 · D1Algorithm Paradigms

Foundations — Backtracking — state-space tree, pruning

2,069 words9 min readBack to topic

This page assumes nothing. Before you can read the parent note (topic), you must own every word and symbol it throws at you. We build each one from a picture, in the order that lets each idea rest on the previous.


1. A "decision" and a "partial solution"

Before any tree or recursion, the atom is this: a decision.

Look at the first figure. On the left is an empty tray (partial solution = nothing chosen). Each arrow you follow adds one item to the tray. A complete solution is a full tray; a dead end is a tray you cannot legally add to.

Why the topic needs this: the whole method is "grow a partial solution decision by decision". If you cannot picture a half-built answer, nothing above makes sense.


2. The tree — root, node, edge, leaf, branch, subtree

Every sequence of decisions can be drawn as a branching diagram. Mathematicians call it a tree (drawn upside-down: root on top).

Why the topic needs this — the payoff sentence: a bad partial choice sits at some node, and every leaf in its subtree inherits that mistake. So if you can prove a node is hopeless, you delete its whole subtree in one stroke. That single fact is why backtracking is fast. Look at the red X in the figure: cutting one edge there erased four leaves at once.


3. Recursion — the tool that walks the tree

For us the base case is "the partial solution is complete → record it and return". Every non-base node says "for each valid child, call myself on that child". See Recursion for the deeper mechanics; here you only need: a self-call means 'go one node deeper', and returning means 'come back up'.

Why the topic needs this: the parent's generic template is a recursive function. Without recursion the "go deeper / come back" motion has no clean form.


4. DFS — the order recursion visits nodes

Recursion gives us a natural visiting order, and it has a name.

Follow the numbered arrows in the figure: 1 → 2 → 3 (hit a leaf) → back up → 4 (next sibling) → deeper again. This is exactly what recursion does automatically: the self-call dives, the return climbs. See Depth-First-Search.

Why the topic needs this: the parent says "explored in Depth-First order". Depth-first is the reason a partial solution is finished (or failed) before its siblings are even touched — which is when we get the chance to prune.


5. The feasibility test — is_valid and "promising"

Why the topic needs this: pruning is nothing but "run is_valid at each node and refuse to expand the failures". No feasibility test → no pruning → plain brute force.


6. Pruning and backtracking — the two named actions


7. The symbols in the parent's formula

The parent's boxed recursion uses compact notation. Here is every piece:


8. Branching factor , depth , and

The parent's complexity uses , , and . Build them from the tree picture.

Why leaves? Level 0 has 1 node. Each node makes children, so level 1 has , level 2 has , … level has . The little superscript means " multiplied by itself times".

Why the topic needs this: is the size of the un-pruned tree. It grows explosively (that superscript is why 4 queens has leaves). Pruning fights this growth. See Time-Complexity and, for the worst-case Big-O talk, Branch-and-Bound.

Recall Quick check: why is

"exponential"? Because the variable () is in the exponent. Add one level and you multiply the count by , not add — the size at least doubles-and-then-some each level. Why does pruning not lower the worst case? ::: An adversarial input can make every node still look promising until the very end, so nothing gets cut and you pay the full .


Prerequisite map

Decision + partial solution

State-space tree

Recursion

DFS visiting order

Feasibility test is_valid

Pruning

Backtracking undo

Branching b and depth d

Complexity b to the d

Backtracking topic

Read it top-down: the atom (decision) feeds both the tree and recursion; recursion + tree give DFS; the feasibility test plus DFS give pruning; pruning plus the undo give full backtracking; the tree's shape gives , , and the cost. All arrows converge on the topic.


Equipment checklist

Test yourself — reveal only after answering aloud.

A "decision" and a "partial solution" are
one move you make, and the incomplete answer built from all moves so far.
Root, node, edge, leaf
root = empty solution on top; node = partial solution; edge = one decision; leaf = complete solution or dead end.
Subtree, and why cutting one matters
a node plus everything below it; every leaf in it inherits that node's partial choice, so one cut kills exponentially many dead ends.
Why recursion over a loop
a tree's nested shape needs "go deeper, then return"; a self-call = go deeper, a return = climb back.
Base case
the stopping condition — here, "partial solution is complete → record and return".
DFS
go as deep as possible down one branch before trying the next sibling.
is_valid / promising node
a fast veto; a promising node is a partial solution that could still become a valid full answer.
Pruning vs backtracking
pruning = decide not to explore a doomed subtree; backtracking = mechanically undo the last move to return to the parent.
in the formula
the state with one more chosen decision appended (not arithmetic).
The union symbol
"combine the solutions from every valid branch".
, ,
branching factor, depth, and the number of leaves in the un-pruned tree.
Why is exponential
the depth is in the exponent, so each new level multiplies the node count by .

Connections

  • Recursion — the self-calling mechanism DFS rides on.
  • Depth-First-Search — the exact visiting order.
  • Time-Complexity — where and Big-O come from.
  • Branch-and-Bound — pruning with numeric bounds, the next step up.
  • N-Queens, Sudoku-Solver, Permutations-and-Combinations — first problems that use all of the above.
  • Backtracking (parent topic)