AI summary by TwinMind
Lecture 6: Bisection Search
MIT OpenCourseWare
1h 14m 26s
Review of Previous Lecture - Floating Point Numbers and Approximation Algorithms
- Last lecture covered floating point numbers and approximation algorithms
- Issue with guess and check: limited to exhaustive search through potential solutions
- Need for approximation: wanted smaller increments (0.1, 0.25, 0.001) instead of integer increments
- Floating point representation problem: numbers can't be stored exactly in memory
- Rounding occurs (approximately 10^-10 or 2^-32)
- Even simple operations like adding 0.1 ten times produce surprising results
- Approximation method introduced epsilon concept
- Can't check for equality with floating point numbers
- Need "wiggle room" - solution within plus or minus epsilon of desired value
- Problem with approximation algorithm: potential infinite loop
- Can overshoot epsilon boundary
- Solution: add sanity check from guess and check algorithm
- Stop when guess becomes unreasonable (guess squared > X)
Approximation Algorithm Performance Issues
- Example with X = 54321, epsilon = 0.01, increment = 0.00001
- Took couple seconds, made 2.3 million guesses
- Failed to find square root
- Solutions attempted:
- Make epsilon bigger (epsilon = 1): worked but less precise
- Make steps smaller: 10x slower (23 million guesses vs 2.3 million)
- Performance problem: waiting 20+ seconds for square root calculation unacceptable
Introduction to Bisection Search Algorithm
- Motivation through book guessing game example
- Book with 448 pages, $100 hidden on one page
- 8 guesses or fewer to win
- Without feedback: 1 in 56 chance of winning
- With "too high/too low" feedback: much better odds (about 1 in 3)
- Live demonstration of guessing game
- Student successfully found page 21 in 7 guesses
- Strategy: always guess midpoint, eliminate half the search space each time
Bisection Search Algorithm Principles
- Requires two properties:
- Ordering to the search space
- Feedback indicating if guess is too low, too high, or correct
- Algorithm process:
- Start with interval [low, high]
- Guess midpoint
- Based on feedback, eliminate half the search space
- Repeat until solution found
- Examples of when bisection search works/doesn't work:
- PIN code guessing: doesn't work (no "too high/too low" feedback)
- Exact decimal number guessing: theoretically works but may never terminate
Bisection Search for Square Roots
- Problem: find square root of X
- Search interval: [0, X] for X ≥ 1
- Algorithm steps:
- Initialize low = 0, high = X
- While |guess² - X| ≥ epsilon:
- guess = (high + low) / 2
- If guess² < X: low = guess (guess too low)
- Else: high = guess (guess too high)
- Logarithmic growth: eliminates half search space each iteration
- Performance comparison with approximation method:
- Approximation: 23 million guesses, 20 seconds
- Bisection: 30 guesses, less than 1 second
Logarithmic Growth Mathematics
- Each guess divides search space by 2
- After K guesses: search space = N / 2^K
- When converged: N / 2^K = 1, so K = log(N)
- Dramatic improvement over linear search
Implementation Issues and Solutions
- Problem with values between 0 and 1:
- Square root of 0.5 is larger than 0.5
- Original interval [0, X] doesn't contain answer
- Causes infinite loop
- Solution: adjust intervals based on X value
- If X ≥ 1: low = 0, high = X
- If 0 < X < 1: low = X, high = 1
- Robust implementation using conditional statements
Student Practice - Cube Root Algorithm
- Exercise: implement bisection search for cube root of 27
- Same structure as square root algorithm
- Replace guess² with guess³ throughout
- Epsilon = 0.01, interval [0, cube]
- Expected result: approximately 3.0
Newton-Raphson Method Introduction
- Alternative algorithm for finding roots of polynomials
- Formula: new_guess = guess - f(guess)/f'(guess)
- For square root of K: polynomial is x² - K
- f(x) = x² - K
- f'(x) = 2x
- new_guess = guess - (guess² - K)/(2 * guess)
- Even faster than bisection search: only 4 guesses for √24, 10 guesses for √54321
- Limitation: only works for polynomial root finding
Algorithm Comparison and Summary
- Three guess-and-check methods covered:
- Exhaustive enumeration: systematic search through all values
- Approximation algorithm: small incremental improvements
- Bisection search: eliminates half search space each iteration
- Newton-Raphson: specialized for polynomial roots
- All methods share common structure:
- Loop for repetition
- Method to generate guesses
- Way to check if guess is acceptable
- Continuation condition
Introduction to Decomposition and Abstraction
- Motivation for next topic: better code organization
- Decomposition: divide large programs into smaller, self-contained parts
- Each part solves different portion of larger program
- Parts can communicate but don't interfere with each other
- Abstraction: hide implementation details
- Focus on what code does, not how it does it
- Reusable components with clear interfaces
- Examples already used:
- Variables (pi, r) instead of copying values
- Comments to explain code purpose
- Goals: create code that's easy to modify, maintain, and understand
