Divide and Conquer CSE 421 Algorithms Richard Anderson Lecture 12 Recurrences and Divide and Conquer Recurrence Examples T(n) = aT(n/b) + f(n) • T(n) = 2 T(n/2) + cn – O(n log n) • T(n) = T(n/2) + cn – O(n) • More useful facts: – log k n = log 2 n / log 2 k – k log n = n log k Recursive Matrix Multiplication Recursive Matrix Multiplication • How many recursive calls Multiply 2 x 2 Matrices: are made at each level? A N x N matrix can be viewed as | r s | | a b| |e g| a 2 x 2 matrix with entries that = are (N/2) x (N/2) matrices. | t u| | c d| | f h| • How much work in The recursive matrix multiplication algorithm r = ae + bf combining the results? recursively multiplies the s = ag + bh (N/2) x (N/2) matrices and t = ce + df combines them using the • What is the recurrence? u = cg + dh equations for multiplying 2 x 2 matrices 1
What is the run time for the recursive T(n) = 4T(n/2) + cn Matrix Multiplication Algorithm? • Recurrence: T(n) = 2T(n/2) + n 2 T(n) = 2T(n/2) + n 1/2 Solve by unrolling Recurrences T(n) = n + 5T(n/2) • Three basic behaviors – Dominated by initial case – Dominated by base case – All cases equal – we care about the depth 2
What you really need to know Classify the following recurrences about recurrences (Increasing, Decreasing, Balanced) • T(n) = n + 5T(n/8) • Work per level changes geometrically with the level • T(n) = n + 9T(n/8) • Geometrically increasing (x > 1) – The bottom level wins • T(n) = n 2 + 4T(n/2) • Geometrically decreasing (x < 1) • T(n) = n 3 + 7T(n/2) – The top level wins • Balanced (x = 1) • T(n) = n 1/2 + 3T(n/4) – Equal contribution Recurrence for Strassen’s Strassen’s Algorithm Algorithms • T(n) = 7 T(n/2) + cn 2 Multiply 2 x 2 Matrices: • What is the runtime? Where: | r s | | a b| |e g| = | t u| | c d| | f h| p 1 = (b + d)(f + g) p 2 = (c + d)e p 3 = a(g – h) r = p 1 + p 4 – p 5 + p 7 p 4 = d(f – e) s = p 3 + p 5 p 5 = (a – b)h t = p 2 + p 5 p 6 = (c – d)(e + g) u = p 1 + p 3 – p 2 + p 7 p 7 = (b – d)(f + h) BFPRT Recurrence • T(n) <= T(3n/4) + T(n/5) + 20 n What bound do you expect? 3
Recommend
More recommend