Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 341: Algorithms Lecture 5: divide and conquer

Similar presentations


Presentation on theme: "CS 341: Algorithms Lecture 5: divide and conquer"β€” Presentation transcript:

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

10

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

16

17

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

20

21

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

25

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)


Download ppt "CS 341: Algorithms Lecture 5: divide and conquer"

Similar presentations


Ads by Google