1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.

Slides:



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

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.
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Strassen's Matrix Multiplication Sibel KIRMIZIGÜL.
Introduction to Algorithms Jiafen Liu Sept
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
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.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CPSC 320: Intermediate Algorithm Design & Analysis Divide & Conquer and Recurrences Steve Wolfman 1.
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.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
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.
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).
Fundamental Techniques
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
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.
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
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter 5 – 2. Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
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.
1 Chapter 4 Divide-and-Conquer. 2 About this lecture Recall the divide-and-conquer paradigm, which we used for merge sort: – Divide the problem into a.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
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
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.
Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
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
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
CSCE 411 Design and Analysis of Algorithms
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
CSE 2010: Algorithms and Data Structures
Introduction to Algorithms
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
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-and-Conquer 7 2  9 4   2   4   7
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the Merge-sort algorithm Solve recurrence equations –Iterative substitution –Recursion trees –Guess-and-test –The master method Discuss integer and matrix multiplications

2 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: –Divide: divide the input data S in two or more disjoint subsets S 1, S 2, … –Recur: solve the subproblems recursively –Conquer: combine the solutions for S 1, S 2, …, into a solution for S The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations

3 Merge-Sort Review Merge-sort on an input sequence S with n elements consists of three steps: –Divide: partition S into two sequences S 1 and S 2 of about n  2 elements each –Recur: recursively sort S 1 and S 2 –Conquer: merge S 1 and S 2 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 (S 1, S 2 )  partition(S, n/2) mergeSort(S 1, C) mergeSort(S 2, C) S  merge(S 1, S 2 ) 7 2  9 4   2  2 79  4   72  29  94  4

4 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.

5 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(n)=b, case occurs when 2 i =n. That is, i = log n. So, Thus, T(n) is O(n log n).

6 The Recursion Tree Draw the recursion tree for the recurrence relation and look for a pattern: depthT’ssize01n 12 n2n2n2n2 i 2i2i2i2i n2in2in2in2i ……… timebn bn bn … Total time = bn + bn log n (last level plus all previous levels)

7 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) < cnlogn. Wrong: we cannot make this last line be less than cn log n

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

9 Master Method Many divide-and-conquer recurrence equations have the form: The Master Theorem:

10 Master Method, Examples The form: The Master Theorem: Example 1: –Solution: a=4, b=2, log b a=2, f(n)=n, so case 1 says T(n) is O(n2). Example 2: –Solution: a=b=2, log b a=1, f(n)=nlogn, so case 2 says T(n) is O(n log2 n).

11 Master Method, Examples The form: The Master Theorem: Example 3: –Solution: a=1, b=3, log b a=0, f(n)=nlogn, so case 3 says T(n) is O(nlogn). Example 4: –Solution: a=2, b=8, log b a=3, f(n)=n 2, so case 1 says T(n) is O(n 3 ).

12 Master Method, Examples The form: The Master Theorem: Example 5: –Solution: a=9, b=3, log b a=2, f(n)=n 3, so case 3 says T(n) is O(n 3 ). Example 6: (binary search) –Solution: a=1, b=2, log b a=0, f(n)=1, so case 2 says T(n) is O(log n). Example 7: (heap construction) –Solution: a=2, b=2, log b a=1, f(n)=logn, so case 1 says T(n) is O(n).

13 Iterative “Proof” of the Master Theorem Using iterative substitution, let us see if we can find a pattern: 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

14 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 O(n 2 ). –But that is no better than the algorithm we learned in grade school.

15 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(n log 2 3 ), by the Master Theorem. –Thus, T(n) is O(n ).

16 Matrix Multiplication Algorithm Problem: Given n×n matrices X and Y, find their product Z=X×Y, General algorithm: O(n 3 ) Strassen’s Algorithm: –Rewrite the matrix product as: –Where I=AE+BG, J=AF+BH K=CE+DG, L=CF+DH –Define 7 submatrix products: S1=A(F-H), S2=(A+B)H, S3=(C+D)E, S4=D(G-E) S5=(A+D)(E+H), S6=(B-D)(G+H), S7=(A-C)(E+F) –Construct: I=S4+S5+S6-S2, J=S1+S2, K=S3+S4, L=S1-S7- S3-S5 –Running time: T(n) = 7T(n/2)+bn 2 for some constant b, which gives: O(n log7 ), better than O(n 3 )