CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.

Slides:



Advertisements
Similar presentations
A simple example finding the maximum of a set S of n numbers.
Advertisements

Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
Recursion & Merge Sort Introduction to Algorithms Recursion & Merge Sort CSE 680 Prof. Roger Crawfis.
Algorithms Recurrences Continued The Master Method.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
CSE 421 Algorithms Richard Anderson Lecture 12 Recurrences.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Updates HW#1 has been delayed until next MONDAY. There were two errors in the assignment Merge sort runs in Θ(n log n). Insertion sort runs in Θ(n2).
CSE 421 Algorithms Richard Anderson Lecture 13 Divide and Conquer.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-Conquer Dr. B C Dhara Department of Information Technology Jadavpur University.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Project 2 due … Project 2 due … Project 2 Project 2.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
Algorithms Merge Sort Solving Recurrences The Master Theorem.
Lecture 28 CSE 331 Nov 9, Mini project report due WED.
CSE 421 Algorithms Lecture 15 Closest Pair, Multiplication.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Foundations II: Data Structures and Algorithms
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Divide and Conquer. Recall Divide the problem into a number of sub-problems that are smaller instances of the same problem. Conquer the sub-problems by.
CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 4.
Divide and Conquer (Part II) Multiplication of two numbers Let U = (u 2n-1 u 2n-2 … u 1 u 0 ) 2 and V = (v 2n-1 v 2n-2 …v 1 v 0 ) 2, and our goal is to.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
CSCI 256 Data Structures and Algorithm Analysis Lecture 10 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
CSE 421 Algorithms Richard Anderson Lecture 13 Recurrences, Part 2.
Equal costs at all levels
Introduction to Algorithms: Recurrences
Divide-and-Conquer 6/30/2018 9:16 AM
Lecture 4 Divide-and-Conquer
Chapter 4 Divide-and-Conquer
Richard Anderson Lecture 14 Divide and Conquer
CSCE 411 Design and Analysis of Algorithms
Punya Biswas Lecture 15 Closest Pair, Multiplication
Richard Anderson Lecture 13 Recurrences and Divide and Conquer
Divide-and-Conquer 7 2  9 4   2   4   7
Richard Anderson Lecture 11 Recurrences
Richard Anderson Lecture 13 Divide and Conquer
Richard Anderson Lecture 11 Minimum Spanning Trees
Ch 4: Recurrences Ming-Te Chi
Richard Anderson Lecture 12 Recurrences and Divide and Conquer
Lecture 29 CSE 331 Nov 8, 2017.
Richard Anderson Lecture 13 Recurrences, Part 2
CSE 2010: Algorithms and Data Structures
Introduction to Algorithms
Divide and Conquer (Merge Sort)
Divide-and-Conquer 7 2  9 4   2   4   7
Trevor Brown CS 341: Algorithms Trevor Brown
Lecture 15, Winter 2019 Closest Pair, Multiplication
Richard Anderson Lecture 14 Divide and Conquer
Richard Anderson Lecture 13, Winter 2019 Recurrences, Part 2
Richard Anderson Lecture 14, Winter 2019 Divide and Conquer
Richard Anderson Lecture 14 Divide and Conquer
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 15 Closest Pair, Multiplication
Richard Anderson Lecture 12, Winter 2019 Recurrences
Richard Anderson Lecture 12 Recurrences
Richard Anderson Lecture 13 Divide and Conquer
Presentation transcript:

CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences

Divide and Conquer Recurrences, Sections 5.1 and 5.2 Algorithms –Counting Inversions (5.3) –Closest Pair (5.4) –Multiplication (5.5) –FFT (5.6)

Divide and Conquer Array Mergesort(Array a){ n = a.Length; if (n <= 1) return a; b = Mergesort(a[0.. n/2]); c = Mergesort(a[n/2+1.. n-1]); return Merge(b, c); }

Algorithm Analysis Cost of Merge Cost of Mergesort

T(n) <= 2T(n/2) + cn; T(2) <= c;

Recurrence Analysis Solution methods –Unrolling recurrence –Guess and verify –Plugging in to a “Master Theorem”

Unrolling the recurrence

Substitution Prove T(n) = 2 Induction: Base Case: Induction Hypothesis:

A better mergesort (?) Divide into 3 subarrays and recursively sort Apply 3-way merge What is the recurrence?

Unroll recurrence for T(n) = 3T(n/3) + dn

T(n) = aT(n/b) + f(n)

T(n) = T(n/2) + cn Where does this recurrence arise?

Recurrences Three basic behaviors –Dominated by initial case –Dominated by base case –All cases equal – we care about the depth

Divide and Conquer

Recurrence Examples 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 Multiply 2 x 2 Matrices: | r s | | a b| |e g| | t u| | c d| | f h| r = ae + bf s = ag + bh t = ce + df u = cg + dh A N x N matrix can be viewed as a 2 x 2 matrix with entries that are (N/2) x (N/2) matrices. The recursive matrix multiplication algorithm recursively multiplies the (N/2) x (N/2) matrices and combines them using the equations for multiplying 2 x 2 matrices =

Recursive Matrix Multiplication How many recursive calls are made at each level? How much work in combining the results? What is the recurrence?

What is the run time for the recursive Matrix Multiplication Algorithm? Recurrence:

T(n) = 4T(n/2) + cn

T(n) = 2T(n/2) + n 2

T(n) = 2T(n/2) + n 1/2

Recurrences Three basic behaviors –Dominated by initial case –Dominated by base case –All cases equal – we care about the depth

Solve by unrolling T(n) = n + 5T(n/2)

What you really need to know about recurrences Work per level changes geometrically with the level Geometrically increasing (x > 1) –The bottom level wins Geometrically decreasing (x < 1) –The top level wins Balanced (x = 1) –Equal contribution

Classify the following recurrences (Increasing, Decreasing, Balanced) T(n) = n + 5T(n/8) T(n) = n + 9T(n/8) T(n) = n 2 + 4T(n/2) T(n) = n 3 + 7T(n/2) T(n) = n 1/2 + 3T(n/4)

Strassen’s Algorithm Multiply 2 x 2 Matrices: | r s | | a b| |e g| | t u| | c d| | f h| r = p 1 + p 4 – p 5 + p 7 s = p 3 + p 5 t = p 2 + p 5 u = p 1 + p 3 – p 2 + p 7 Where: p 1 = (b + d)(f + g) p 2 = (c + d)e p 3 = a(g – h) p 4 = d(f – e) p 5 = (a – b)h p 6 = (c – d)(e + g) p 7 = (b – d)(f + h) =

Recurrence for Strassen’s Algorithms T(n) = 7 T(n/2) + cn 2 What is the runtime?

BFPRT Recurrence T(n) <= T(3n/4) + T(n/5) + 20 n What bound do you expect?