AI summary by TwinMind
Lecture 15: Recursion
MIT OpenCourseWare
45m 18s
Introduction to Recursion Concept
- Recursion: programming technique and algorithmic problem-solving approach
- Forces different thinking from iterative solutions (loops)
- Not mandatory but more natural for certain problems
- Requires "forgetting" loop-based approaches temporarily
- Interactive demonstration planned for student participation
Iterative vs Recursive Algorithms Overview
- Iterative algorithms: use loops (for/while)
- Variables capture computation state
- Variables change each loop iteration
- Stop conditions: exhausted sequence (for) or false condition (while)
- Result stored/accumulated through loop
Multiplication Example - Iterative Approach
- Goal: implement multiplication without built-in * operator
- Rely on addition: multiply a * b = add a to itself b times
- For loop version:
- Range 0 to b, repeat b times
- Variable 'total' captures state
- Return total at end
- While loop version:
- Start at b, count down to zero
- Variable 'result' tracks sum
- Decrease b by 1 each iteration
Multiplication Example - Recursive Approach
- Pattern recognition: 5 * 4 = 5 + (5 * 3)
- Extract one instance, solve smaller similar problem
- Recursive breakdown:
- 5 * 4 = 5 + 5 * 3
- 5 * 3 = 5 + 5 * 2
- 5 * 2 = 5 + 5 * 1
- 5 * 1 = 5 (base case)
- Building back up: substitute known values
- 5 * 1 = 5
- 5 * 2 = 5 + 5 = 10
- 5 * 3 = 5 + 10 = 15
- 5 * 4 = 5 + 15 = 20
Recursive Function Structure
- Recursive step: a * b = a + a * (b-1)
- Base case: when b = 1, a * b = a
- Code implementation:
- Function mult_recur(a, b)
- If b == 1: return a
- Else: return a + mult_recur(a, b-1)
- Function calls itself with changed parameters (b-1)
Python Tutor Demonstration
- Function call stack visualization:
- mult_recur(5, 4) → mult_recur(5, 3) → mult_recur(5, 2) → mult_recur(5, 1)
- Base case returns 5
- Values build back up: 5 → 10 → 15 → 20
- Multiple function environments simultaneously active
- Earlier calls wait for later calls to complete
Real-World Analogy - Exam Regrade Process
- Iterative approach:
- Student visits each staff member individually
- Student tracks all scores personally
- One function call total
- Recursive approach:
- Student only contacts instructor
- Each staff member delegates to next person
- Chain of function calls passes request down
- Results built back up through chain
- Student receives final answer only
Power Function Exercise
- Task: Calculate n^p without ** operator
- Base cases:
- If p == 0: return 1
- If p == 1: return n
- Recursive step: return n * power_recur(n, p-1)
- Demonstrates multiple base cases possible
Factorial Example with Live Demonstration
- Mathematical definition: n! = n * (n-1) * (n-2) * ... * 1
- Base case: 1! = 1 (or 0! = 1)
- Recursive step: n! = n * (n-1)!
- Live demonstration with 4 students:
- Each student represents function call environment
- factorial(4) → factorial(3) → factorial(2) → factorial(1)
- Base case returns 1
- Values: 1 → 2 → 6 → 24
- Students "disappear" after returning values
Key Observations About Function Calls
- Each function call creates separate environment
- Same function name, different parameter values
- Variables don't interfere between environments
- Return values only go to immediate caller
- Flow: divide down to base case, then conquer back up
Iterative vs Recursive Factorial Comparison
- Recursive version: mirrors mathematical definition closely
- Iterative version: uses loop with state variables
- Both achieve same result
- Recursive often more readable for mathematical problems
When to Use Recursion
- Current examples: simple cases where iteration equally valid
- Better recursion cases:
- Unknown depth requirements
- File system searching (variable directory depth)
- Expression parsing with nested parentheses
- Lists within lists (unknown nesting levels)
- Next lecture: Fibonacci sequence and recursive list processing
