Data Structures and Algorithms CMPSC 465

Similar documents
What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Data Structures and Algorithms CMPSC 465

Design and Analysis of Algorithms Recurrence. Prof. Chuhua Xian School of Computer Science and Engineering

Algorithm Design and Analysis

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

Analysis of Algorithms - Using Asymptotic Bounds -

Algorithm Design and Analysis

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Algorithms Design & Analysis. Analysis of Algorithm

Data Structures and Algorithms Chapter 3

Data Structures and Algorithms CSE 465

Chapter 4. Recurrences

Data Structures and Algorithms CSE 465

Recurrence Relations

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Algorithms Chapter 4 Recurrences

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

Introduction to Algorithms 6.046J/18.401J/SMA5503

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Algorithm efficiency analysis

Data Structures and Algorithms Chapter 3

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Objective. - mathematical induction, recursive definitions - arithmetic manipulations, series, products

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

A SUMMARY OF RECURSION SOLVING TECHNIQUES

Asymptotic Algorithm Analysis & Sorting

Divide and Conquer. Andreas Klappenecker

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Analysis of Multithreaded Algorithms

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Fast Convolution; Strassen s Method

Analysis of Algorithms CMPSC 565

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Intro to Theory of Computation

Chapter 4 Divide-and-Conquer

CSC236 Week 4. Larry Zhang

Data Structures and Algorithm. Xiaoqing Zheng

Methods for solving recurrences

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Growth of Functions (CLRS 2.3,3)

Asymptotic Analysis and Recurrences

ECE250: Algorithms and Data Structures Analyzing and Designing Algorithms

Data Structures and Algorithms

Lecture 4. Quicksort

CS473 - Algorithms I

CS 2110: INDUCTION DISCUSSION TOPICS

COE428 Notes Week 4 (Week of Jan 30, 2017)

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Introduction to Algorithms 6.046J/18.401J

Design and Analysis of Algorithms

1 Substitution method

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Design and Analysis of Algorithms

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Review Of Topics. Review: Induction

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

COMP Analysis of Algorithms & Data Structures

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

COMP 382: Reasoning about algorithms

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

6.046 Recitation 2: Recurrences Bill Thies, Fall 2004 Outline

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Analysis of Algorithms Generating Functions II. Holger Findling

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Analysis of Algorithms. Randomizing Quicksort

Announcements. CompSci 230 Discrete Math for Computer Science. The Growth of Functions. Section 3.2

Practical Session #3 - Recursions

CS583 Lecture 02. Jana Kosecka. some materials here are based on E. Demaine, D. Luebke slides

Divide and Conquer. Arash Rafiey. 27 October, 2016

CS 240A : Examples with Cilk++

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

CS F-01 Algorithm Analysis 1

Recap & Interval Scheduling

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Notes for Recitation 14

4.4 Recurrences and Big-O

Ch01. Analysis of Algorithms

Lecture 6: Recurrent Algorithms:

Introduction to Divide and Conquer

Lecture 3. Big-O notation, more recurrences!!

CSC Design and Analysis of Algorithms. Lecture 1

Data Structures and Algorithms Running time and growth functions January 18, 2018

Computational Complexity

COMP 9024, Class notes, 11s2, Class 1

CS 61B Asymptotic Analysis Fall 2017

Transcription:

Data Structures and Algorithms CMPSC 465 LECTURE 9 Solving recurrences Substitution method Adam Smith S. Raskhodnikova and A. Smith; based on slides by E. Demaine and C. Leiserson

Review question Draw the recursion tree for the following recurrence: T(n) = 4T(n/2) + c What is the depth? How many leaves? Guess T(n)?

Methods for solving recurrences Recursion tree Examples in last two lectures Substitution method This lecture Master theorem

Substitution method The most general method: 1.Guess the form of the solution. 2.Verify by induction. 3.Solve for constants.

Substitution method The most general method: 1.Guess the form of the solution. 2.Verify by induction. 3.Solve for constants. EXAMPLE: T(n) = 4T(n/2) + n [Assume that T(1) = Θ(1).] Guess O(n 3 ). (Prove O and Ω separately.) Assume that T(k) ck 3 for k < n. Prove T(n) cn 3 by induction.

Example of substitution T ( n) = 4T ( n / 2) + n 4c( n / 2) 3 + n = ( c / 2) n3 + n = cn3 (( c / 2) n3 n) desired residual cn3 desired whenever (c/2)n 3 n 0, for example, if c 2 and n 1. residual

Example (continued) We must also handle the initial conditions, that is, ground the induction with base cases. Base: T(n) = Θ(1) for all n < n 0, where n 0 is a suitable constant. For 1 n < n 0, we have Θ(1) cn 3, if we pick c big enough.

Example (continued) We must also handle the initial conditions, that is, ground the induction with base cases. Base: T(n) = Θ(1) for all n < n 0, where n 0 is a suitable constant. For 1 n < n 0, we have Θ(1) cn 3, if we pick c big enough. This bound is not tight!

A tighter upper bound? We shall prove that T(n) = O(n 2 ).

A tighter upper bound? We shall prove that T(n) = O(n 2 ). Assume that T(k) ck 2 for k < n: T ( n) = 4T ( n / 2) + n 2 4c( n / 2) + n 2 = cn + n 2 = O( n )

A tighter upper bound? We shall prove that T(n) = O(n 2 ). Assume that T(k) ck 2 for k < n: T ( n) = 4T ( n / 2) + n 2 4c( n / 2) + n 2 = cn + n 2 = O( n ) Wrong! We must prove the inductive hypothesis

A tighter upper bound? We shall prove that T(n) = O(n 2 ). Assume that T(k) ck 2 for k < n: T ( n) = 4T ( n / 2) + n 2 4c( n / 2) + n 2 = cn + n 2 = O( n ) Wrong! We must prove the I.H. = cn2 ( n) [ desired residual ] cn2 for no choice of c > 0. Lose!

A tighter upper bound! IDEA: Strengthen the inductive hypothesis. Subtract a low-order term. Inductive hypothesis: T(k) c 1 k 2 c 2 k for k < n.

A tighter upper bound! IDEA: Strengthen the inductive hypothesis. Subtract a low-order term. Inductive hypothesis: T(k) c 1 k 2 c 2 k for k < n. T(n) = 4T(n/2) + n 4(c 1 (n/2) 2 c 2 (n/2)) + n = c 1 n 2 2c 2 n + n = c 1 n 2 c 2 n (c 2 n n) c 1 n 2 c 2 n if c 2 1.

A tighter upper bound! IDEA: Strengthen the inductive hypothesis. Subtract a low-order term. Inductive hypothesis: T(k) c 1 k 2 c 2 k for k < n. T(n) = 4T(n/2) + n 4(c 1 (n/2) 2 c 2 (n/2)) + n = c 1 n 2 2c 2 n + n = c 1 n 2 c 2 n (c 2 n n) c 1 n 2 c 2 n if c 2 1. Pick c 1 big enough to handle the initial conditions.

A matching lower bound IDEA: Formulate inductive hypothesis with Inductive hypothesis: T(k) ck 2 for all k < n. T(n) = 4T(n/2) + n 4 cn 2 /4 + n = cn 2 + n cn 2 for any c 0. Pick c small enough to handle the initial conditions.

Appendix: geometric series 1 1 1 2 x x x = + + + for x < 1 1 1 1 1 2 x x x x x n n = + + + + + for x 1

Data Structures and Algorithms CMPSC 465 LECTURE 10? Solving recurrences Master theorem Adam Smith S. Raskhodnikova and A. Smith; based on slides by E. Demaine and C. Leiserson

Review questions Guess the solution to the recurrence: T(n)=2T(n/3)+n 3/2. (Answer: Θ(n 3/2 ).) Draw the recursion tree for this recurrence. a. What is its height? b. What is the number of leaves in the tree? (Answer: h=log 3 n.) (Answer: n (1/log 3).)

The master method The master method applies to recurrences of the form T(n) = a T(n/b) + f (n), where a 1, b > 1, and f is asymptotically positive, that is f (n) >0 for all n > n 0.

Three common cases Compare f (n) with n log ba : 1. f (n) = O(n log ba ε ) for some constant ε > 0. f (n) grows polynomially slower than n log ba (by an n ε factor). Solution: T(n) = Θ(n log ba ).

Three common cases Compare f (n) with n log ba : 1. f (n) = O(n log ba ε ) for some constant ε > 0. f (n) grows polynomially slower than n log ba (by an n ε factor). Solution: T(n) = Θ(n log ba ). 2. f (n) = Θ(n log ba lg k n) for some constant k 0. f (n) and n log ba grow at similar rates. Solution: T(n) = Θ(n log ba lg k+1 n).

Three common cases (cont.) Compare f (n) with n log ba : 3. f (n) = Ω(n log ba + ε ) for some constant ε > 0. f (n) grows polynomially faster than n log ba (by an n ε factor), and f (n) satisfies the regularity condition that a f (n/b) c f (n) for some constant c < 1. Solution: T(n) = Θ( f (n) ).

Idea of master theorem Recursion tree: f (n) a f (n/b) f (n/b) a f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) Τ (1)

Idea of master theorem Recursion tree: f (n) a f (n/b) f (n/b) a f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n) a f (n/b) a 2 f (n/b 2 ) Τ (1)

Idea of master theorem h = log b n Recursion tree: f (n) a f (n/b) f (n/b) a f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n) a f (n/b) a 2 f (n/b 2 ) Τ (1)

Idea of master theorem h = log b n Recursion tree: f (n) a f (n/b) f (n/b) a f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n) a f (n/b) a 2 f (n/b 2 ) Τ (1) #leaves = a h = a log bn = n log ba n log ba Τ (1)

Idea of master theorem h = log b n Recursion tree: f (n) a f (n/b) f (n/b) a f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n) a f (n/b) a 2 f (n/b 2 ) Τ (1) CASE 1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. n log ba Τ (1) Θ(n log ba )

Idea of master theorem h = log b n Recursion tree: f (n) a f (n/b) f (n/b) a f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n) a f (n/b) a 2 f (n/b 2 ) Τ (1) CASE 2: (k = 0) The weight is approximately the same on each of the log b n levels. n log ba Τ (1) Θ(n log ba lg n)

Idea of master theorem h = log b n Recursion tree: f (n) a f (n/b) f (n/b) a f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n) a f (n/b) a 2 f (n/b 2 ) Τ (1) CASE 3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. n log ba Τ (1) Θ( f (n))

Examples EX. T(n) = 4T(n/2) + n a = 4, b = 2 n log ba = n 2 ; f (n) = n. CASE 1: f (n) = O(n 2 ε ) for ε = 1. T(n) = Θ(n 2 ).

Examples EX. T(n) = 4T(n/2) + n a = 4, b = 2 n log ba = n 2 ; f (n) = n. CASE 1: f (n) = O(n 2 ε ) for ε = 1. T(n) = Θ(n 2 ). EX. T(n) = 4T(n/2) + n 2 a = 4, b = 2 n log ba = n 2 ; f (n) = n 2. CASE 2: f (n) = Θ(n 2 lg 0 n), that is, k = 0. T(n) = Θ(n 2 lg n).

Examples EX. T(n) = 4T(n/2) + n 3 a = 4, b = 2 n log ba = n 2 ; f (n) = n 3. CASE 3: f (n) = Ω(n 2 + ε ) for ε = 1 and 4(n/2) 3 cn 3 (reg. cond.) for c = 1/2. T(n) = Θ(n 3 ).

Examples EX. T(n) = 4T(n/2) + n 3 a = 4, b = 2 n log ba = n 2 ; f (n) = n 3. CASE 3: f (n) = Ω(n 2 + ε ) for ε = 1 and 4(n/2) 3 cn 3 (reg. cond.) for c = 1/2. T(n) = Θ(n 3 ). EX. T(n) = 4T(n/2) + n 2 /lg n a = 4, b = 2 n log ba = n 2 ; f (n) = n 2 /lg n. Master method does not apply. In particular, for every constant ε > 0, we have n ε = ω(lg n).