We think you have liked this presentation. If you wish to download it, please recommend it to your friends in any social system. Share buttons are a little bit lower. Thank you!
Presentation is loading. Please wait.
Published byJenifer Blow
Modified over 2 years ago
© 2004 Goodrich, Tamassia Divide-and-Conquer
© 2004 Goodrich, Tamassia Divide-and-Conquer2 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
© 2004 Goodrich, Tamassia Divide-and-Conquer3 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 )
© 2004 Goodrich, Tamassia Divide-and-Conquer4 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.
© 2004 Goodrich, Tamassia Divide-and-Conquer5 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).
© 2004 Goodrich, Tamassia Divide-and-Conquer6 The Recursion Tree Draw the recursion tree for the recurrence relation and look for a pattern: depthTssize 01n 12 n 2 i2i2i n 2 i ……… time bn … Total time = bn + bn log n (last level plus all previous levels)
© 2004 Goodrich, Tamassia Divide-and-Conquer7 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
© 2004 Goodrich, Tamassia Divide-and-Conquer8 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.
© 2004 Goodrich, Tamassia Divide-and-Conquer9 Master Method (Appendix) Many divide-and-conquer recurrence equations have the form: The Master Theorem:
© 2004 Goodrich, Tamassia Divide-and-Conquer10 Master Method, Example 1 The form: The Master Theorem: Example: Solution: log b a=2, so case 1 says T(n) is O(n 2 ).
© 2004 Goodrich, Tamassia Divide-and-Conquer11 Master Method, Example 2 The form: The Master Theorem: Example: Solution: log b a=1, so case 2 says T(n) is O(n log 2 n).
© 2004 Goodrich, Tamassia Divide-and-Conquer12 Master Method, Example 3 The form: The Master Theorem: Example: Solution: log b a=0, so case 3 says T(n) is O(n log n).
© 2004 Goodrich, Tamassia Divide-and-Conquer13 Master Method, Example 4 The form: The Master Theorem: Example: Solution: log b a=3, so case 1 says T(n) is O(n 3 ).
© 2004 Goodrich, Tamassia Divide-and-Conquer14 Master Method, Example 5 The form: The Master Theorem: Example: Solution: log b a=2, so case 3 says T(n) is O(n 3 ).
© 2004 Goodrich, Tamassia Divide-and-Conquer15 Master Method, Example 6 The form: The Master Theorem: Example: Solution: log b a=0, so case 2 says T(n) is O(log n). (binary search)
© 2004 Goodrich, Tamassia Divide-and-Conquer16 Master Method, Example 7 The form: The Master Theorem: Example: Solution: log b a=1, so case 1 says T(n) is O(n). (heap construction)
© 2004 Goodrich, Tamassia Divide-and-Conquer17 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
© 2004 Goodrich, Tamassia Divide-and-Conquer18 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.
© 2004 Goodrich, Tamassia Divide-and-Conquer19 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 ).
Analysis of Algorithms II The Recursive Case Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
Divide-and-Conquer CIS 606 Spring Analyzing Divide-and-Conquer Algorithms Use a recurrence to characterize the running time of a divide-and-conquer.
Jan Divide and Conquer (Merge Sort) Divide and conquer Merge sort Loop-invariant Recurrence relations.
Quicksort Introduction to Algorithms Quicksort CSE 680 Prof. Roger Crawfis.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CPSC 411 Design and Analysis of Algorithms Set 3: Divide and Conquer Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 3 1.
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
: 1 Recurrences. Contents of Table Recurrence solution methods The substitution method The recursion-tree method 2 Relationship between asymptotic.
: 1 Chapter 3. Growth of function Chapter 4. Recurrences.
Input sizeTime I1I1 T1T1 I2I2 T2T2 …… Algorithm Analysis We want to quantify the behavior of an algorithm. Useful to compare efficiency of two algorithms.
Mathematical Induction Rosen 3.3. Basics The Well-Ordering Property - Every nonempty set of nonnegative integers has a least element. Many theorems state.
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
September 12, 2005 Copyright© Erik D. Demaine and Charles E. Leiserson L2.1 Introduction to Algorithms LECTURE2 Asymptotic Notation O-, Ω-, and Θ-notation.
Growth-rate Functions O(1) – constant time, the time is independent of n, e.g. array look-up O(log n) – logarithmic time, usually the log is base 2, e.g.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 2 Prof. Erik Demaine.
22-Jan-14 Dynamic Programming. 2 Counting coins To find the minimum number of US coins to make any amount, the greedy method always works At each step,
110/6/2014CSE Suprakash Datta datta[at]cse.yorku.ca CSE 3101: Introduction to the Design and Analysis of Algorithms.
Daily Quiz - Simplify the expression, then create your own realistic scenario for the final expression.
1 Computational Complexity Size Matters!. 2 Suppose there are several algorithms which can all be used to perform the same task. We need some way to judge.
Completeness and Expressiveness. Consistency: a syntactic definition, related to the proof system. A set of formulas is consistent if there is no formula.
©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Copyright © Cengage Learning. All rights reserved. 1.6 Inverse Functions.
CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 4 1.
Great Theoretical Ideas in Computer Science for Some.
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Data Structures and Algorithms Courses slides: Sorting Algorithms
© 2004 Goodrich, Tamassia Analysis of Algorithms Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount.
Ch 3.2: Solutions of Linear Homogeneous Equations; Wronskian Let p, q be continuous functions on an interval I = ( , ), which could be infinite. For.
With examples from Number Theory (Rosen 1.5, 3.1, sections on methods of proving theorems and fallacies)
© 2016 SlidePlayer.com Inc. All rights reserved.