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

Similar documents
Data Structures and Algorithms CMPSC 465

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

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

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:

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

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

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

Data Structures and Algorithms Chapter 3

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

Analysis of Algorithms - Using Asymptotic Bounds -

Algorithms Design & Analysis. Analysis of Algorithm

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Algorithms Chapter 4 Recurrences

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

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

1 Substitution method

Algorithm Design and Analysis

Divide and Conquer. Recurrence Relations

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

Algorithms. Adnan YAZICI Dept. of Computer Engineering Middle East Technical Univ. Ankara - TURKEY. Algorihms, A.Yazici, Fall 2007 CEng 315

Module 1: Analyzing the Efficiency of Algorithms

Asymptotic Algorithm Analysis & Sorting

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

Divide and Conquer. Andreas Klappenecker

Recurrence Relations

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

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

Algorithm efficiency analysis

CS Analysis of Recursive Algorithms and Brute Force

CS 2110: INDUCTION DISCUSSION TOPICS

Advanced Counting Techniques. Chapter 8

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

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

Methods for solving recurrences

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

Chapter 4 Divide-and-Conquer

A SUMMARY OF RECURSION SOLVING TECHNIQUES

Analysis of Algorithms CMPSC 565

CS Non-recursive and Recursive Algorithm Analysis

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

CS Data Structures and Algorithm Analysis

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

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

Principles of Algorithm Analysis

Asymptotic Analysis and Recurrences

Module 1: Analyzing the Efficiency of Algorithms

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

Data Structures and Algorithms CSE 465

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

Data Structures and Algorithms Chapter 3

Data Structures and Algorithms

COMP Analysis of Algorithms & Data Structures

Binomial Coefficient Identities/Complements

Data Structures and Algorithms CMPSC 465

COMP Analysis of Algorithms & Data Structures

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

Data Structures and Algorithm. Xiaoqing Zheng

Practical Session #3 - Recursions

Review Of Topics. Review: Induction

Computational Complexity

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

CS 4349 Lecture August 30th, 2017

CS Asymptotic Notations for Algorithm Analysis

CS F-01 Algorithm Analysis 1

Recurrence Relations

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

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

with the size of the input in the limit, as the size of the misused.

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

Analysis of Multithreaded Algorithms

CSCI 3110 Assignment 6 Solutions

6.046 Recitation 2: Recurrences Bill Thies, Fall 2004 Outline

Growth of Functions (CLRS 2.3,3)

CS 310 Advanced Data Structures and Algorithms

Lecture 4. Quicksort

data structures and algorithms lecture 2

Data Structures and Algorithms CSE 465

CSC Design and Analysis of Algorithms. Lecture 1

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

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Chapter 4. Recurrences

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Quiz 3 Reminder and Midterm Results

COMPUTER ALGORITHMS. Athasit Surarerks.

Advanced Counting Techniques

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

CSE 417: Algorithms and Computational Complexity

COMP 9024, Class notes, 11s2, Class 1

Analysis of Algorithms

Recursion: Introduction and Correctness

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

csci 210: Data Structures Program Analysis

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

ECE250: Algorithms and Data Structures Analyzing and Designing Algorithms

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

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Transcription:

Lecture 3 The Analysis of Recursive Algorithm Efficiency

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asymptotic orders of growth: O, Θ and Ω Analyze the time complexity of non-recurrent relation 2-1

Outline Some examples on the time complexity of non-recurrence relation How to analyze the time complexity of recurrent algorithms Substitution Recursion Tree Master Theorem 2-2

Time Complexity of Non-recursive algorithms General Plan for Analysis Decide on parameter n indicating input size Identify algorithm s basic operation Determine worst, average,, and best cases for input of size n St Set up a sum for the number of fti times the basic operation is executed td Simplify the sum using standard formulas and rules (see Appendix A) 2-3

Example 1: Insertion Sort 2-4

2-5

For some algorithms efficiency depends on form of input: Worst case: C worst (n) maximum over inputs of size n Best case: C best(n) minimum over inputs of size n Average case: C avg( (n) average over inputs of size n 2-6

Example 2: Bubble Sort 2-7

Worst case Best case Average case 2-8

Example: Sequential search Worst case Best case Average case 2-9

Useful summation formulas and rules l i u 1 = 1+1+ +1 = u - l + 1 In particular, l i u 1 = n - 1 + 1 = n (n) 1 i n i = 1+2+ +n = n(n+1)/2 n 2 /2 (n 2 ) 1 i n i 2 = 1 2 +2 2 + +n 2 = n(n+1)(2n+1)/6 +1)/6 n 3 /3 (n 3 ) 0 i n a i = 1 + a + + a n = (a n+1-1)/( 1)/(a - 1) for any a 1 In particular, 0 i n 2 i = 2 0 + 2 1 + + 2 n = 2 n+1-1 (2 n ) (a i ± b i ) = a i ± b i ca i = c a i l i u a i = l i m a i + m+1 +1 i ua i 2-10

Plan for Analysis of Recursive Algorithms Decide on a parameter indicating an input s size. Identify the algorithm s basic operation. Check whether the number of times the basic op. is executed may vary on different inputs of the same size. (If it may, the worst, average, and best cases must be investigated separately.) Set up a recurrence relation with an appropriate initial condition expressing the number of times the basic op. is executed. Solve the recurrence (or, at the very least, establish its solution s order of growth) by backward substitutions or another method. 2-11

Example 1: Recursive evaluation of n! Definition: n! = 1 2 (n-1) n for n 1 and 0! = 1 Recursive definition of n!: F(n) ) = F(n-1) n for n 1 and F(0) = 1 Size: Basic operation: Recurrence relation: 2-12

Solving the recurrence for M(n) T(n) = T(n-1) + 1, T(0) = 0 (M is the number of execution of multiplication) T(n) =? 2-13

Example 2: The Tower of Hanoi Puzzle 1 3 2 Recurrence for number of moves: 2-14

Solving recurrence for number of moves T(n) = 2T(n-1) + 1, T(1) = 1 T(n) =? 2-15

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) + 100n [Assume that T(1) = (1).] Guess O(n( 3 ). (Prove O and separately.) y) Assume that T(k) ck 3 for k < n. Prove T(n) ( ) cn 3 by induction. 2-16

Example of substitution T ( n) 4T ( n / 2) 100n 4 c ( n / 2) 3 100n ( c / 2) n3 100n cn 3 (( c / 2) n 3 100n ) desired residual cn3 desired whenever (c/2)n 3 100n 0, for example, if c 200 and n 1. 2-17

Example (continued) We must also handle the initial iti 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 (1) 3 0, we have cn, if we pick c big enough. This bound is not tight! 2-18

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) 100n cn2 100n cn 2 for no choice of c > 0. Lose! 2-19

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 ) 4 T ( n / 2) 100n 4( c ( / 2) 2 1 n c2( n / 2)) 100n c 2 1n 2c2n 100n c 2 1n c 2n ( c 2n 100n ) c 2 1n c2n if c 2 > 100. Pick c 1 big enough to handle the initial iti conditions. 2-20

Recursion-tree method A recursion tree models the costs (time) of a recursive execution of an algorithm. The recursion tree method dis good dfor generating guesses for the substitution method. The recursion-tree method promotes intuition, however. 2-21

Example of recursion tree Solve T(n) ) = T(n/4) + T(n/2) + n 2 : 2-22

Example of recursion tree Solve T(n) ) = T(n/4) + T(n/2) + n 2 : T(n) 2-23

Example of recursion tree Solve T(n) ) = T(n/4) + T(n/2) + n 2 : n 2 T(n/4) ( ) T(n/2) 2-24

Example of recursion tree Solve T(n) ) = T(n/4) + T(n/2) + n 2 : n 2 (n/4) 2 (n/2) 2 T(n/16) T(n/8) T(n/8) T(n/4) 2-25

Example of recursion tree Solve T(n) ) = T(n/4) + T(n/2) + n 2 : n 2 (n/4) 2 (n/2) 2 (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 (1) 2-26

Example of recursion tree Solve T(n) ) = T(n/4) + T(n/2) + n 2 : n 2 n (n/4) 2 (n/2) 2 (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 n 2 (1) 2-27

Example of recursion tree Solve T(n) ) = T(n/4) + T(n/2) + n 2 : n 2 n 2 (n/4) 2 (n/2) 2 5 n 2 16 (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 (1) 2-28

Example of recursion tree Solve T(n) ) = T(n/4) + T(n/2) + n 2 : n 2 n 2 (n/4) 2 5 n 2 (n/2) 2 16 (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 25 n 2 256 (1) 2-29

Example of recursion tree Solve T(n) ) = T(n/4) + T(n/2) + n 2 : n 2 n 2 (n/4) 2 5 n 2 (n/2) 2 16 (n/16) 2 (n/8) 2 (n/8) 2 (n/4) 2 25 n 256 (1) 2 2 Total = n 1 5 5 5 16 16 16 = (n 2 ) geometric series 3 2 2-30

The master method The master method applies to recurrences of the form T(n) ) = a T(n/b) /b)+f f (n), where a 1, b > 1, and f is asymptotically y positive. 2-31

Idea of master theorem h =logn 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) af(n/b) a 2 f (n/b 2 ) (1) #leaves = a h = alogbn = n log ba n log ba (1) 2-32

Three common cases Compare f (n) with ihn 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-33

Idea of master theorem h =logn 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) af(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 n log ba (1) fraction of the total weight. (n log ba ) 2-34

Three common cases Compare f (n) with ihn 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 il rates. Solution: T(n) ( ) = (n( log ba lg k+1 n) ). 2-35

Idea of master theorem h =logn 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) af(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) 2-36

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

Idea of master theorem h =logn 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) af(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)) 2-38

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 t is, k = 0. T(n) = (n 2 lg n). 2-39

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(cn/2) 3 cn 3 (reg. cond.) for c = 1/2. T(n) = (n 3 ). 2-40

Summarization How to analyze the time complexity of recurrent algorithms 1. Substitution 2. Recursion Tree 3. Master Theorem 2-41

Exercises Exercises 2.3 3, 5, 11 Exercises 2.4 5, 8, 9, 11 2-42