Bitpath

Lesson 2 · Unit 04

Prune early

Skip branches that cannot reach an answer

13 exercisesabout 3 min

What you practise

  • Combination sum with sorted candidates and break
  • Generating balanced parentheses

Try a question from this lesson

Pick one

You pick from 2, 3 and 5, reusing numbers freely, to reach exactly 8. Your picks so far are 5 and 5. What is the smart move?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Prune dead branches

A branch that cannot reach an answer is a dead end. Checking only at the leaves means walking the whole dead subtree first.

Pruning moves the check earlier: test a choice before you recurse, and skip it if it cannot work.

Sorting makes this stronger. With the candidates in rising order, the first one that is too big proves all the later ones are too big as well, so one break ends the loop.

def sums(nums, lo, left, path):
    if left == 0:
        print(*path)
        return
    for i in range(lo, len(nums)):
        n = nums[i]
        if n > left:
            break             # prune
        path.append(n)
        sums(nums, i, left - n, path)
        path.pop()

sums([2, 3, 5], 0, 8, [])
function sums(nums, lo, left, path) {
  if (left === 0) {
    console.log(path.join(" "));
    return;
  }
  for (let i = lo; i < nums.length; i++) {
    const n = nums[i];
    if (n > left) break;   // prune
    path.push(n);
    sums(nums, i, left - n, path);
    path.pop();
  }
}

sums([2, 3, 5], 0, 8, []);
function sums(nums: number[], lo: number,
  left: number, path: number[]) {
  if (left === 0) {
    console.log(path.join(" "));
    return;
  }
  for (let i = lo; i < nums.length; i++) {
    const n = nums[i];
    if (n > left) break;   // prune
    path.push(n);
    sums(nums, i, left - n, path);
    path.pop();
  }
}

sums([2, 3, 5], 0, 8, []);

Concept

Rules that prune

Some dead ends are broken rules, not sums. Take strings of balanced brackets, like (()) and ()().

Build them one character at a time with two counters. need is how many pairs still have to be opened. owed is how many closers are owed to brackets that are already open.

Open only while need is above zero. Close only while owed is above zero. A string that starts with a closer is never even begun.

def gen(cur, need, owed):
    if need == 0 and owed == 0:
        print(cur)
        return
    if need > 0:
        gen(cur + "(", need - 1, owed + 1)
    if owed > 0:
        gen(cur + ")", need, owed - 1)

gen("", 2, 0)
function gen(cur, need, owed) {
  if (need === 0 && owed === 0) {
    console.log(cur);
    return;
  }
  if (need > 0) {
    gen(cur + "(", need - 1, owed + 1);
  }
  if (owed > 0) {
    gen(cur + ")", need, owed - 1);
  }
}

gen("", 2, 0);
function gen(
  cur: string, need: number, owed: number,
) {
  if (need === 0 && owed === 0) {
    console.log(cur);
    return;
  }
  if (need > 0) {
    gen(cur + "(", need - 1, owed + 1);
  }
  if (owed > 0) {
    gen(cur + ")", need, owed - 1);
  }
}

gen("", 2, 0);

10 more exercises in the app

  • 4× multiple choice
  • 2× fill the blanks
  • 1× true or false
  • 2× type the answer
  • 1× match the pairs

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy