Professional Documents
Culture Documents
1. Analysis 50%
2. D&C 50%
– Contains new design problem
Announcements
Competition TOP5
SC & CSYS Compensation Quiz 2) In binary insertion sorting
(insertion sort that uses
1) You are running a library catalog. You
binary search to find each
know that the books in your collection are
insertion point)... How many
almost in sorted ascending order by title,
total operations are required
with the exception of one book which is in
in its expected case?
the wrong place. You want the catalog to
be completely sorted in ascending order?
What’s the most suitable sort? 3) Given N points in 2-dimensional space, it's required
to find the two points that are closest to each
other? What's the complexity of BRUTE-FORCE Sol?
GENERAL NOTE
Hidden Slides are Extra Knowledge
Not Included in Exams
Roadmap
Analysis Design
Can we do better?
Agenda
1. Select Kth Order Element
•
Learning Outcomes
< ? >
Select K Order Element
th
• Naïve Solution
1. Sort the elements O(Nlog(N))
2. Select Kth element O(1)
Total O(Nlog(N))
Desired O(N)
Select K Order Element
th
Partition ?
N–1 ?
N–2 ?
N–3 ?
RANDOMIZED-SELECT (A, L, R, K) 2 5 6 1 4 3 7 8
1 if L == R
Where’s each
2 return A[L]
step of D&C
3 Q = RANDOMIZED-PARTITION (A, L, R)
???
4J=Q–L+1
5 if K == J //the pivot value is the answer
6 return A[Q]
7 elseif K < J
8 return RANDOMIZED-SELECT (A, L, Q – 1, K)
9 else return RANDOMIZED-SELECT (A, Q + 1, R, K – J)
Select Kth Order Element – ALG1
• Pseudo-code
RANDOMIZED-PARTITION (A, L, R)
1 i = RANDOM (L, R)
2 exchange A[L] with A[i]
3 return PARTITION(A, L, R) From QuickSort
Agenda
1. Select Kth Order Element
•
Maximum Sequence Sum
• Given: array of N numbers
• Required: find a sequence of contiguous numbers with max sum
• Applications?
MAX: ML MC MR
Maximum Sequence Sum (D&C)
• D&C Solution
1. Divide: Trivial (divide the given array in two halves)
2. Conquer: Recursively calculate max. sequence sum in each half
3. Combine: Find maximum subarray sum from left (ML), right (MR) &
subarray crosses the midpoint (MC) HOW?
a) Find max sum starting from mid point and ending at some point on left of mid
b) Find max sum starting from mid+1 & ending with some point on right of mid+1
c) Add the two sums together to form Mc
N/2 M N/2
Maximum Sequence Sum (D&C)
• D&C Solution
– Analysis:
• Divide (Split the array into 2 Halves) Θ(1)
• Conquer (Recurse on 2 Subproblems) 2 T(N/2)
• Combine (Calculate the maximum subarray crosses the midpoint)
Θ(N)
•
Integer Multiplication
• Given: two N-digit large integers, X & Y
• Required: Multiply X & Y
N N-1 N-2 3 2 1
X . . .
×
Y . . .
2N N N-1 N-2 3 2 1
. . . . . .
Integer Multiplication (Naïve)
– Multiply each digit from Y by X Θ(N2)
– Add the results COMPLEXITY?
Θ(N2)
N N-1 N-2 3 2 1
X . . .
×
Y . . .
. . .
. . .
. . . +
. . .
. . .
2N N N-1 N-2 3 2 1
. . . . . .
Integer Multiplication (D&C)
• D&C Solution – Trial#1
1. Divide: split X & Y into two halves: A & B, C & D.
2. Conquer: Recursively multiply 4 subproblems
3. Combine: X = 10N/2
×B+A X B A
Y = 10N/2 × D + C Y D C
×
a) Pad B×D with N zeros,
A
b) Pad B×C, A×D with N/2 zeros
C
×
c) Add all together
B
B×D 000000000 ………. 00000000000 ×
B×C 000 ………. 000
+ C
X × Y = 10 × B×D + 10
N N/2
× (B×C + A×D) + A×C
B
D
×
Integer Multiplication (D&C)
• D&C Solution – Trial#1
– Analysis:
• Divide (Split X & Y into 2 Halves) Θ(N)
• Conquer (Recurs on 4 Subproblems) 4 T(N/2)
• Combine (Pad by 0’s & Add 4 Values) Θ(N)
Enc(M) = ME mod N
M = Enc(M)D mod N
• Public Key: two BIG integers N, E
• Private Key: two BIG integers N, D
Integer Multiplication (Karatsuba)
• Application
– RSA Cryptosystem Algorithm (Ex) DEMO
Enc(M) = ME mod N
M: Algorithms A’D
M = Enc(M)D mod N
N:475949804756254177244082678231127644638635769186852263630327872399101187400048606241668
59668486833021538759738968887527
E:2239763787088254951736859662264130092417109502055775358260344690284619737765819697471457
5577237681892436409853219169457
D:17
Agenda
1. Select Kth Order Element
•
Matrix multiplication
Input: A = [aij], B = [bij].
i, j = 1, 2,… , n.
Output: C = [cij] = A B.
n
cij a ik bkj
k 1
September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.27
Standard algorithm
for i 1 to n
do for j 1 to n
do cij 0
for k 1 to n
do cij cij + aik bkj
September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.28
Matrix Mul: D&C ALG1
IDEA:
nn matrix = 22 matrix of (n/2)n/2) submatrices:
r = ae + recursive
bg s = af 8 mults of (n/2)n/2) submatrices
^
+ bh t = 4 adds of (n/2)n/2) submatrices
ce + dg u
= cf + dh Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.30
Matrix Mul: D&C ALG1
1. Divide: each matrix into 4 quadrants
2. Conquer: Recursively multiply 8 problems.
3. Combine: add mul results & form final matrix.
T(N) = 8 T(N/2)+ (N2)
CASE 1 T(n) =
SAME AS NAÏVE !!!
nlogba = nlog28 = n3
September 14, 2005 (n
Copyright © 2001-5 ).Demaine and Charles E. Leiserson
Erik3D. L2.4 Can we do BETTER?
Strassen’s idea [D&C ALG2]
• Multiply 22 matrices with only 7 recursive mults.
P1 = a ( f – h) 7 mults, 18 adds/subs.
P2 = (a + b) h Note: No reliance on
commutativity of mult!
P3 = (c + d) e
P4 = d (g – e)
P5 = (a + d) (e + h) r = P5 + P4 – P2 + P6
P6 = (b – d) (g + h) s = P1 + P2
P7 = (a – c) (e + f ) t = P3 + P4
u = P5 + P1 – P3 – P7
September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.36
Strassen’s idea [D&C ALG2]
• Multiply 22 matrices with only 7 recursive mults.
P1 = a ( f – h) r = P5 + P4 – P2 + P6
P2 = (a + b) = (a + d) (e + h)
h P3 = (c + d) + d (g – e) – (a
e P4 = d (g + b) h
– e) + (b – d) (g + h)
P5 = (a + d) (e + h) = ae + ah + de +
P6 = (b – d) (g + dh
h) + dg –de – ah –
P =
September
7 (a
14, –
2005 c) (e
Copyright+© f )
2001-5 Erik D. Demaine
bh
and Charles E. Leiserson L2.39
Strassen’s Alg. [D&C ALG2]
P1 = a ( f – h)
1. Divide: P2 = (a + b) h
1. Partition A and B into (n/2)(n/2) submatrices. P3 = (c + d) e
P4 = d (g – e)
2. Form 10 terms to be multiplied using + and – . P5 = (a + d) (e + h)
P6 = (b – d) (g + h)
2. Conquer: P7 = (a – c) (e + f )
• Perform 7 multiplications of (n/2)(n/2) submatrices recursively.
3. Combine:
• Form C using + and – on (n/2)(n/2) submatrices.
Speedup
Naïve Solution N3 D&C Solution N2.81 factor
100 1,000,000 416,869 2.4x
1000 1,000,000,000 269,153,480 3.7x
10,000 1,000,000,000,000 173,780,082,874 5.6x
Agenda
1. Select Kth Order Element
•
Agenda
1. Select Kth Order Element
•
Summary
• D&C paradigm has 3 steps:
1. Divide
2. Conquer
3. Combine
• It significantly reduce complexity of some brute-force solutions
N = 106 Speedup
1. Search: N Log(N) 106 vs. 20 x50k
2. Sort: N2 N Log(N) 1012 vs. 2x107 x50k
3. Select Kth order: N Log(N) N 2x107 vs. 106 x20
4. Max Seq. Sum: N2 N Log(N) 1012 vs. 2x107 x50k
5. Big Integer Mul: N2 N1.58 1012 vs. 3x109 x330
3 2.8 18 15
Questions
• FIRST: D&C SHEET with Solutions
1. General Questions (MCQ, T/F, Trace…)
2. 12 Design Problems [with solution]
3. 6 Design Problems [without solution]
Questions
• SECOND: MIT Examples
1. Fibonacci Number
2. VLSI Tree Layout