Multiplication
Grade School Algorithmic Problem:
Given two n-digit numbers , output
size of instance: n
n - 1 additions of O(n)-digit numbers, resulting in…
Total number of basic operations:
Is there a faster way?
Half instance sizes (each var of ) (naive approach):
Recursive Multiplication:
mult(x, y) if n = 1, output x * y else return
Running time? Recursive relation:
Explanation: 4 subproblems of size n/2 then combining with addition of O(n)-digit numbers, which takes O(n) time. Why?
New idea:
Identity:
Plug back in for new recursive formula: (computes using already known values)
mult2(x, y)
if n = 1, return x * y
else
p1 = mult2(x_left, y_left)
p2 = mult2(x_right, y_right)
p3 = mult2(x_left + x_right,y_left + y_right)
return p1 * 2^n + (p3 - p1 - p2) * 2^(n/2) + p2Running time?
or
Solving recurrence relations:
General set-up:
- to solve a problem of size n, we need subproblems of size and recombination time.
- parameters
- mult:
- mult2:
General recurrence:
mult2: 3 subproblems of n/2 and an recombination time
- forms a tree
Tree definition:
- depth:
- recombination time for a single problem at level j:
- num nodes at level j:
- time spent at level j:
- e.g. for mult2,
Master Theorem
In general: 3 cases
- , - bottom level dominates (work grows at each level)
- , - top heavy (work shrinks at each level)
- - work is equal at every level
3 says every level takes the same computation
- subproblem count - division of n - degree of recombination time
First example:
Binary search:
Matches the third case:
Second example: Merge Sort
lecture notes: https://static.us.edusercontent.com/files/dVKhl6iQpQcVo0YrCFdpBak5
Intuition for getting Big O from Recursion Trees
Three general cases Goal is to determine which one an algorithm falls into
Top-heavy
The work done at the top is the biggest This could happen when where
Balanced
The work is done equally throughout
Example
where Evaluated with recursion trees like 2(n/2) = n which is the same work as the top level
Big O