CS330-Lec08
CS330-Lec08
T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8)
⋮
T(1) T(1) ………. base case: input n=1
22
Recursion tree method — TopHat Question 2
T (n / 4) T (n / 4) T (n / 4) T (n / 4)
T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8)
⋮
T(1) T(1) ……….
23
Recursion tree method — TopHat Question 3
T (n / 4) T (n / 4) T (n / 4) T (n / 4)
T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8)
⋮
T(1) T(1) ……….
24
Recursion tree method
T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8)
⋮
T(1) T(1) ………. #leaves = n base case: input n=1
25
Recursion tree method — analysis idea
• charge each operation to the function call (i.e. tree node) at which it is executed
• for Mergesort on a subarray of length k we do O(k) work to compute the split point
and do the merge
• work in further recursive calls is counted separately
T (n)
use cn instead of Θ(n)
T (n / 2) T (n / 2)
T (n / 4) T (n / 4) T (n / 4) T (n / 4)
T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8)
• charge each operation to the function call (i.e. tree node) at which it is executed
• for Mergesort on a subarray of length k we do O(k) work to compute the split point
and do the merge
• work in further recursive calls is counted separately
cn(n)
T
use cn instead of Θ(n)
Tcn/2
(n / 2) T cn/2
(n / 2)
T (n / 4) T (n / 4) T (n / 4) T (n / 4)
T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8)
• charge each operation to the function call (i.e. tree node) at which it is executed
• for Mergesort on a subarray of length k we do O(k) work to compute the split point
and do the merge
• work in further recursive calls is counted separately
amount of work per level:
T(n) = 2 ⋅ T(n /2) + cn where c > 0
cn(n)
T c⋅n
use cn instead of Θ(n)
Tcn/2
(n / 2) T cn/2
(n / 2) 2 ⋅ c ⋅ n /2
Tcn/4
(n / 4) T cn/4
(n / 4) Tcn/4
(n / 4) Tcn/4
(n / 4) 4 ⋅ c ⋅ n /4
T (n / 8) T (n / 8) T (n / 8) T (n / 8) cn/2
Tk(n / 8) T (n / 8) T (n / 8) T (n / 8) 2k ⋅ c ⋅ n /2k
Θ(1) Θ(n)
example: find 16
2 3 7 10 11 14 16 18 20 23
29
binary search - running time
30
binary search — running time
31
find closed form by “telescoping”
Reminder: our goal is to find a closed form formula for the recurrence
method:
• substitute the recursive formula until we get a good guess
• use induction to prove the formula
proof. by induction on n.
• base case: for n = 2 (or any constant) it takes const comparisons and log n = const
• suppose that the formula is true for every k < n
• proof for n: substitute k = n/2
assuming n
( is a power of 2
1 n == 1
T (n) = n
2T 2 + cn otherwise
Pf. T (n) 2 · T n
+ cn 4 · T n
+ 2cn
2 4
n 4 n
Substitute the formula in to T(n/2)
8·T 8 + 3cn 2 · T 24 + 4cn
···
n
2k T 2k
+ kcn
The last line corresponds to the base case. We know that the base case is T(1) = 0
34
Master Theorem
For recurrences defined as
Such that
log a if f (n) = O(nlogb a−! )
Θ(n b )
T (n) = Θ(nlogb a log n) if f (n) = Θ(nlogb a )
Θ(f (n)) if f (n) = Ω(nlogb a+! ) and af (n/b) ≤ cf (n)
1. T (n) = 4T (n/2) + n
2. T (n) = 4T (n/2) + n2
3. T (n) = 4T (n/2) + n3
Asymptotic time to execute integer operations
Basic operations:
adding, subtracting, multiplying, dividing
2
Integer addition
2 1 3
1 1 0 1 0 1 0 1
+ 1 2 5
+ 0 1 1 1 1 1 0 1
3
Integer addition
1 1 1 1 1 1 0 1
2 1 3
1 1 0 1 0 1 0 1
+ 1 2 5
+ 0 1 1 1 1 1 0 1
3 3 8
1 0 1 0 1 0 0 1 0
4
Integer addition
an-1 an-2…a2 a1 a0
+ bn-1 bn-2…b2 b1 b0
bits of a + b
5
Integer multiplication
2 1 3
x 1 2 5
6
Integer multiplication
2 1 3
x 1 2 5
1 0 6 5
4 2 6
+ 2 1 3
2 6 6 2 5
7
TopHat
B. O(log n) 1 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0
C. O(n)
1 1 0 1 0 1 0 1
D. D. (n2)
1 1 0 1 0 1 0 1
1 1 0 1 0 1 0 1
1 1 0 1 0 1 0 1
1 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0
0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1
8
Integer multiplication
1 1 0 1 0 1 0 1
× 0 1 1 1 1 1 0 1
1 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0
n bits each 1 1 0 1 0 1 0 1 n intermediate
total of O(n2) bits 1 1 0 1 0 1 0 1 products
1 1 0 1 0 1 0 1
1 1 0 1 0 1 0 1
1 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0
0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1
2n additions
9
Integer multiplication
an-1 an-2…a2 a1 a0
+ bn-1 bn-2…b2 b1 b0
n bits
n bits each n intermediate
n bits
total of O(n2) bits products
… n bits
+
2n additions a x b 2n bits
Divide-and-conquer:
・subproblem: same problem on smaller input
- input size here is the number of bits in the integers
- goal: compute ab by multiplying n/2 bit integers and then combine them
11
Divide: n bits to n/2
Input: n-bit integers a , b
Compute: ab
Note: in decimal multiplying by 10k shifts the number k bits to the left. Same in
binary, multiplying by 2k shifts k bits to the left (glues k 0s at the end.)
12
TopHat
Question. Write the representation of the base-3 number 1201203 as two parts of
n/2 digit base-3 integers.
A. 2 ⋅ 1203
B. 3 ⋅ 1203 + 1203
3
C. 120 3 ⋅ 3 + 1203
6 3
D. 120 3 ⋅ 3 + 120 3 ⋅ 3
Divide: n bits to n/2
・then a = A12n/2 + A0
・same for b: b = B12n/2+B0
14
Multiplying large integers using D&C
a = A12n/2 + A0 b = B12n/2 + B0
Compute:
ab = (A12n/2 + A0)(B12n/2 + B0)=
15
Multiplying large integers using D&C
a = A12n/2 + A0 b = B12n/2 + B0
16
Multiplying large integers using D&C
Algorithm 1: Multiply(ints a, b, n)
1 if n == 1 then
2 return ab;
3 m b n2 c;
4 A0 ba/2m c /* integer division */
5 B0 bb/2m c /* integer division */
6 A1 a mod 2m ;
7 B1 b mod 2m ;
8 x Multiply(A1 , B1 , m);
9 y Multiply(A1 , B0 , m); recursive call on input of size n/2
10 z Multiply(A0 , B1 , m);
11 w Multiply(A0 , B0 , m);
12 return x22m + (y + z)2m + w;
Note that lines 4-7 can be implemented by simple bit-shifts — no real division
is taking place!
Recurrence:
17
Solve the recurrence
(
1 n=1
T (n)
4T ( n2 ) + O(n) otherwise
18
Solve the recurrence
(
1 n=1
T (n)
4T ( n2 ) + O(n) otherwise
n
Use telescoping and solve T(n) = 4 ⋅ T( ) + cn
2
( 2)
n n n 2 2 n
T(n) = 4 ⋅ T( ) + cn = 4 4 ⋅ T( ) + c + cn = (2 ) ⋅ T( 2 ) + 3cn = …
2 4 2
2 3 n 2 k n
= (2 ) ⋅ T( 3 ) + 5cn = … = (2 ) ⋅ T( k ) + (k + 1)cn = …
2 2
19
General formula for recurrences of specific form
Recurrence:
(2)
n
T(n) = 4T + cn = cn log24 = Θ(n 2)
General form:
(2)
n
T(n) = qT + cn = cn log2 q for q > 2
20
Multiplying large integers using D&C
Algorithm 1: Multiply(ints a, b, n)
1 if n == 1 then
2 return ab;
3 m b n2 c;
4 A0 ba/2m c /* integer division */
5 B0 bb/2m c /* integer division */
6 A1 a mod 2m ;
7 B1 b mod 2m ;
8 x Multiply(A1 , B1 , m);
9 y Multiply(A1 , B0 , m); recursive call on input of size n/2
10 z Multiply(A0 , B1 , m);
11 w Multiply(A0 , B0 , m);
12 return x22m + (y + z)2m + w;
Note that lines 4-7 can be implemented by simple bit-shifts — no real division
is taking place!
22
Karatsuba’s algorithm
23
Karatsuba’s algorithm
24
Karatsuba’s algorithm
Algorithm 1: Karatsuba(ints a, b, n)
1 if n == 1 then
2 return ab;
3 m b n2 c;
4 A0 ba/2m c /* integer division */
5 B0 bb/2m c /* integer division */
6 A1 a mod 2m ;
7 B1 b mod 2m ;
8 x Karatsuba(A1 , B1 , m);
9 y Karatsuba(A0 , B0 , m);
10 z Karatsuba(A1 + A0 , B1 + B0 , m);
11 return x22m + (z x y)2m + y;
25
Karatsuba’s algorithm - TopHat
Algorithm 1: Karatsuba(ints a, b, n)
1 if n == 1 then
2 return ab;
3 m b n2 c;
4 A0 ba/2m c /* integer division */
5 B0 bb/2m c /* integer division */
6 A1 a mod 2m ;
7 B1 b mod 2m ;
8 x Karatsuba(A1 , B1 , m);
9 y Karatsuba(A0 , B0 , m);
10 z Karatsuba(A1 + A0 , B1 + B0 , m);
11 return x22m + (z x y)2m + y;
Algorithm 1: Karatsuba(ints a, b, n)
1 if n == 1 then
2 return ab;
3 m b n2 c;
4 A0 ba/2m c /* integer division */
5 B0 bb/2m c /* integer division */
6 A1 a mod 2m ;
7 B1 b mod 2m ;
8 x Karatsuba(A1 , B1 , m);
9 y Karatsuba(A0 , B0 , m);
10 z Karatsuba(A1 + A0 , B1 + B0 , m);
11 return x22m + (z x y)2m + y;
T (n) = 3T ( n2 ) + cn
27
Recursion tree method
work per procedure
n
Solve T (n) = 3T ( 2 ) + cn where c 2 const
cn cn
3
c n2 2 cn
c n2 c n2
9
c n4 c n4 c n4 n cn
c4 4 c n4 4 cn
( 32 )k cn
28
Recursion tree method - TopHat
work per procedure
n
Solve T (n) = 3T ( 2 ) + cn where c 2 const
cn cn
3
c n2 2 cn
c n2 c n2
9
c n4 c n4 c n4 n cn
c4 4 c n4 4 cn
A. log3 n & n k +
B. log2 n & 3k
C. log2 n & n 3
D. log3 n & k 3
29
Recursion tree method
work per procedure
n
Solve T (n) = 3T ( 2 ) + cn where c 2 const
cn cn
3
c n2 2 cn
h = log2 n c n2 c n2
9
c n4 c n4 c n4 n cn
c4 4 c n4 4 cn
( 32 )k cn
log3 n
( 32 )= 1 log2 n
n3 =
1
+
= 3log3 n ⇤ 3 log3 2
= n3log2 3
( 32 )log3 n cn = cnlog2 3 = cn1.59...
30
General formula for recurrences of specific form
T (n) = 3T ( n2 ) + cn = cnlog2 3
General form:
31
History of asymptotic complexity of integer multiplication
? grade-school Θ(n2)
Faster than grade-
32