Divide-and-Conquer 7 2  9 4   2   4   7

Slides:



Advertisements
Similar presentations
Divide-and-Conquer 7 2  9 4   2   4   7
Advertisements

Divide-and-Conquer CIS 606 Spring 2010.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
A simple example finding the maximum of a set S of n numbers.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Strassen's Matrix Multiplication Sibel KIRMIZIGÜL.
Introduction to Algorithms Jiafen Liu Sept
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Foundations of Algorithms, Fourth Edition
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.
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.
Analysis of Recursive Algorithms
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Fundamental Techniques
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
Divide-and-Conquer 7 2  9 4   2   4   7
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter 5 – 2. Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 3 Prof. Erik Demaine.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures and Algorithms A. G. Malamos
Project 2 due … Project 2 due … Project 2 Project 2.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
File Organization and Processing Week 13 Divide and Conquer.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Foundations II: Data Structures and Algorithms
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.
Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Merge Sort 1/12/2018 9:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Introduction to Algorithms: Divide-n-Conquer Algorithms
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Introduction to Algorithms
Mathematical Foundations (Solving Recurrence)
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
Chapter 4 Divide-and-Conquer
Design and Analysis of Algorithms
More on Merge Sort CS 244 This presentation is not given in class
CSCE 411 Design and Analysis of Algorithms
Algorithms and Data Structures Lecture III
Data Structures Review Session
Divide-and-Conquer 7 2  9 4   2   4   7
Ch 4: Recurrences Ming-Te Chi
CS 3343: Analysis of Algorithms
Introduction to Algorithms
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Merge Sort 2/24/2019 6:07 PM Merge Sort 7 2   7  2  2 7
Divide-and-Conquer 7 2  9 4   2   4   7
Trevor Brown CS 341: Algorithms Trevor Brown
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Divide & Conquer Sorting
Algorithms Recurrences.
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
Divide and Conquer Merge sort and quick sort Binary search
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Divide-and-Conquer 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 Merge Sort 6/2/2019 4:51 AM Divide-and-Conquer 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4 Spring 2008 Divide-and-Conquer

Outline and Reading Divide-and-conquer paradigm (§5.2) Merge Sort 6/2/2019 4:51 AM Outline and Reading Divide-and-conquer paradigm (§5.2) Review Merge-sort (§4.1.1) Recurrence Equations (§5.2.1) Iterative substitution Recursion trees Guess-and-test The master method Integer Multiplication (§5.2.2) Spring 2008 Divide-and-Conquer

Merge Sort 6/2/2019 4:51 AM Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two or more disjoint subsets S1, S2, … Recur: solve the subproblems recursively Conquer: combine the solutions for S1, S2, …, into a solution for S The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations Spring 2008 Divide-and-Conquer

Merge Sort 6/2/2019 4:51 AM Merge-Sort Review Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S1 and S2 of about n/2 elements each Recur: recursively sort S1 and S2 Conquer: merge S1 and S2 into a unique sorted sequence Algorithm mergeSort(S, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S1, S2)  partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) S  merge(S1, S2) Spring 2008 Divide-and-Conquer

Recurrence Equation Analysis Merge Sort 6/2/2019 4:51 AM Recurrence Equation Analysis The conquer step of merge-sort consists of merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b. Likewise, the basis case (n < 2) will take at b most steps. Therefore, if we let T(n) denote the running time of merge-sort: We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation. That is, a solution that has T(n) only on the left-hand side. Spring 2008 Divide-and-Conquer

Iterative Substitution Merge Sort 6/2/2019 4:51 AM Iterative Substitution In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern: Note that base, T(1)=b, case occurs when 2i=n. That is, i = log n. So, Thus, T(n) is O(n log n). Spring 2008 Divide-and-Conquer

(last level plus all previous levels) The Recursion Tree Draw the recursion tree for the recurrence relation and look for a pattern: time bn … depth T’s size 1 n 2 n/2 i 2i n/2i … Total time = bn + bn log n (last level plus all previous levels) Spring 2008 Divide-and-Conquer

Guess-and-Test Method In the guess-and-test method, we guess a closed form solution and then try to prove it is true by induction: Guess: T(n) < cn log n. Wrong: we cannot make this last line be less than cn log n Spring 2008 Divide-and-Conquer

Guess-and-Test Method, Part 2 Recall the recurrence equation: Guess #2: T(n) < cn log2 n. if c > b. So, T(n) is O(n log2 n). In general, to use this method, you need to have a good guess and you need to be good at induction proofs. Spring 2008 Divide-and-Conquer

Master Method Many divide-and-conquer recurrence equations have the form: The Master Theorem: Spring 2008 Divide-and-Conquer

Solution: logba=2, so case 1 says T(n) is Θ(n2). Master Method, Example 1 The form: The Master Theorem: Example: Solution: logba=2, so case 1 says T(n) is Θ(n2). Spring 2008 Divide-and-Conquer

Solution: logba=1, so case 2 says T(n) is Θ(n log2 n). Master Method, Example 2 The form: The Master Theorem: Example: Solution: logba=1, so case 2 says T(n) is Θ(n log2 n). Spring 2008 Divide-and-Conquer

Solution: logba=0, so case 3 says T(n) is Θ(n log n). Master Method, Example 3 The form: The Master Theorem: Example: Solution: logba=0, so case 3 says T(n) is Θ(n log n). Spring 2008 Divide-and-Conquer

Solution: logba=3, so case 1 says T(n) is Θ(n3). Master Method, Example 4 The form: The Master Theorem: Example: Solution: logba=3, so case 1 says T(n) is Θ(n3). Spring 2008 Divide-and-Conquer

Solution: logba=2, so case 3 says T(n) is Θ(n3). Master Method, Example 5 The form: The Master Theorem: Example: Solution: logba=2, so case 3 says T(n) is Θ(n3). Spring 2008 Divide-and-Conquer

Solution: logba=0, so case 2 says T(n) is Θ(log n). Master Method, Example 6 The form: The Master Theorem: Example: (binary search) Solution: logba=0, so case 2 says T(n) is Θ(log n). Spring 2008 Divide-and-Conquer

Solution: logba=1, so case 1 says T(n) is Θ(n). Master Method, Example 7 The form: The Master Theorem: Example: (heap construction) Solution: logba=1, so case 1 says T(n) is Θ(n). Spring 2008 Divide-and-Conquer

Iterative “Proof” of the Master Theorem Using iterative substitution, let us see if we can find a pattern: Spring 2008 Divide-and-Conquer

Iterative “Proof” of the Master Theorem Merge Sort 6/2/2019 4:51 AM Iterative “Proof” of the Master Theorem We then distinguish the three cases as The first term is dominant Each part of the summation is equally dominant The summation is a geometric series Spring 2008 Divide-and-Conquer

Integer Multiplication Algorithm: Multiply two n-bit integers I and J. Divide step: Split I and J into high-order and low-order bits We can then define I*J by multiplying the parts and adding: So, T(n) = 4T(n/2) + n, which implies T(n) is Θ(n2). But that is no better than the algorithm we learned in grade school. where did this come from? Spring 2008 Divide-and-Conquer

An Improved Integer Multiplication Algorithm Algorithm: Multiply two n-bit integers I and J. Divide step: Split I and J into high-order and low-order bits Observe that there is a different way to multiply parts: So, T(n) = 3T(n/2) + n, which implies T(n) is O(nlog23), by the Master Theorem. Thus, T(n) is O(n1.585). requires only 3 mults! Spring 2008 Divide-and-Conquer

Matrix Multiplication Given n x n matrices X and Y, wish to compute the product Z=XY. Formula for doing this is This runs in O(n3) time In fact, multiplying an n x m by an m x q takes nmq operations Spring 2008 Divide-and-Conquer

Matrix Multiplication Spring 2008 Divide-and-Conquer

Matrix Multiplication Using the decomposition on previous slide, we can computer Z using 8 recursively computed (n/2)x(n/2) matrices plus four additions that can be done in O(n2) time Thus T(n) = 8T(n/2) + bn2 Still gives T(n) is Θ(n3) Spring 2008 Divide-and-Conquer

Strassen’s Algorithm If we define the matrices S1 through S7 as follows Spring 2008 Divide-and-Conquer

Strassen’s Algorithm Then we get the following: So now we can compute Z=XY using only seven recursive multiplications Spring 2008 Divide-and-Conquer

Strassen’s Algorithm This gives the relation T(n)=7T(n/2)+bn2 for some b>0. By the Master Theorem, we can thus multiply two n x n matrices in Θ(nlog7) time, which is approximately Θ(n2.808) . May not seem like much, but if you’re multiplying two 100 x 100 matrices: n3 is 1,000,000 n2.808 is 413,048 With added complexity, there are algorithms to multiply matrices in as little as Θ(n2.376) time Reduces figures above to 56,494 Spring 2008 Divide-and-Conquer