Download presentation
Presentation is loading. Please wait.
Published byCandice Charles Modified over 5 years ago
1
CS 341: Algorithms Lecture 5: divide and conquer
Slides by Trevor Brown (some material from Doug Stinsonβs slides) (
2
Finishing up last time: solving recurrence relations
3
Worked Examples Recall: simplified master theorem
b=2; y=1; x=1 Recall: simplified master theorem Ξ π π₯ log π =Ξ π log π a=3; b=2; y=1; x=log2 3 Ξ π π₯ =Ξ π log 2 3 a=4; b=2; y=1; x=log2 4 Ξ π π₯ =Ξ π 2 Questions: a=? b=? y=? x=? which Ξ function? a=2; b=2; y=3/2; x=1 Ξ π π¦ =Ξ π 3/2
4
General master theorem
Example recurrence: Arbitrary function of π (not just π π π¦ ) Must reason about relationship between π(π) and π π₯
5
Revisiting the recursion tree method
Some recurrences with complex π(π) functions (such as π(π) = πππβ‘π) can still be solved βby handβ Example: Let π= 2 π ; π 1 =1 ; π» π =ππ» π π +π π₯π¨π π There may have been a problem with this slide and the next oneβ¦ covering next time insteadβ¦
6
More Divide and conquer memes algorithms
7
Problem: Non-dominated points
So, I am a non-dominated point I dominate everything to my southwest No other point dominates me +π¦ So are we! So are we! So are we! +π₯
8
Whatβs an easy (brute force) algorithm for this?
More formally +π¦ Given two points ( π₯ 1 , π¦ 1 ) and ( π₯ 2 , π¦ 2 ), we say ( π₯ 1 , π¦ 1 ) dominates ( π₯ 2 , π¦ 2 ) if π π > π π and π π > π π Input: a set S of n points, Output: all non-dominated points in S, i.e., all points in S that are not dominated by any point in S +π₯ Whatβs an easy (brute force) algorithm for this?
9
Letβs come up with a better algorithm
Brute force algorithm For each point p dominated[p] = false For each other point q If q.x > p.x and q.y > p.y then dominated[p] = true If dominated[p] = false then add p to the output Running time? πΆ(ππ) Letβs come up with a better algorithm
11
Problem decomposition
12
Combining to get Non-dominated points
Let π 1 , π 2 , β¦, π π be the non-dominated points in πΊ π Let π
1 , π
2 , β¦, π
π be the non-dominated points in πΊ π Just need to find rightmost πΈ π that is not dominated (that has π¦-coordinate β₯ π
1 .π¦) π 1 π 2
13
Running time complexity?
14
Multiprecision multiplication
Input: two π-bit positive integers X and Y With binary representations: X=[X[k-1], β¦, X[0]] Y=[Y[k-1], β¦, Y[0]] Output: The 2k-bit positive integer Z=XY With binary representation: Z=[Z[2k-1], β¦, Z[0]]
15
Grade school multiplication
How to multiply π-bit numbers: Multiply all π2 pairs of digits This creates π partial products, each with between k and 2π bits Sum the partial products Total cost: bit-wise multiplication: π(π2) bit operations π sums of π(π)-bit numbers: π(π)βπ(π)=π(π2) bit operations
18
Complexity 4 recursive calls on π 2 bit numbers
combining takes Ξ π steps π π =4π π 2 +Ξ(π) Master theorem says π π = π log = π 2 Same complexity as naΓ―ve grade school multiplication! But with a more complicated recursive algorithmβ¦
19
An algorithm thatβs actually better
22
We did not get past here
23
Matrix multiplication
Input: A and B Output: their product C=AB Word-RAM model (64-bit ints) NaΓ―ve algorithm for πΓπ matrices: For each output cell πͺππ πΆ ππ =π·ππ‘ππππ(ππ π€ π (π΄), ππ π π π΅ π ) = π=1 π π΄ ππ π΅ ππ Running time?
24
Attempting a better solution
What if we first partition the matrix into sub-matrices Then divide and conquer on the sub-matrices Example of partitioning: 4x4 matrix into four 2x2 matrices
26
Deriving a recurrence MatrixMultiply performs π»(π) work, consisting of: Eight recursive calls to perform multiplications on π 2 Γ π 2 matrices Four additions of π 2 Γ π 2 matrices Work: 4 π 2 β
π 2 βΞ( π 2 ) π π =8π π 2 +Ξ( π 2 ) Recurse to multiply Adding is βcombiningβ
27
Solve using the (Simp.) Master theorem
a=? b=? y=? x=? Recurrence: π π =8π π 2 +Ξ( π 2 ) No better than the naΓ―ve algorithm! a=8 b=2 y=2 x= log 2 8=3 x>y so π» π βπ― π π =π―( π π )
28
Strassen matrix multiplication
For example, by definition π=ππ+π
π And according to Strassen, π= π· π + π· π Plugging in π 3 , π 4 , π= π+π
π+π
(πβπ) This simplifies to π=ππ+π
π+π
πβπ
π =ππ+π
π
29
How much better is Ξ( π 2.81 ) than Ξ( π 3 )?
Let n=10,000 π 2.81 β174 billion π 3 =1 trillion (~6x more) How much better is Ξ( π ) than Ξ( π 3 )? Let n=10,000 π β3.2 billion π 3 =1 trillion (~312x)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.