cs141 intermediate data structures and algorithms divide
play

CS141: Intermediate Data Structures and Algorithms Divide and - PowerPoint PPT Presentation

CS141: Intermediate Data Structures and Algorithms Divide and Conquer: Design and Analysis Amr Magdy Divide-and-Conquer (D&C) (Problem X) (Problem X) (Problem X) 2 Merge Sort 3 Merge Sort 4 Merge Sort 5 Merge Sort 6 Merge Sort


  1. CS141: Intermediate Data Structures and Algorithms Divide and Conquer: Design and Analysis Amr Magdy

  2. Divide-and-Conquer (D&C) (Problem X) (Problem X) (Problem X) 2

  3. Merge Sort 3

  4. Merge Sort 4

  5. Merge Sort 5

  6. Merge Sort 6

  7. Merge Sort 7

  8. Merge Sort 8

  9. Merge Sort 9

  10. Merge Sort 10

  11. Matrix Multiplication 11

  12. Matrix Multiplication 12

  13. Simple Matrix Multiplication 13

  14. Simple Matrix Multiplication 14

  15. Simple Matrix Multiplication 15

  16. D&C Matrix Multiplication 16

  17. D&C Matrix Multiplication Implementation details Partitioning matrices: index calculation not copying Is this faster than Θ (n 3 )? How to analyze a recursive algorithm? 17

  18. Analyzing Recursive Algorithms Determine the recursive relation 1. Analyze the complexity of the recursive relation 2. Two methods: Recursive tree expansion (or substitution method) Master theorem 18

  19. Analyzing Recursive Algorithms: Merge Sort 19

  20. Analyzing Recursive Algorithms: Merge Sort ……………………………… T(n) …………………………………………… Θ (1) ……………………………… Θ (1) ……………………… T(n/2) …………………. T(n/2) …………………………… Θ (n) 20

  21. Analyzing Recursive Algorithms: Merge Sort ……………………………… T(n) …………………………………………… Θ (1) ……………………………… Θ (1) ……………………… T(n/2) …………………. T(n/2) …………………………… Θ (n) T(n) = Θ (1) + Θ (1) + T(n/2) + T(n/2) + Θ (n) T(n) = 2 T(n/2) + Θ (n) 21

  22. Analyzing Recursive Algorithms: Merge Sort Recursion tree expansion for T(n) = 2 T(n/2) + Θ (n) 22

  23. Analyzing Recursive Algorithms: Merge Sort Recursion tree expansion for T(n) = 2 T(n/2) + Θ (n) 23

  24. Analyzing Recursive Algorithms: Merge Sort Recursion tree expansion for T(n) = 2 T(n/2) + Θ (n) 24

  25. Analyzing Recursive Algorithms General recursion tree 25

  26. Analyzing Recursive Algorithms Recursive Fibonacci algorithm? 26

  27. Analyzing Recursive Algorithms: Master Theorem Master Theorem: Used to solve recurrences in the form T(n) = aT(n/b) + f(n), a >= 1, b > 1, f(n) is a function, T(n) defined on nonnegative integers T(n) bound depends on polynomial comparison between f(n) and 𝑜 log 𝑐 𝑏 if f(n) polynomial less 𝑜 log 𝑐 𝑏  T(n) = Θ ( 𝑜 log 𝑐 𝑏 ) if f(n) polynomial equal 𝑜 log 𝑐 𝑏  T(n) = Θ ( 𝑜 log 𝑐 𝑏 log 𝑜 ) if f(n) polynomial greater 𝑜 log 𝑐 𝑏  T(n) = Θ ( 𝑔(𝑜) ) 27

  28. Analyzing Recursive Algorithms: Master Theorem Master Theorem: Used to solve recurrences in the form T(n) = aT(n/b) + f(n), a >= 1, b > 1, f(n) is a function, T(n) defined on nonnegative integers T(n) bound depends on polynomial comparison between f(n) and 𝑜 log 𝑐 𝑏 Formally: 28

  29. Analyzing Recursive Algorithms: Merge Sort T(n) = 2 T(n/2) + Θ (n) In the general form of Master theorem, a=2, b=2, f(n)=cn 𝑜 log 𝑐 𝑏 = 𝑜 log 2 2 = 𝑜 then f(n) = Θ ( 𝑜 log 𝑐 𝑏 ), case 2 T(n) = Θ ( 𝑜 log 𝑐 𝑏 log 𝑜 ) = Θ ( 𝑜 log 𝑜 ) 29

  30. Analyzing Recursive Algorithms Recursive Fibonacci algorithm? 30

  31. Analyzing Recursive Algorithms: Matrix Multiplication …………… .. ……………………… . …… T(n) ……………..…………………………………….….. Θ (1) ……………..………………………………... Θ (1) (2T(n/2) + 𝑜 2 ∗ 𝑜 2 )*4 T(n) = 8 T(n/2) + Θ (n 2 ) 31

  32. Analyzing Recursive Algorithms: Matrix Multiplication T(n) = 8 T(n/2) + Θ (n 2 ) In the general form of Master theorem, a=8, b=2, f(n)= Θ (n 2 ) 𝑜 log 𝑐 𝑏 = 𝑜 log 2 8 = 𝑜 3 then f(n) = O( 𝑜 log 𝑐 𝑏−𝜁 ) = O( 𝑜 3−1 ) , ϵ = 1 , case 1 T(n) = Θ ( 𝑜 log 𝑐 𝑏 ) = Θ ( 𝑜 3 ) D&C matrix multiplication is as fast as the simple matrix multiplication 32

  33. Strassen ’ s Matrix Multiplication 33

  34. Strassen’s Matrix Multiplication 𝑜 𝑜 Each P has T(n/2) and Θ (n 2 ) (adding two 2 𝑦 2 matrices) T(n) = 7T(n/2) + Θ (n 2 ) 34

  35. Analyzing Recursive Algorithms: Strassen ’ s Matrix Multiplication T(n) = 7 T(n/2) + Θ (n 2 ) In the general form of Master theorem, a=7, b=2, f(n)= Θ (n 2 ) 𝑜 log 𝑐 𝑏 = 𝑜 log 2 7 = 𝑜 2.807 then f(n) = O( 𝑜 log 𝑐 𝑏−𝜁 ) = O( 𝑜 2.807−0.8 ), ϵ = 0.8, case 1 T(n) = Θ ( 𝑜 log 𝑐 𝑏 ) = Θ ( 𝑜 2.807 ) 35

  36. Analyzing Recursive Algorithms: Strassen’s Matrix Multiplication T(n) = 7 T(n/2) + Θ (n 2 ) In the general form of Master theorem, a=7, b=2, f(n)= Θ (n 2 ) 𝑜 log 𝑐 𝑏 = 𝑜 log 2 7 = 𝑜 2.807 then f(n) = O( 𝑜 log 𝑐 𝑏−𝜁 ) = O( 𝑜 2.807−0.8 ), ϵ = 0.8, case 1 T(n) = Θ ( 𝑜 log 𝑐 𝑏 ) = Θ ( 𝑜 2.807 ) n 2.807 n 3 n 10 641.2096 1000 100 411149.7 1000000 1000 2.64E+08 1E+09 10000 1.69E+11 1E+12 100000 1.08E+14 1E+15 1000000 6.95E+16 1E+18 36 10000000 4.46E+19 1E+21

  37. When Master Theorem Fails? When 𝑜 log 𝑐 𝑏 and f(n) are not polynomially comparable Example 1: T(n) = 3 T(n/4) + n log n a=3, b=4, f(n) = n log n 𝑜 log 𝑐 𝑏 = 𝑜 log 4 3 = 𝑜 0.79 𝑔(𝑜)/𝑜 log 𝑐 𝑏 = 𝑜 0.21 log 𝑜 f(n) is polynomially larger than 𝑜 log 𝑐 𝑏 and case 3 applies What values of constant c satisfies the condition a f(n/b) <= cf(n)? 37

  38. When Master Theorem Fails? When 𝑜 log 𝑐 𝑏 and f(n) are not polynomially comparable Example 2: T(n) = 2 T(n/2) + n log n a=2, b=2, f(n) = n log n 𝑜 log 𝑐 𝑏 = 𝑜 log 2 2 = 𝑜 𝑔(𝑜)/𝑜 log 𝑐 𝑏 = log 𝑜 f(n) is not polynomially larger than 𝑜 log 𝑐 𝑏 (i.e., there is not polynomial factor n x in the ratio 𝑔(𝑜)/𝑜 log 𝑐 𝑏 , 𝑜𝑝 𝜁 > 0 exists) and Master theorem does not apply 38

  39. Credits & Book Readings Book Readings 2.3, Ch. 4 Intro, 4.2, 4.3, 4.4, 4.5 Credits Prof. Ahmed Eldawy notes Online Sources https://upload.wikimedia.org/wikipedia/commons/e/e6/Merge_s ort_algorithm_diagram.svg http://www.geeksforgeeks.org/wp- content/uploads/stressen_formula_new_new.png 39

Recommend


More recommend