Lecture 31 CSE 331 Nov 16, 2009

advertisement
Lecture 31
CSE 331
Nov 16, 2009
Jeff is out of town this week
No regular recitation or Jeff’s normal office hours
I’ll hold extra Question sessions
Mon, Tue 4-6pm
Bell 242
Email me if you
plan to come in
after 5:30pm
Divide and Conquer
Divide up the problem into at least two sub-problems
Solve all sub-problems: Mergesort
Recursively solve the sub-problems
Solve some sub-problems: Multiplication
Solve stronger sub-problems: Inversions
“Patch up” the solutions to the sub-problems for the final solution
Integer Multiplication
Input: a = (an-1,..,a0) and b = (bn-1,…,b0)
Output: c = a x b
a = 1101 b = 1001
c =1110101
a = a12n/2 + a0
a1 = 11 and a0 = 01
b = b12n/2 + b0
b1 = 10 and b0 = 01
c=
2n + (
+
)2n/2 +
First attempt
Shift by O(n) bits
Adding O(n) bit numbers
Mult over n
bits
ab=
2n + (
+
)2n/2 +
Multiplication over n/2 bit inputs
T(n) = 4T(n/2) + cn
T(1) = c
T(n) is O(n2)
The key identity
+
= (a1+a0)(b1+b0) -
-
The final algorithm
Input: a = (an-1,..,a0) and b = (bn-1,…,b0)
T(1) = c
Mult (a, b)
If n = 1 return a0b0
T(n) = 3T(n/2) + cn
a1 = an-1,…,an/2 and a0 = an/2-1,…, a0
Compute b1 and b0 from b
x = a1 a0 and y = b1 b0
O(nlog 3) = O(n1.59)
run time
Let p = Mult (x, y), D = Mult (a1, b1), E = Mult (a0, b0)
All
operations
are O(n) time
F=p D E
return D 2n F 2n/2 E
ab=
2n +( (a1+a0)(b1+b0) -
-
2n/2 +
(Old) Reading Assignment
Sec 5.2 of [KT]
Rankings
How close are two rankings?
Today’s agenda
Formal problem: Counting inversions
Divide and Conquer algorithm
Download