Problem Solving — CS & DS

Lecture 21 — Binary Search Everywhere

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The Course Finder, revisited: sorted catalogue

cs-066

The catalogue from lecture 17 is now sorted by code: CS1, CS3, DS2. Find 'DS2' in at most two comparisons.

codes = ["CS1", "CS3", "DS2"]
lo, hi = 0, len(codes) - 1
probes = 0
found = None
while lo <= hi:
    m = (lo + hi) // 2
    probes += 1
    if codes[m] == "DS2":
        found = m
        break
    if codes[m] < "DS2":
        lo = m + 1
    else:
        hi = m - 1
print(found, probes)

Expected: found at index 2 after 2 probes

Cost: O(log n): a million sorted entries need 20 probes. Linear scan needed a million. That is the sort of factor that changes what is feasible.

Worked example

The First Bad Version: find the boundary

cs-082

Commits 1..10; every commit from some point on is bad. Badness probe answers True/False. Locate the first bad commit in as few probes as possible.

bad_from = 7

def is_bad(m):
    return m >= bad_from

lo, hi = 1, 10
while lo < hi:
    m = (lo + hi) // 2
    if is_bad(m):
        hi = m
    else:
        lo = m + 1
print(lo)

Expected: first bad commit: 7

Cost: O(log n) probes - the classic 'binary search on the answer' template that returns in lecture 30 (optimisation formulations).

Worked example

Book Allocation: binary search on the answer

cs-083

Book pages 10, 20, 30, 40 and 2 students; each student gets a contiguous run. Minimise the largest share.

pages = [10, 20, 30, 40]

def feasible(cap, students_max=2):
    students, load = 1, 0
    for p in pages:
        if load + p > cap:
            students += 1
            load = 0
        load += p
    return students <= students_max

lo, hi = max(pages), sum(pages)
while lo < hi:
    mid = (lo + hi) // 2
    if feasible(mid):
        hi = mid
    else:
        lo = mid + 1
print(lo)

Expected: minimum largest share: 60

Cost: O(n log(sum)) - the log factor from the cap search, the linear factor from each feasibility sweep.

Case attempt · lecture 21
Case cs-081 · Beginner

Guess the Number, Formally

The guessing game as binary search: guarantee the number of questions. Halving as an idea before any code.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-081 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-081 — not embedded in this deck.

Case attempt · lecture 21
Case cs-082 · Foundational

The First Bad Version

Versions 1..n, one onward is 'bad'. Find the first bad one with minimal checks. The canonical boundary-finding binary search.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-082 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-082 — not embedded in this deck.

Case attempt · lecture 21
Case cs-083 · Intermediate

Book Allocation

Split pages across k readers so the busiest reads as few as possible. Binary search the answer, feasibility check inside. Search-on-answer as a pattern.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-083 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-083 — not embedded in this deck.

Case attempt · lecture 21
Case cs-084 · Expert

The Rotten Timeline

An event switches state once and stays switched (monotone); find when, given a testable 'state at time t'. Generalising binary search to monotone predicates.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-084 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-084 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Why must the predicate be monotone for binary search?
  2. What is searched in search-on-the-answer?
  3. How many halvings for 1,000,000?
Expected answers (instructor): so that one test discards half the space forever the feasibility boundary over candidate answers, not the items about 20 — log2 of a million
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF