Académique Documents
Professionnel Documents
Culture Documents
CSOR W4246
Eleni Drinea
Computer Science Department
Columbia University
Tuesday, September 15, 2015
Outline
1 Recap
3 Binary search
4 Integer multiplication
Today
1 Recap
2 The running time of Mergesort and solving recurrences
3 Binary search
4 Integer multiplication
5 Fast matrix multiplication
I
I
I
I
Analyzed Mergesort
1. Correctness: by induction on the size of the array
2. Space: extra (n) space
I Input to Merge is stored in auxiliary arrays
Unlike insertion-sort, Mergesort does not sort in place
Today
1 Recap
2 The running time of Mergesort and solving recurrences
3 Binary search
4 Integer multiplication
5 Fast matrix multiplication
Mergesort: pseudocode
Mergesort (A, lef t, right)
if right == lef t then return
end if
middle = lef t + b(right lef t)/2c
Mergesort (A, lef t, middle)
Mergesort (A, middle + 1, right)
Merge (A, lef t, middle, right)
Remarks
I
Remarks
I
Total work:
Plogb n
i=0
ai c(n/bi )k = cnk
log
Pb n
i=0
a i
bk
O(nlogb a ) ,
O(nk log n) ,
T (n) =
O(nk ) ,
What about...
1. T (n) = 2T (n 1) + 1, T (1) = 2
Today
1 Recap
2 The running time of Mergesort and solving recurrences
3 Binary search
4 Integer multiplication
5 Fast matrix multiplication
I
I
I
I
I
I
Binary search
First, probe the middle entry. Let mid = dn/2e.
I
A[mid]
mid
A[mid]
mid mid+1
Today
1 Recap
2 The running time of Mergesort and solving recurrences
3 Binary search
4 Integer multiplication
5 Fast matrix multiplication
Integer multiplication
I
1100
11
1011
12
+ 12
132
1100
1100
0000
+ 1100
10000100
xL
yL
Examples
I
n = 2, x = 12, y = 11
12 = |{z}
1 |{z}
101 + |{z}
2
|{z}
x
xH
10n/2
1
xL
10n/2
yL
11 = |{z}
1 |{z}
10 + |{z}
1
|{z}
y
yH
n = 4, x = 1000, y = 1110
1000
10 |{z}
102 + |{z}
0
|{z} = |{z}
x
xH
10n/2
2
xL
10n/2
yL
11 |{z}
10 + |{z}
1110
10
|{z} = |{z}
y
yH
Karatsubas observation
Running time: T (n) 4T (n/2) + cn
I
no improvement
Karatsubas observation
Running time: T (n) 4T (n/2) + cn
I
no improvement
Karatsubas observation
Running time: T (n) 4T (n/2) + cn
I
no improvement
Karatsubas algorithm
x y = (xH 10n/2 + xL ) (yH 10n/2 + yH )
= xH yH 10n + (xH yL + xL yH )10n/2 + xL yL
Similarly to Gausss method for multiplying two complex
numbers, compute only the three products
xH yH , xL yL , (xH + xL )(yH + yL )
and obtain the sum xH yL + xL yH from
(xH + xL )(yH + yL ) xH yH xL yL = xH yL + xL yH .
Combining requires O(n) time hence
T (n) 3T (n/2) + cn = O(nlog2 3 ) = O(n1.59 )
Pseudocode
Concluding remarks
Today
1 Recap
2 The running time of Mergesort and solving recurrences
3 Binary search
4 Integer multiplication
5 Fast matrix multiplication
Output: m p matrix C = AB
Example: A = 10 01 , B = 11 11 , C =
I
1 0
0 1
for 1 i m do
for 1 j p do
n
P
ai,k bk,j
ci,j =
k=1
end for
end for
I
Running time?
Can we do better?
z}|{
A11 A12
B11
A21 A22
B21
B12
B22
C11
=
C21
where
C11 = A11 B11 + A12 B21
C12 = A11 B12 + A12 B22
C21 = A21 B11 + A22 B21
C22 = A21 B12 + A22 B22
Running time?
C12
C22