Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

Similar documents
Data Structures and Algorithms

Algorithms Chapter 4 Recurrences

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

CS473 - Algorithms I

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

Chapter 4. Recurrences

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

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

Principles of Algorithm Analysis

Data Structures and Algorithms CMPSC 465

CS F-01 Algorithm Analysis 1

Analysis of Algorithms - Using Asymptotic Bounds -

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

Divide and Conquer. Recurrence Relations

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

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

Recurrence Relations

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

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

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

Algorithms Design & Analysis. Analysis of Algorithm

Algorithm efficiency analysis

CS Non-recursive and Recursive Algorithm Analysis

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

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

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

CS Analysis of Recursive Algorithms and Brute Force

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

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

Module 1: Analyzing the Efficiency of Algorithms

Practical Session #3 - Recursions

COMP 382: Reasoning about algorithms

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

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

Methods for solving recurrences

Data Structures and Algorithms Chapter 3

Chapter 4 Divide-and-Conquer

Divide and Conquer. Andreas Klappenecker

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

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Module 1: Analyzing the Efficiency of Algorithms

Data Structures and Algorithms CSE 465

1 Substitution method

Asymptotic Analysis and Recurrences

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

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

Introduction to Algorithms

CS 161 Summer 2009 Homework #2 Sample Solutions

Analysis of Algorithms. Randomizing Quicksort

Computational Complexity and Intractability: An Introduction to the Theory of NP. Chapter 9

CPS 616 DIVIDE-AND-CONQUER 6-1

282 Math Preview. Chris Brown. September 8, Why This? 2. 2 Logarithms Basic Identities Basic Consequences...

CSCI 3110 Assignment 6 Solutions

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

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

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

Cpt S 223. School of EECS, WSU

Review Of Topics. Review: Induction

Asymptotic Algorithm Analysis & Sorting

Lecture 4. Quicksort

MIDTERM I CMPS Winter 2013 Warmuth

A SUMMARY OF RECURSION SOLVING TECHNIQUES

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

CS Data Structures and Algorithm Analysis

The Divide-and-Conquer Design Paradigm

Data Structures and Algorithms CMPSC 465

Algorithm Design and Analysis

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

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

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

Written Homework #1: Analysis of Algorithms

CSC236 Week 4. Larry Zhang

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

The Time Complexity of an Algorithm

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Growth of Functions (CLRS 2.3,3)

Fast Convolution; Strassen s Method

ECE250: Algorithms and Data Structures Analyzing and Designing Algorithms

Find an Element x in an Unsorted Array

CPSC 221 Basic Algorithms and Data Structures

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

Algorithm Design and Analysis

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Why do we need math in a data structures course?

Recurrences COMP 215

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

The Time Complexity of an Algorithm

The Growth of Functions (2A) Young Won Lim 4/6/18

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

Introduction to Algorithms 6.046J/18.401J/SMA5503

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

Reading 10 : Asymptotic Analysis

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

Compare the growth rate of functions

Ch01. Analysis of Algorithms

CSC Design and Analysis of Algorithms. Lecture 1

Data Structures and Algorithms CSE 465

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

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

Transcription:

Chapter 3 Recursive Algorithms

Writing recurrences +

Writing recurrences To determine the statement execution count is a two-step problem. Write down the recurrence from the recursive code for the algorithm. Solve the recurrence.

2 n 0, T (n 1)+3 n > 0.

2 n 0, T (n 1)+3 n > 0.

2 n 0, T (n 1)+3 n > 0.

2 n 0, T (n 1)+3 n > 0.

Θ(1) n 0, T (n 1)+Θ(1) n > 0.

Θ(1) n 1, T (n/2)+θ(1) n > 1.

Θ(1) n 1, T (n/2)+θ(1) n > 1.

Θ(1) n 1, T (n/2)+θ(1) n > 1.

Θ(1) n 1, T (n/2)+θ(1) n > 1.

Figure 3.7 1 (a) n disks on peg 1. 2 3 1 2 3 (b) Move n 1 disks from peg 1 to peg 2. 1 2 3 (c) Move one disk from peg 1 to peg 3. 1 2 3 (d) Move n 1 disks from peg 2 to peg 3.

Θ(1) n = 1, 2T (n 1)+Θ(1) n > 1.

Θ(1) n = 1, 2T (n 1)+Θ(1) n > 1.

Θ(1) n = 1, 2T (n 1)+Θ(1) n > 1.

Θ(1) n = 1, 2T (n 1)+Θ(1) n > 1.

Solving recurrences backward substitution, the recursion-tree method, guess and verify, and the master method.

Backward substitution T (n 1)+1 1 n 0, = T (n 2)+1 + 1 T (n 1)+1 n > 0. = T (n 3)+1 + 1 + 1 = T (n n)+ = T (0)+n = 1 + n n }} 1 + 1 + + 1 n + 1 Θ(n)

Design Patterns for Data Structures 1 n 1, T (n/2)+1 n > 1. T (n/2)+1 = T (n/2 1 )+1 = T (n/4)+1 + 1 = T (n/2 2 )+1 + 1 = T (n/8)+1 + 1 + 1 = T (n/2 3 )+1 + 1 + 1 = T (n/n)+1 + 1 + + 1 = T (n/2 k )+ = T (1)+k = 1 + k 1 + lgn Θ(lgn) k }} 1 + 1 + + 1

Design Patterns for Data Structures 1 n 1, T (n/2)+1 n > 1. T (n/2)+1 = T (n/2 1 )+1 = T (n/4)+1 + 1 = T (n/2 2 )+1 + 1 = T (n/8)+1 + 1 + 1 = T (n/2 3 )+1 + 1 + 1 = T (n/n)+1 + 1 + + 1 = T (n/2 k )+ = T (1)+k = 1 + k 1 + lgn Θ(lgn) k }} 1 + 1 + + 1

Design Patterns for Data Structures 1 n 1, T (n/2)+1 n > 1. T (n/2)+1 = T (n/2 1 )+1 = T (n/4)+1 + 1 = T (n/2 2 )+1 + 1 = T (n/8)+1 + 1 + 1 = T (n/2 3 )+1 + 1 + 1 = T (n/n)+1 + 1 + + 1 = T (n/2 k )+ = T (1)+k = 1 + k 1 + lgn Θ(lgn) k }} 1 + 1 + + 1

Recurrence tree method Example: 1 n = 1, 2T (n/2)+n n > 1.

Figure 3.8 height = k n T(n) n/2 n/2 T(n/2) T(n/2) 1(n) = n 2(n/2) = n n/4 T(n/4) n/4 n/4 n/4 T(n/4) T(n/4) T(n/4) 4(n/4) = n n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8)...... 1 1... 1 T(1) T(1)......... T(n/8) 8(n/8) = n... n(n/n) = n Total = nk

Recurrence tree method Example: 1 n = 1, 2T (n/2)+n n > 1. Conclusion: Θ(nlgn)

Guess and verify 1 n = 1, 2T (n 1)+1 n > 1. Prove: 2 n 1 2 n 1 = n = 1 T (1)=2 1 1 = T (1)=1 = T (n) 1 = 1

Design Patterns for Data Structures 1 n = 1, 2T (n 1)+1 n > 1. Prove: T (n + 1) =2 n+1 1 2 n 1 2 n 1 Prove assuming as ind. hyp. T (n + 1) = T (n) [n := n + 1] 2T (n + 1 1)+1 = 2T (n)+1 = 2(2 n 1)+1 = 2 n+1 1

1 n 1, T (n/2)+1 n > 1. Guess and verify Prove: lgn + 1 lgn + 1 = n = 1 T (1)=lg1 + 1 = T (1)=1 = T (n) 1 = 1

1 n 1, T (n/2)+1 n > 1. Prove: lgn + 1 Prove T (2n) =lg(2n)+1 assuming T (n) =lgn + 1 as ind. hyp. T (2n) = T (n) [n := 2n] T (2n/2)+1 = T (n)+1 = lgn + 1 + 1 = lgn + lg2 + 1 = lga + lgb = lg(ab) lg(2n)+1

The master method Master theorem: Let a be an integer a 1, b be a real number b > 1, andc be a real number c > 0. Given a recurrence of the form 1 if n = 1, at (n/b)+n c if n > 1. then, for n an exact power of b, if log b a < c, Θ(n c ), if log b a = c, Θ(n c logn), and if log b a > c, Θ(n log b a ).

The master method Master theorem: Let a be an integer a 1, b be a real number b > 1, andc be a real number c > 0. Given a recurrence of the form 1 if n = 1, at (n/b)+n c if n > 1. then, for n an exact power of b, if log b a < c, Θ(n c ), if log b a = c, Θ(n c logn), and if log b a > c, Θ(n log b a ). 1 n = 1, 2T (n/2)+n n > 1. a = 2 b = 2 c = 1 n Θ(nlgn)

Figure 3.9 Properties of asymptotic bounds 3 n 2 lgn f.n 1 0 1 1 2 0 1 2 3 4 n

Figure 3.9 Properties of asymptotic bounds 3 n f.n 2 1 0 lgn 1 If n 1, thenlgn 0. If n 2, thenlgn 1. For all positive values of n, n > lgn. 1 2 0 1 2 3 4 n

Figure 3.9 Properties of asymptotic bounds 3 n f.n 2 1 0 lgn 1 If n 1, thenlgn 0. If n 2, thenlgn 1. For all positive values of n, n > lgn. 1 2 0 1 2 3 4 n

Figure 3.9 Properties of asymptotic bounds 3 n f.n 2 1 0 lgn 1 If n 1, thenlgn 0. If n 2, thenlgn 1. For all positive values of n, n > lgn. 1 2 0 1 2 3 4 n

f.n = 3n 2 + 2nlgn = O(n 2 ) f n 3n 2nlgn O n c n 0 f.n = 3n 2 + 2nlgn cn 2 n n 0 3n 2 + 2nlgn lgn 3n 2 + 2n n = 5n 2 = c = 5 cn 2 c = 5 n 0 n 0

Figure 3.10 40 n 3 n 2 lgn n 2 nlgn 30 2 n f.n 20 n! 10 0 0 5 10 n n lgn 1

Figure 3.10 4000 2 n f.n 3000 2000 n! n 3 1000 0 0 5 10 n n 2 lgn n 2

Figure 3.11 3nlgn + 5n 2n 2 + 7n O(n) O(n lg n) 17nlgn 2n + 5 6lgn 1 2 n! O(n) O(nlgn)

Figure 3.11 830 3nlgn + 5n Ω(nlgn) Ω(n) 1 2 n! 2n + 5 9n 18 6lgn Ω(n) Ω(nlgn)

O(1) O(lgn) O(n) O(nlgn) O(n 2 ) O(n 3 ) O(2 n ) O(n!) Ω(1) Ω(lgn) Ω(n) Ω(nlgn) Ω(n 2 ) Ω(n 3 ) Ω(2 n ) Ω(n!)