Chapter 3: Recursion, Binary Search and Backtracking
Introduction
Learning Outcomes
Express a problem as a base case + a shrinking subproblem; compare recursion with iteration.
Draw the stack frame of each call (parameters, locals, return address).
Write iterative and recursive binary search; show the O(log n) bound.
Set up and solve h(n) = 2h(n−1) + 1 for Hanoi; O(n·n!) for permutations.
Tell brute force from backtracking; SAT, n-queens, subset generation.
Explain why Warnsdorff’s rule beats backtracking on the knight’s tour.
Recursion
Recursive Algorithms
When the logic that solves the whole problem also applies to its subproblems, a recursive algorithm can be used.
A function calls itself: fact → fact.
A function it calls calls it back: A → B → A.
The smallest instance, solved without recursion. Without it the calls never end (the stack overflows).
Reduces the problem to a smaller instance; every call must move toward the base case.
Recursion
The Stack Frame
- A function call uses the system stack.
- Before control passes to the function, some bookkeeping is done: where to return, saving the parameters…
- This communication and control structure is called a stack frame.
- Initially the frame holds two values: the return address and a pointer to the previous frame.
- Every recursive call means a new frame → at depth d the extra space is O(d).
Recursion · Run the code step by step
Code Trace: fact(3) and the call stack
main calls fact(3): a new frame will be pushed.
fact(3): n = 3 > 1, not the base case.
3 * fact(2): the multiplication waits; fact(2) is called first.
fact(2): n = 2 > 1, not the base case.
2 * fact(1): the multiplication waits; fact(1) is called first.
fact(1): base case → return 1. The stack is at its deepest (4 frames).
fact(2) resumes where it left off: 2 · 1 = 2, its frame is popped.
fact(3) resumes where it left off: 3 · 2 = 6, its frame is popped.
main prints 6. The variable n is a separate copy in every frame.
long fact(int n) { if (n <= 1) return 1; /* base case */ return n * fact(n - 1); } int main(void) { printf("%ld\n", fact(3)); return 0; }
Recursion
Recursion or Iteration?
/* recursive */ long fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); } /* iterative */ long fact_iter(int n) { long f = 1; while (n > 1) f *= n--; return f; }
| Recursive | Iterative | |
|---|---|---|
| Readability | Close to the definition | Needs state variables |
| Extra space | O(depth) stack | O(1) |
| Call overhead | A frame per step | None |
| Risk | Stack overflow when deep | Infinite loop |
| Natural for | Trees, divide and conquer, backtracking | Linear scans, counters |
Horowitz–Sahni 1.2.2
Recursion
Tail Recursion
If the recursive call is the last thing a function does (nothing is left to do after it returns), it is called tail recursion.
long fact_tail(int n, long acc) { if (n <= 1) return acc; return fact_tail(n - 1, n * acc); /* last: call */ } /* first call: fact_tail(n, 1) */
n * fact(n-1): a multiplication waits after the return, so the frame must be kept.
acc carries the partial result; nothing is left to do on return.
A tail call can become a jump (O(1) stack). The C standard does not guarantee this; gcc/clang usually do it when optimizing.
Binary Search
Binary Search
We search a sorted array for a value: return its index if found, −1 otherwise. Each step looks at the middle element and discards half of the range.
Test array
#define COMPARE(x, y) ((x) < (y) ? -1 : (y) < (x) ? 1 : 0)Binary Search · Run the code step by step
Code Trace: iterative binsearch(list, 18, 0, 9)
binsearch(list, 18, 0, 9): the range is the whole array.
0 ≤ 9: round 1. middle = (0 + 9) / 2 = 4, list[4] = 24.
24 > 18 → COMPARE = 1: discard the right half, right = 3.
0 ≤ 3: round 2. middle = (0 + 3) / 2 = 1, list[1] = 13.
13 < 18 → COMPARE = −1: discard the left half, left = 2.
2 ≤ 3: round 3. middle = (2 + 3) / 2 = 2, list[2] = 18.
COMPARE = 0 → return 2. Found in 3 rounds.
int binsearch(int list[], int searchnum, int left, int right) { while (left <= right) { int middle = (left + right) / 2; switch (COMPARE(list[middle], searchnum)) { case -1: left = middle + 1; break; case 0: return middle; case 1: right = middle - 1; } } return -1; }
Binary Search · Run the code step by step
Code Trace: binsearch(list, 25, 0, 9) → −1
binsearch(list, 25, 0, 9): the range is the whole array.
0 ≤ 9: round 1. middle = (0 + 9) / 2 = 4, list[4] = 24.
24 < 25 → COMPARE = −1: discard the left half, left = 5.
5 ≤ 9: round 2. middle = (5 + 9) / 2 = 7, list[7] = 36.
36 > 25 → COMPARE = 1: discard the right half, right = 6.
5 ≤ 6: round 3. middle = (5 + 6) / 2 = 5, list[5] = 27.
27 > 25 → COMPARE = 1: discard the right half, right = 4.
left > right: the loop ends → return −1 (3 rounds).
int binsearch(int list[], int searchnum, int left, int right) { while (left <= right) { int middle = (left + right) / 2; switch (COMPARE(list[middle], searchnum)) { case -1: left = middle + 1; break; case 0: return middle; case 1: right = middle - 1; } } return -1; }
Binary Search
Recursive Binary Search
int binsearch(int list[], int x, int l, int r) { int m; if (l > r) return -1; /* empty range */ m = (l + r) / 2; switch (COMPARE(list[m], x)) { case -1: return binsearch(list, x, m + 1, r); case 0: return m; case 1: return binsearch(list, x, l, m - 1); } return -1; /* unreachable */ }
- Instead of looping, it calls itself on a smaller range.
- Base cases: found (
case 0) or the range is empty (l > r). For brevity x = searchnum, l/r = left/right, m = middle. - Both recursive calls are tail calls: they return the result unchanged.
while instead of if in the recursive version. Every branch exits with return, so the loop runs once and the result is correct, but it is misleading: it should be if.Binary Search · Run the code step by step
Code Trace: recursive binsearch(list, 18, 0, 9)
New frame: l = 0, r = 9 → m = 4 (list[4] = 24).
24 > 18 → case 1: call binsearch(list, 18, 0, 3).
New frame: l = 0, r = 3 → m = 1 (list[1] = 13).
13 < 18 → case −1: call binsearch(list, 18, 2, 3).
New frame: l = 2, r = 3 → m = 2 (list[2] = 18).
list[2] = 18: case 0 → return 2.
The returned 2 is passed up unchanged (the case −1 frame is popped).
The returned 2 is passed up unchanged (the case 1 frame is popped).
2 reaches main: “it returns 2 to case 1, which returns it to main.” Depth 3 → O(log n) stack.
int binsearch(int list[], int x, int l, int r) { int m; if (l > r) return -1; /* empty range */ m = (l + r) / 2; switch (COMPARE(list[m], x)) { case -1: return binsearch(list, x, m + 1, r); case 0: return m; case 1: return binsearch(list, x, l, m - 1); } return -1; /* unreachable */ }
Binary Search
Complexity of Binary Search
Each round at least halves the range: n → n/2 → n/4 → … → 1.
- At most ⌊log₂ n⌋ + 1 rounds.
- Extra space for the iterative version: O(1).
- Recursive version: one frame per level → O(log n).
- Precondition: the array must be sorted and indexable in O(1) (useless on a linked list).
| n | max rounds | linear search |
|---|---|---|
| 10 | 4 | 10 |
| 1,000 | 10 | 1,000 |
| 1,000,000 | 20 | 1,000,000 |
| 1,000,000,000 | 30 | 1,000,000,000 |
Towers of Hanoi
The Towers of Hanoi Problem
The source tower holds n disks, each larger than the one above it. Goal: move them all to the target tower, using the auxiliary tower.
- Move one disk at a time.
- Never place a disk on top of a smaller disk.
In theory any recursion can become a loop with an explicit stack; the point is that recursion is the only natural way to express such problems.
Towers of Hanoi · Run the code step by step
Code Trace: hanoi(3, "A", "B", "C")
n = 3: first move the top 2 disks A → B (via C).
n = 2: first move the top disk A → C (via B).
n = 1: base case. Disk 1: A → C.
Back again; the largest disk 2: A → B. Then 1 disk C → B.
n = 1: base case. Disk 1: C → B.
Back again; the largest disk 3: A → C. Then 2 disks B → C.
n = 2: first move the top disk B → A (via C).
n = 1: base case. Disk 1: B → A.
Back again; the largest disk 2: B → C. Then 1 disk A → C.
n = 1: base case. Disk 1: A → C.
Done: 7 = 2³ − 1 moves, 7 calls. The stack was at most n = 3 frames deep.
void hanoi(int n, char *from, char *via, char *to) { if (n == 1) printf("%s -> %s\n", from, to); else { hanoi(n - 1, from, to, via); printf("%s -> %s\n", from, to); hanoi(n - 1, via, from, to); } } /* main: hanoi(3, "A", "B", "C"); */
Towers of Hanoi homework
Homework: Stack Frames of hanoi(3, "1", "2", "3")
Each box is the frame of one call: n, from, via, to. The left child is the first recursive call, the right child the second. At any moment the stack holds the path from the root to the current node.
Order of moves (inorder traversal)
1→3 · 1→2 · 3→2 · 1→3 · 2→1 · 2→3 · 1→3 — a node’s move is printed after its left subtree is finished.
Towers of Hanoi · Analysis
Solving the Recurrence h(n) = 2·h(n−1) + 1
h(1) = 1
h(n) = 2·h(n−1) + 1
= 4·h(n−2) + 2 + 1
= 8·h(n−3) + 4 + 2 + 1
= …
= 2ⁿ⁻¹·h(1) + 2ⁿ⁻² + … + 1
= 2ⁿ − 1| n | moves |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 7 |
| 4 | 15 |
| 10 | 1,023 |
| 20 | 1,048,575 |
| 64 | 18,446,744,073,709,551,615 |
Permutations
The Permutation Problem [O(n!)]
We want to print every arrangement of a set of n ≥ 1 elements: there are n! of them. {a, b, c} → abc, acb, bac, bca, cab, cba.
The permutations of {a, b, c, d} are:
• a followed by the permutations of {b, c, d}
• b followed by the permutations of {a, c, d}
• c followed by the permutations of {a, b, d}
• d followed by the permutations of {a, b, c}
Bring each element in turn to position i with a swap, permute the remaining n−i−1 positions recursively, then swap back to restore the array (backtracking!).
Permutations · Run the code step by step
Code Trace: perm("abc", 0, 3)
i = 0, j = 0: swap with itself → abc; call perm(i = 1).
i = 1, j = 1: swap with itself → abc; call perm(i = 2).
i = n: an arrangement is complete → print abc.
i = 1, j = 2: list[1] ↔ list[2] → acb; call perm(i = 2).
i = n: an arrangement is complete → print acb.
Swap back: list[1] ↔ list[2] → abc (restored).
i = 0, j = 1: list[0] ↔ list[1] → bac; call perm(i = 1).
i = 1, j = 1: swap with itself → bac; call perm(i = 2).
i = n: an arrangement is complete → print bac.
i = 1, j = 2: list[1] ↔ list[2] → bca; call perm(i = 2).
i = n: an arrangement is complete → print bca.
void perm(char *list, int i, int n) { int j, temp; if (i == n) { /* one arrangement */ for (j = 0; j < n; j++) printf("%c", list[j]); printf("\n"); } else for (j = i; j < n; j++) { SWAP(list[i], list[j], temp); perm(list, i + 1, n); SWAP(list[i], list[j], temp); } }
Permutations · Run the code step by step
Code Trace: perm("abc", 0, 3) (continued)
Swap back: list[1] ↔ list[2] → bac (restored).
Swap back: list[0] ↔ list[1] → abc (restored).
i = 0, j = 2: list[0] ↔ list[2] → cba; call perm(i = 1).
i = 1, j = 1: swap with itself → cba; call perm(i = 2).
i = n: an arrangement is complete → print cba.
i = 1, j = 2: list[1] ↔ list[2] → cab; call perm(i = 2).
i = n: an arrangement is complete → print cab.
Swap back: list[1] ↔ list[2] → cba (restored).
Swap back: list[0] ↔ list[2] → abc (restored).
void perm(char *list, int i, int n) { int j, temp; if (i == n) { /* one arrangement */ for (j = 0; j < n; j++) printf("%c", list[j]); printf("\n"); } else for (j = i; j < n; j++) { SWAP(list[i], list[j], temp); perm(list, i + 1, n); SWAP(list[i], list[j], temp); } }
Permutations · Analysis
Recursion Tree and Complexity
Root: no fixed prefix · each level fixes one position · leaves: the 6 arrangements in output order
1 + n + n(n−1) + … + n! calls; n! leaves.
Each leaf prints n characters → O(n·n!). The common shorthand O(n!) misses this factor of n.
Sibling branches must see the same starting array; the second SWAP repairs it.
Backtracking
Brute Force and Backtracking
Generate every candidate and test each one. It relies on raw computing power; there may be 2ⁿ, n! … candidates.
Build the solution step by step; as soon as a partial solution breaks a rule, prune that branch, undo the last choice and try the next.
void backtrack(partial_solution) {
if (is_complete) { print; return; }
for (each candidate choice)
if (satisfies_rules) { choose; backtrack(...); undo_choice; }
}Backtracking
The Satisfiability (SAT) Problem
(x1 ∧ ¬x2) ∨ (¬x1 ∧ x3) ∨ ¬x3
x1, x2, … are Boolean variables taking true/false; ∧, ∨, ¬ are the operators. Question: is there an assignment that makes the formula true?
- 3 variables → 2³ = 8 possible assignments: {F,F,F}, {F,F,T}, …
- n variables → 2ⁿ cases; brute force is O(2ⁿ). Even split over m processors it is O(2ⁿ/m): still exponential.
- SAT is NP-complete; no polynomial-time algorithm is known. 2-SAT is solvable in polynomial time, 3-SAT is NP-complete.
Backtracking
SAT: Evaluation by Postorder Traversal
typedef enum { L_NOT, L_AND, L_OR, L_TRUE, L_FALSE } logical; typedef struct node *tree_ptr; struct node { tree_ptr left_child, right_child; logical data; short value; }; void post_order_eval(tree_ptr t) { if (t) { post_order_eval(t->left_child); post_order_eval(t->right_child); switch (t->data) { case L_NOT: t->value = !t->right_child->value; break; case L_AND: t->value = t->left_child->value && t->right_child->value; break; case L_OR: t->value = t->left_child->value || t->right_child->value; break; case L_TRUE: t->value = 1; break; case L_FALSE: t->value = 0; break; } } }
- Postorder: both children first, then the node itself.
- A ¬ node keeps its single operand in the right child.
- Leaves: for each assignment the variables’ values are stored as
L_TRUE/L_FALSE.
enum { not, and, or, true, false } clash with the C99 <stdbool.h> macros and C23 keywords, and typedef struct node {…}; is a typedef that names nothing. Here we use prefixed names that do not clash.Backtracking · Run the code step by step
Code Trace: trying all 2³ assignments
Assignment 000: ¬x3 = T → root true: print.
Assignment 001: ¬x1 ∧ x3 = T → root true: print.
Assignment 010: ¬x3 = T → root true: print.
Assignment 011: ¬x1 ∧ x3 = T → root true: print.
Assignment 100: ¬x3 = T → root true: print.
Assignment 101: x1 ∧ ¬x2 = T → root true: print.
Assignment 110: ¬x3 = T → root true: print.
Assignment 111: no term is T → root false.
7 of the 8 assignments satisfy the formula; only x1 = x2 = x3 = 1 does not. A version that returns at the first solution would stop at 000.
int m, x1, x2, x3; for (m = 0; m < 8; m++) { /* 2^3 assignments */ x1 = (m >> 2) & 1; x2 = (m >> 1) & 1; x3 = m & 1; set_leaves(root, x1, x2, x3); post_order_eval(root); if (root->value) printf("%d%d%d\n", x1, x2, x3); }
Backtracking
SAT Variants and Precomputing
Is there an input assignment that makes the output of a given logic circuit 1?
A propositional formula built with ∧, ∨, ¬, as above.
An ∧ of ∨-clauses, each with 3 literals: (x1 ∨ ¬x1 ∨ ¬x2) ∧ (x3 ∨ x2 ∨ x4) ∧ (¬x1 ∨ ¬x3 ∨ ¬x4).
The formula tree is built once; each assignment changes only the leaves. A data structure prepared once for work done many times boosts efficiency a lot.
“I haven’t failed, I’ve found 10,000 ways that don’t work.” — attributed to Thomas Edison.
Backtracking
The n-Queens Problem
Place n queens on an n×n chessboard so that no queen attacks another: no two queens share a row, column or diagonal.
- One queen per row → a solution is an array
q[0..n−1]. - Brute force: nⁿ candidates (one queen per row; 16.8 million for n = 8).
- Backtracking: go row by row; if no column fits, return to the previous row.
- Diagonal test: |q[i] − c| == r − i.
int q[8]; /* q[r]: column of the queen in row r */ int is_safe(int r, int c) { int i; for (i = 0; i < r; i++) if (q[i] == c || abs(q[i] - c) == r - i) return 0; /* same column or diagonal */ return 1; }
Horowitz–Sahni; Sedgewick. There are 2 solutions for n = 4 and 92 for n = 8.
Backtracking · Run the code step by step
Code Trace: place(0, 4)
(0, 0) is safe → q[0] = 0, go to the next row.
Columns 0, 1 conflict (×); (1, 2) is safe → q[1] = 2, go to the next row.
Columns 0, 1, 2, 3 conflict; no queen fits in row 2 → return 0: backtrack, the row above tries its next column.
(1, 3) is safe → q[1] = 3, go to the next row.
Column 0 conflicts (×); (2, 1) is safe → q[2] = 1, go to the next row.
Columns 0, 1, 2, 3 conflict; no queen fits in row 3 → return 0: backtrack, the row above tries its next column.
Columns 2, 3 conflict; no queen fits in row 2 → return 0: backtrack, the row above tries its next column.
No columns left; no queen fits in row 1 → return 0: backtrack, the row above tries its next column.
(0, 1) is safe → q[0] = 1, go to the next row.
Columns 0, 1, 2 conflict (×); (1, 3) is safe → q[1] = 3, go to the next row.
(2, 0) is safe → q[2] = 0, go to the next row.
Columns 0, 1 conflict (×); (3, 2) is safe → q[3] = 2, go to the next row.
r = n: four queens placed → solution q = [1, 3, 0, 2]. We made 8 placements; brute force has 4⁴ = 256 candidates.
int place(int r, int n) { int c; if (r == n) return 1; /* solved */ for (c = 0; c < n; c++) if (is_safe(r, c)) { q[r] = c; if (place(r + 1, n)) return 1; } return 0; /* backtrack */ }
Backtracking · Run the code step by step
Code Trace: subset("abc", 3, 0, pick)
Choices: take a, take b, take c → {abc}.
Choices: take a, take b, skip c → {ab}.
Choices: take a, skip b, take c → {ac}.
Choices: take a, skip b, skip c → {a}.
Choices: skip a, take b, take c → {bc}.
Choices: skip a, take b, skip c → {b}.
Choices: skip a, skip b, take c → {c}.
Choices: skip a, skip b, skip c → {}.
void subset(char *s, int n, int i, int pick[]) { int j; if (i == n) { /* all decided */ printf("{"); for (j = 0; j < n; j++) if (pick[j]) printf("%c", s[j]); printf("}\n"); return; } pick[i] = 1; subset(s, n, i + 1, pick); pick[i] = 0; subset(s, n, i + 1, pick); }
Backtracking
The Knight’s Tour on a Chessboard
The knight must visit every square, each exactly once. It moves in an L: 2 across + 1 up/down, or 2 up/down + 1 across.
| Move | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Δi | −2 | −1 | +1 | +2 | +2 | +1 | −1 | −2 |
| Δj | +1 | +2 | +2 | +1 | −1 | −2 | −2 | −1 |
- Backtracking: try a move; at a dead end, undo the last move and try the next. It always finds a solution, but it is essentially brute force: very slow on 8×8.
- Heuristic: a “smart” choice; it finds near-optimal solutions without guarantees (like Minesweeper).
Backtracking is not “rewinding the scenario from the end”: it builds the partial solution forward and undoes the last choice at a dead end.
Backtracking
Warnsdorff’s Heuristic (1823)
“Move the knight to the square with the fewest onward moves, but not zero.” Onward moves: the number of free squares reachable directly from that square. Corners and edges are used up first, so dead ends don’t pile up at the end.
Square 1. Candidates (green = onward moves from there): 5, 5 → smallest positive: 5, choose (1, 2).
Square 2. Candidates (green = onward moves from there): 1, 3, 3, 3, 3 → smallest positive: 1, choose (0, 4).
Square 3. Candidates (green = onward moves from there): 5 → smallest positive: 5, choose (2, 3).
Square 4. Candidates (green = onward moves from there): 1, 3, 2, 3, 3 → smallest positive: 1, choose (4, 4).
Square 5. Candidates (green = onward moves from there): 5 → smallest positive: 5, choose (3, 2).
Square 9. Candidates (green = onward moves from there): 2, 2 → smallest positive: 2, choose (1, 4).
Square 16. Candidates (green = onward moves from there): 1, 4 → smallest positive: 1, choose (4, 2).
Square 24. Candidates (green = onward moves from there): 0 → the last free square is the only candidate: choose (2, 2).
All 25 squares visited without backtracking once. Warnsdorff looks at only 8 neighbors per step: O(n²) work in total.
The rule is named after H. C. von Warnsdorff. Ties are broken by the order in the move table (0…7).
Lab · C code step by step
Lab: Binary Search
Lab · C code step by step
Lab: n-Queens and Backtracking
Summary
Complexity Summary
| Algorithm | Time | Stack / extra space | Note |
|---|---|---|---|
fact (recursive) | Θ(n) | O(n) | the tail version can get down to O(1) |
| Binary search (iterative) | O(log n) | O(1) | array must be sorted |
| Binary search (recursive) | O(log n) | O(log n) | tail recursion |
| Hanoi | Θ(2ⁿ) | O(n) | 2ⁿ − 1 moves |
| Permutations | Θ(n·n!) | O(n) | n! arrangements |
| Subset generation | Θ(n·2ⁿ) | O(n) | 2ⁿ subsets |
| SAT (brute force) | O(2ⁿ·|F|) | O(|F|) | |F|: size of the formula tree |
| n-queens (backtracking) | O(n!) upper bound | O(n) | far less in practice thanks to pruning |
| Knight’s tour (Warnsdorff) | O(n²) | O(n²) | heuristic; backtracking is exponential |
Summary
Common Mistakes
returning the result of the recursive call: calling binsearch(...) and discarding the value.pick[i] = 0).Summary
Exercises
- Write a recursive function that finds the largest element of an array. What is the stack depth?
- At most how many rounds does
binsearchtake on a 1000-element array? And if the key is absent? - How many times does hanoi(4, …) call
printf, and how many times does it call itself? - Write the first 6 lines printed by perm("abcd", 0, 4).
- Find by brute force the assignments that satisfy (x1 ∨ x2) ∧ (¬x1 ∨ ¬x2).
- Find the first solution of 5-queens by backtracking (the q array).
Show answers / hints
- max_of(a, n) = max(a[n−1], max_of(a, n−1)); base n == 1. Depth n.
- ⌊log₂ 1000⌋ + 1 = 10 rounds; if the key is absent, left > right after at most 10 rounds too.
- 15 printfs (2⁴ − 1); 15 calls (each call does exactly one printf).
- abcd, abdc, acbd, acdb, adcb, adbc.
- 01 and 10 (exclusive or).
- q = [0, 2, 4, 1, 3].
Summary
Chapter Summary
Base case + shrinking step. Each call is a stack frame; tail recursion can become a loop.
Halves the range of a sorted array: O(log n). Return the index; −1 means not found.
h(n) = 2h(n−1) + 1 = 2ⁿ − 1. The call tree = the history of the stack.
Swap + recurse + swap back: n! lines of output, O(n·n!).
Build a partial solution, prune and undo when a rule breaks: n-queens, subsets (SAT was solved by brute force here).
Warnsdorff: the square with the fewest onward moves. Never stop looking for a better solution.
Summary
References
- Horowitz, Sahni, Anderson-Freed, Fundamentals of Data Structures in C, 2nd ed., Section 1.2 (recursive algorithms).
- Cormen et al., Introduction to Algorithms, 3rd ed.: Chapter 4 (recurrences), Chapter 34 (NP-completeness, SAT).
- Sedgewick, Algorithms in C, Chapter 5 (recursion).