Chapter 3: Recursion, Binary Search and Backtracking

Data Structures and Algorithms with C
Stack framesTail recursionBinary searchTowers of HanoiPermutationsSatisfiabilityn-queensKnight’s tour
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Thinking recursively

Express a problem as a base case + a shrinking subproblem; compare recursion with iteration.

Tracing the stack

Draw the stack frame of each call (parameters, locals, return address).

Binary search

Write iterative and recursive binary search; show the O(log n) bound.

Recurrences

Set up and solve h(n) = 2h(n−1) + 1 for Hanoi; O(n·n!) for permutations.

Backtracking

Tell brute force from backtracking; SAT, n-queens, subset generation.

Heuristics

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.

Direct recursion

A function calls itself: fact → fact.

Indirect recursion

A function it calls calls it back: A → B → A.

Base case

The smallest instance, solved without recursion. Without it the calls never end (the stack overflows).

Recursive step

Reduces the problem to a smaller instance; every call must move toward the base case.

From the instructor“‘Doesn’t this recursive solution eat a lot of memory?’ Too early for that question. First learn to write the code; only once you can compute its complexity can you say so. You can’t judge recursion before you can write it.”

Recursion

The Stack Frame

system stack (grows upward)main()• return address• previous frame pointer• local variablesbinsearch(list, 18, 0, 9)• parameters: list, searchnum, left, right• local: middle• return address → mainnew call on top
  • 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

call stackmain()

main calls fact(3): a new frame will be pushed.

call stackmain()fact(n=3)

fact(3): n = 3 > 1, not the base case.

call stackmain()fact(n=3)

3 * fact(2): the multiplication waits; fact(2) is called first.

call stackmain()fact(n=3)fact(n=2)

fact(2): n = 2 > 1, not the base case.

call stackmain()fact(n=3)fact(n=2)

2 * fact(1): the multiplication waits; fact(1) is called first.

call stackmain()fact(n=3)fact(n=2)fact(n=1)→ 1

fact(1): base case → return 1. The stack is at its deepest (4 frames).

call stackmain()fact(n=3)fact(n=2)= 2→ 2

fact(2) resumes where it left off: 2 · 1 = 2, its frame is popped.

call stackmain()fact(n=3)= 6→ 6

fact(3) resumes where it left off: 3 · 2 = 6, its frame is popped.

call stackmain()

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;
}
RecursiveIterative
ReadabilityClose to the definitionNeeds state variables
Extra spaceO(depth) stackO(1)
Call overheadA frame per stepNone
RiskStack overflow when deepInfinite loop
Natural forTrees, divide and conquer, backtrackingLinear scans, counters
Any recursive algorithm can be turned into a loop by simulating the call stack with an explicit stack (and vice versa). The choice trades clarity against space and speed.

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) */
fact_tail(4, 1)fact_tail(3, 4)fact_tail(2, 12)fact_tail(1, 24)→ 24 returned directly
fact is not tail-recursive

n * fact(n-1): a multiplication waits after the return, so the frame must be kept.

Accumulator parameter

acc carries the partial result; nothing is left to do on return.

The compiler

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.

04113218319424527633736839940

Test array

#define COMPARE(x, y) ((x) < (y) ? -1 : (y) < (x) ? 1 : 0)
From the instructor“Return i, not list[i]. Why? So that the error value −1 can’t be confused with an array element that happens to be −1.”

Binary Search · Run the code step by step

Code Trace: iterative binsearch(list, 18, 0, 9)

04113218319424527633736839940▲ left▲ right

binsearch(list, 18, 0, 9): the range is the whole array.

04113218319424527633736839940▲ left▲ right▲ middle

0 ≤ 9: round 1. middle = (0 + 9) / 2 = 4, list[4] = 24.

04113218319424527633736839940▲ left▲ right

24 > 18 → COMPARE = 1: discard the right half, right = 3.

04113218319424527633736839940▲ left▲ right▲ middle

0 ≤ 3: round 2. middle = (0 + 3) / 2 = 1, list[1] = 13.

04113218319424527633736839940▲ left▲ right

13 < 18 → COMPARE = −1: discard the left half, left = 2.

04113218319424527633736839940▲ leftmiddle▲ right

2 ≤ 3: round 3. middle = (2 + 3) / 2 = 2, list[2] = 18.

04113218319424527633736839940▲ leftmiddle▲ right

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

04113218319424527633736839940▲ left▲ right

binsearch(list, 25, 0, 9): the range is the whole array.

04113218319424527633736839940▲ left▲ right▲ middle

0 ≤ 9: round 1. middle = (0 + 9) / 2 = 4, list[4] = 24.

04113218319424527633736839940▲ left▲ right

24 < 25 → COMPARE = −1: discard the left half, left = 5.

04113218319424527633736839940▲ left▲ right▲ middle

5 ≤ 9: round 2. middle = (5 + 9) / 2 = 7, list[7] = 36.

04113218319424527633736839940▲ left▲ right

36 > 25 → COMPARE = 1: discard the right half, right = 6.

04113218319424527633736839940▲ leftmiddle▲ right

5 ≤ 6: round 3. middle = (5 + 6) / 2 = 5, list[5] = 27.

04113218319424527633736839940left = 5 > right = 4: empty range

27 > 25 → COMPARE = 1: discard the right half, right = 4.

04113218319424527633736839940left = 5 > right = 4: empty range

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.
A common mistake: writing 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)

04113218319424527633736839940▲ ml=0 r=9 m=4

New frame: l = 0, r = 9 → m = 4 (list[4] = 24).

04113218319424527633736839940▲ ml=0 r=9 m=4

24 > 18 → case 1: call binsearch(list, 18, 0, 3).

04113218319424527633736839940▲ ml=0 r=9 m=4l=0 r=3 m=1

New frame: l = 0, r = 3 → m = 1 (list[1] = 13).

04113218319424527633736839940▲ ml=0 r=9 m=4l=0 r=3 m=1

13 < 18 → case −1: call binsearch(list, 18, 2, 3).

04113218319424527633736839940▲ ml=0 r=9 m=4l=0 r=3 m=1l=2 r=3 m=2

New frame: l = 2, r = 3 → m = 2 (list[2] = 18).

04113218319424527633736839940▲ ml=0 r=9 m=4l=0 r=3 m=1l=2 r=3 m=2→2

list[2] = 18: case 0 → return 2.

04113218319424527633736839940▲ ml=0 r=9 m=4l=0 r=3 m=1→2

The returned 2 is passed up unchanged (the case −1 frame is popped).

04113218319424527633736839940▲ ml=0 r=9 m=4→2

The returned 2 is passed up unchanged (the case 1 frame is popped).

04113218319424527633736839940▲ m

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.

T(n) = T(n/2) + c ⇒ T(n) = O(log n)
  • 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).
nmax roundslinear search
10410
1,000101,000
1,000,000201,000,000
1,000,000,000301,000,000,000
In the sample array (n = 10) at most 4 rounds: 3 were enough for 18.

Towers of Hanoi

The Towers of Hanoi Problem

source321auxtarget

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.

  1. Move one disk at a time.
  2. Never place a disk on top of a smaller disk.
Recursive solution: to move the largest disk to the target, there must be a moment when the n−1 disks above it are on the auxiliary tower: n−1 disks source→aux, the largest disk source→target, n−1 disks aux→target. h(n) = h(n−1) + 1 + h(n−1).
From the instructor“Some problems can only be solved recursively.”

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")

A321BC

n = 3: first move the top 2 disks A → B (via C).

A321BC

n = 2: first move the top disk A → C (via B).

A32BC1

n = 1: base case. Disk 1: A → C.

A3B2C1

Back again; the largest disk 2: A → B. Then 1 disk C → B.

A3B21C

n = 1: base case. Disk 1: C → B.

AB21C3

Back again; the largest disk 3: A → C. Then 2 disks B → C.

AB21C3

n = 2: first move the top disk B → A (via C).

A1B2C3

n = 1: base case. Disk 1: B → A.

A1BC32

Back again; the largest disk 2: B → C. Then 1 disk A → C.

ABC321

n = 1: base case. Disk 1: A → C.

ABC321

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.

3,1,2,32,1,3,21,1,2,31,3,1,22,2,1,31,2,3,11,1,2,3
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
nmoves
11
23
37
415
101,023
201,048,575
6418,446,744,073,709,551,615
64 disks: 2⁶⁴ − 1 ≈ 1.8·10¹⁹ moves. At one move per second ≈ 585 billion years. The algorithm uses O(2ⁿ) time and O(n) stack, and nothing does better: 2ⁿ − 1 moves are necessary and sufficient.

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 recursive idea

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}

Implementation by swapping

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)

0a1b2c▲ ij

i = 0, j = 0: swap with itself → abc; call perm(i = 1).

0a1b2c▲ ij

i = 1, j = 1: swap with itself → abc; call perm(i = 2).

0a1b2c

i = n: an arrangement is complete → print abc.

0a1c2b▲ i▲ j

i = 1, j = 2: list[1] ↔ list[2] → acb; call perm(i = 2).

0a1c2b

i = n: an arrangement is complete → print acb.

0a1b2c▲ i▲ j

Swap back: list[1] ↔ list[2] → abc (restored).

0b1a2c▲ i▲ j

i = 0, j = 1: list[0] ↔ list[1] → bac; call perm(i = 1).

0b1a2c▲ ij

i = 1, j = 1: swap with itself → bac; call perm(i = 2).

0b1a2c

i = n: an arrangement is complete → print bac.

0b1c2a▲ i▲ j

i = 1, j = 2: list[1] ↔ list[2] → bca; call perm(i = 2).

0b1c2a

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)

0b1a2c▲ i▲ j

Swap back: list[1] ↔ list[2] → bac (restored).

0a1b2c▲ i▲ j

Swap back: list[0] ↔ list[1] → abc (restored).

0c1b2a▲ i▲ j

i = 0, j = 2: list[0] ↔ list[2] → cba; call perm(i = 1).

0c1b2a▲ ij

i = 1, j = 1: swap with itself → cba; call perm(i = 2).

0c1b2a

i = n: an arrangement is complete → print cba.

0c1a2b▲ i▲ j

i = 1, j = 2: list[1] ↔ list[2] → cab; call perm(i = 2).

0c1a2b

i = n: an arrangement is complete → print cab.

0c1b2a▲ i▲ j

Swap back: list[1] ↔ list[2] → cba (restored).

0a1b2c▲ i▲ j

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

·aababcacacbbbabacbcbcaccbcbacacab

Root: no fixed prefix · each level fixes one position · leaves: the 6 arrangements in output order

Number of calls

1 + n + n(n−1) + … + n! calls; n! leaves.

Time

Each leaf prints n characters → O(n·n!). The common shorthand O(n!) misses this factor of n.

Why swap back?

Sibling branches must see the same starting array; the second SWAP repairs it.

Backtracking

Brute Force and Backtracking

Brute force

Generate every candidate and test each one. It relies on raw computing power; there may be 2ⁿ, n! … candidates.

Backtracking

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; }
}
From the instructor“Keep looking for a better solution: when solving problems with data structures, never be fully sure of your choice. Keep writing, keep discussing; tell yourself, ‘There is a better method, we just haven’t found it.’”

Backtracking

The Satisfiability (SAT) Problem

∨∨∧x1¬x2∧¬x1x3¬x3

(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.
From the instructor“I wish you had taken a course on the theory of computation too; that is where problems get classified.”

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.
Names like 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

∨=T∨=F∧=Fx1=F¬=Tx2=F∧=F¬=Tx1=Fx3=F¬=Tx3=F

Assignment 000: ¬x3 = T → root true: print.

∨=T∨=T∧=Fx1=F¬=Tx2=F∧=T¬=Tx1=Fx3=T¬=Fx3=T

Assignment 001: ¬x1 ∧ x3 = T → root true: print.

∨=T∨=F∧=Fx1=F¬=Fx2=T∧=F¬=Tx1=Fx3=F¬=Tx3=F

Assignment 010: ¬x3 = T → root true: print.

∨=T∨=T∧=Fx1=F¬=Fx2=T∧=T¬=Tx1=Fx3=T¬=Fx3=T

Assignment 011: ¬x1 ∧ x3 = T → root true: print.

∨=T∨=T∧=Tx1=T¬=Tx2=F∧=F¬=Fx1=Tx3=F¬=Tx3=F

Assignment 100: ¬x3 = T → root true: print.

∨=T∨=T∧=Tx1=T¬=Tx2=F∧=F¬=Fx1=Tx3=T¬=Fx3=T

Assignment 101: x1 ∧ ¬x2 = T → root true: print.

∨=T∨=F∧=Fx1=T¬=Fx2=T∧=F¬=Fx1=Tx3=F¬=Tx3=F

Assignment 110: ¬x3 = T → root true: print.

∨=F∨=F∧=Fx1=T¬=Fx2=T∧=F¬=Fx1=Tx3=T¬=Fx3=T

Assignment 111: no term is T → root false.

∨=F∨=F∧=Fx1=T¬=Fx2=T∧=F¬=Fx1=Tx3=T¬=Fx3=T

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

CIRCUIT-SAT

Is there an input assignment that makes the output of a given logic circuit 1?

Formula SAT

A propositional formula built with ∧, ∨, ¬, as above.

3-CNF-SAT

An ∧ of ∨-clauses, each with 3 literals: (x1 ∨ ¬x1 ∨ ¬x2) ∧ (x3 ∨ x2 ∨ x4) ∧ (¬x1 ∨ ¬x3 ∨ ¬x4).

Precomputing

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.

From the instructor“Type it into Google and there it is! A fast algorithm: Knuth–Morris–Pratt. Be sure to study greedy algorithms, branch and bound and precomputing in algorithm analysis books!”

“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)

Qr →

(0, 0) is safe → q[0] = 0, go to the next row.

QQ××r →

Columns 0, 1 conflict (×); (1, 2) is safe → q[1] = 2, go to the next row.

QQ××××r →

Columns 0, 1, 2, 3 conflict; no queen fits in row 2 → return 0: backtrack, the row above tries its next column.

QQr →

(1, 3) is safe → q[1] = 3, go to the next row.

QQQ×r →

Column 0 conflicts (×); (2, 1) is safe → q[2] = 1, go to the next row.

QQQ××××r →

Columns 0, 1, 2, 3 conflict; no queen fits in row 3 → return 0: backtrack, the row above tries its next column.

QQ××r →

Columns 2, 3 conflict; no queen fits in row 2 → return 0: backtrack, the row above tries its next column.

Qr →

No columns left; no queen fits in row 1 → return 0: backtrack, the row above tries its next column.

Qr →

(0, 1) is safe → q[0] = 1, go to the next row.

QQ×××r →

Columns 0, 1, 2 conflict (×); (1, 3) is safe → q[1] = 3, go to the next row.

QQQr →

(2, 0) is safe → q[2] = 0, go to the next row.

QQQQ××r →

Columns 0, 1 conflict (×); (3, 2) is safe → q[3] = 2, go to the next row.

QQQQ

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)

0a1b2c→ {abc}

Choices: take a, take b, take c → {abc}.

0a1b2c→ {ab}

Choices: take a, take b, skip c → {ab}.

0a1b2c→ {ac}

Choices: take a, skip b, take c → {ac}.

0a1b2c→ {a}

Choices: take a, skip b, skip c → {a}.

0a1b2c→ {bc}

Choices: skip a, take b, take c → {bc}.

0a1b2c→ {b}

Choices: skip a, take b, skip c → {b}.

0a1b2c→ {c}

Choices: skip a, skip b, take c → {c}.

0a1b2c→ {}

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

x01234567

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.

Move01234567
Δ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.

155

Square 1. Candidates (green = onward moves from there): 5, 5 → smallest positive: 5, choose (1, 2).

1213333

Square 2. Candidates (green = onward moves from there): 1, 3, 3, 3, 3 → smallest positive: 1, choose (0, 4).

1325

Square 3. Candidates (green = onward moves from there): 5 → smallest positive: 5, choose (2, 3).

132413233

Square 4. Candidates (green = onward moves from there): 1, 3, 2, 3, 3 → smallest positive: 1, choose (4, 4).

132455

Square 5. Candidates (green = onward moves from there): 5 → smallest positive: 5, choose (3, 2).

19328467522

Square 9. Candidates (green = onward moves from there): 2, 2 → smallest positive: 2, choose (1, 4).

1149321510138461116712514

Square 16. Candidates (green = onward moves from there): 1, 4 → smallest positive: 1, choose (4, 2).

1149203241921510138421182361116712172250

Square 24. Candidates (green = onward moves from there): 0 → the last free square is the only candidate: choose (2, 2).

11492032419215101382542118236111671217225

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

AlgorithmTimeStack / extra spaceNote
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 boundO(n)far less in practice thanks to pruning
Knight’s tour (Warnsdorff)O(n²)O(n²)heuristic; backtracking is exponential

Summary

Common Mistakes

1Forgetting the base case, or writing a step that never reaches it → stack overflow.
2Not returning the result of the recursive call: calling binsearch(...) and discarding the value.
3Using binary search on an unsorted array or a linked list.
4Returning the value found: it can be confused with −1; return the index.
5Forgetting to undo a choice in backtracking (the second SWAP, pick[i] = 0).
6Running an exponential algorithm on large n because “the machine is fast”.

Summary

Exercises

  1. Write a recursive function that finds the largest element of an array. What is the stack depth?
  2. At most how many rounds does binsearch take on a 1000-element array? And if the key is absent?
  3. How many times does hanoi(4, …) call printf, and how many times does it call itself?
  4. Write the first 6 lines printed by perm("abcd", 0, 4).
  5. Find by brute force the assignments that satisfy (x1 ∨ x2) ∧ (¬x1 ∨ ¬x2).
  6. Find the first solution of 5-queens by backtracking (the q array).
Show answers / hints
  1. max_of(a, n) = max(a[n−1], max_of(a, n−1)); base n == 1. Depth n.
  2. ⌊log₂ 1000⌋ + 1 = 10 rounds; if the key is absent, left > right after at most 10 rounds too.
  3. 15 printfs (2⁴ − 1); 15 calls (each call does exactly one printf).
  4. abcd, abdc, acbd, acdb, adcb, adbc.
  5. 01 and 10 (exclusive or).
  6. q = [0, 2, 4, 1, 3].

Summary

Chapter Summary

Recursion

Base case + shrinking step. Each call is a stack frame; tail recursion can become a loop.

Binary search

Halves the range of a sorted array: O(log n). Return the index; −1 means not found.

Hanoi

h(n) = 2h(n−1) + 1 = 2ⁿ − 1. The call tree = the history of the stack.

Permutations

Swap + recurse + swap back: n! lines of output, O(n·n!).

Backtracking

Build a partial solution, prune and undo when a rule breaks: n-queens, subsets (SAT was solved by brute force here).

Heuristics

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).

The End

Chapter 3: Recursion, Binary Search and Backtracking
Next chapter: Chapter 4: Linked Lists · Course page
1 / 1 Data Structures in C