CPSC 320: Intermediate Algorithm Design & Analysis Divide & Conquer and Recurrences Steve Wolfman 1.

Slides:



Advertisements
Similar presentations
Divide and Conquer (Merge Sort)
Advertisements

A simple example finding the maximum of a set S of n numbers.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
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.
1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.
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.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
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.
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
CS 253: Algorithms Chapter 4 Divide-and-Conquer Recurrences Master Theorem Credit: Dr. George Bebis.
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.
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
Induction and recursion
Analysis of Recursive Algorithms October 29, 2014
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
Analysis of Algorithms
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
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.
1 Section 5.5 Solving Recurrences Any recursively defined function ƒ with domain N that computes numbers is called a recurrence or recurrence relation.
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.
Introduction to Algorithms Chapter 4: Recurrences.
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]
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences.
Foundations II: Data Structures and Algorithms
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
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.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Equal costs at all levels
Divide-and-Conquer 6/30/2018 9:16 AM
Unit 1. Sorting and Divide and Conquer
Chapter 4 Divide-and-Conquer
CSCE 411 Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Ch 4: Recurrences Ming-Te Chi
Divide and Conquer Algorithms Part I
Divide and Conquer (Merge Sort)
Divide-and-Conquer 7 2  9 4   2   4   7
Divide & Conquer Algorithms
Algorithms Recurrences.
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
Divide-and-Conquer 7 2  9 4   2   4   7
Divide and Conquer Merge sort and quick sort Binary search
Algorithms and Data Structures Lecture III
Presentation transcript:

CPSC 320: Intermediate Algorithm Design & Analysis Divide & Conquer and Recurrences Steve Wolfman 1

Problem-Solving Approaches Many problems can be solved by the same broad style of approach. We’ll run into several of these styles: – Input consuming (like insertion sort) – Output producing (like selection sort) – Divide-and-Conquer (like merge sort) – Greedy – Dynamic Programming 2

Divide-and-Conquer Approach When a larger problem can be divided into similar sub-problems, it’s often possible to solve the larger problem by: – dividing it into smaller pieces – recursively solving the pieces – assembling the larger solution from the smaller ones 3

Merge Sort Reminder How can we sort a list by divide-and-conquer? – Break the list into two (roughly) equal-sized pieces – Sort the pieces (using Merge Sort) – Merge the two sorted lists back together We need a base case for the recursion to “bottom out”! Fortunately, any list of length 1 is already sorted. 4

Skyline Problem 5

Analyzing Iterative Algorithms We label lines that take constant time and build up summations to represent loops. Together, these form an equation like T(n) that indicates the number of “simple computational steps” to solve a problem of size n. 6

Analyzing Recursive Algorithms We label lines that take constant time and build up summations to represent loops. Together, these form an equation like T(n) that indicates the number of “simple computational steps” to solve a problem of size n. We already have a formula for how long a recursive call takes: T(. ) Just add in a T(. ) term with an appropriate size argument based on how large a “piece” we pass to the recursive call. 7

Recurrence Example: FindMax FindMax(arr) if arr.length == 1: return arr[0] else: return larger of arr[0] and FindMax(arr[1..arr.length-1]) T(1) <= b T(n) 1 8

Recurrence Example: FindMax T(1) <= b T(n) 1 Analysis: T(n) <= c + c + T(n - 2) (by substitution) T(n) <= c + c + c + T(n - 3) (by substitution, again) T(n) <= kc + T(n - k) (extrapolating 0 < k  n ) T(n) <= (n – 1)c + T(1) = (n – 1)c + b (for k = n – 1 ) 9

Analyzing Recurrence Relations: Approaches Guess-and-Test Guess the solution (magically), prove that it works by induction. Requires a magical guess! Repeated Substitution/Recursion Tree Either repeatedly substitute the recursive definition for recursive terms or draw out the tree of recursive calls generated, annotating each level by the time spent on that level. Devise an expression for the total time. Master Method Essentially a summary of common recursion tree patterns. Completely mechanical, but does not always apply! 10

Analysis of Skyline Problem 11

More Examples + Pitfalls Skyline, Guess-and-Test T(n)  O(n) T(n) = T(  n/2  ) + T(  n/2  ) + 1, T(1) = 1, Guess-and-Test T(n)  O(n) T(n) = 3T(n/4) + n 2, T(1) = 1 T(n) = T(n/2) + 1, T(1) = 1 12

Master Method: Context Let a  1 and b > 1 be constants and f(n) : N  R + and T(n) be defined by: T(n) = aT(“n/b”) + f(n) where T(n)  Θ(1) for sufficiently small n, and “n/b” can be  n/b  or  n/b . 13

Master Method: Cases Assuming the previous slide’s context holds: (1)If f(n)  for some  > 0, then T(n)  (2)If f(n)  then, T(n)  (3)If f(n)  for some  > 0 and if af(“n/b”)  cf(n) for some constant c < 1 and sufficiently large n, then T(n)  14 Dominated by “leaf” cost. “Balanced” cost. Dominated by “root” cost.

More Examples (if time available) Integer Multiplication Closest Pair of Points, 1-D Closest Pair of Points, 2-D 15

What’s Next? Selection (and a bit of Randomized Algorithms): CLRS Chapter 9 16

On Your Own PRACTICE various analysis methods, particularly use of recursion trees/repeated substitution and the Master Method. MANY practice problems available in CLRS Review recursion 17