Big-O & Recursion.

Slides:



Advertisements
Similar presentations
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Advertisements

Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Past Midterm Review COMP171 Fall Midterm Review / Slide 2 Multiple Choices (1) (2)
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
COMP2230 Tutorial 2. Mathematical Induction A tool for proving the truth of a statement for some integer “n”. “n” is usually a +ve integer from 1 to infinity.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Lecture 8 Analysis of Recursive Algorithms King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CSC 3323 Notes – Recurrence Relations Algorithm Analysis.
Analysis of Recursive Algorithms
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Recurrence Examples.
Analysis of Algorithms
8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method.
Analysis of Recursive Algorithms October 29, 2014
Mathematics Review and Asymptotic Notation
Analysis of Algorithms
Analysis of Algorithms CS 477/677
Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
CSC 413/513: Intro to Algorithms Merge Sort Solving Recurrences.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
Introduction to Algorithms Chapter 4: Recurrences.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
Foundations II: Data Structures and Algorithms
Solving Recurrences with the Substitution Method.
Divide and Conquer. Recall Divide the problem into a number of sub-problems that are smaller instances of the same problem. Conquer the sub-problems by.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
Lecture #3 Analysis of Recursive Algorithms
CompSci 100e 7.1 Plan for the week l Recurrences  How do we analyze the run-time of recursive algorithms? l Setting up the Boggle assignment.
Algorithm Analysis 1.
Unit 6 Analysis of Recursive Algorithms
Analysis of Algorithms CS 477/677
Asymptotic Notations Dr. Munesh Singh.
Introduction to Algorithms: Recurrences
Fundamental Data Structures and Algorithms
Analysis: Algorithms and Data Structures
Analysis of Algorithms
Recursion notes Chapter 8.
Amortised Analysis.
Lecture 11. Master Theorem for analyzing Recursive relations
Compsci 201 Recursion, Recurrences, & Trees
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Analysis of Algorithms
CS 3343: Analysis of Algorithms
A Brief Summary for Exam 2
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Introduction to Algorithms 6.046J
תרגול 3 - רקורסיה.
Richard Anderson Lecture 11 Recurrences
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Lecture 29 CSE 331 Nov 8, 2017.
Recurrences (Method 4) Alexandra Stefan.
Recursion.
Divide-and-Conquer 7 2  9 4   2   4   7
Searching.
Trevor Brown CS 341: Algorithms Trevor Brown
At the end of this session, learner will be able to:
Recurrences.
Richard Anderson Lecture 12, Winter 2019 Recurrences
Algorithms CSCI 235, Spring 2019 Lecture 8 Recurrences III
Richard Anderson Lecture 12 Recurrences
Presentation transcript:

Big-O & Recursion

Recursion What is the Big-O of each?

Recursion What is the Big-O of each? Need Number of recursive calls Work done at each recursive call

Recursion What calls get made?

Recursion What calls get made? 16, 8, 4, 2, 1 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0

Recursion Number of recursive calls 16, 8, 4, 2, 1 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 O(logn) O(n)

Recursion BigO Assume print is O(1) All work is O(1) = logn calls * O(1) work per call = O(logn)

Recursion BigO All work is O(1) = n calls * O(1) work per call = O(n)

Recursion BigO Work is is O(1) + O(n) = O(n) O(n) recursive calls = O(n) calls * O(n) work per call = O(n2) foo(array, size) if(size == 0) return for(int i = 0; i < size; i++) cout << array[i] foo(array, size - 1) Recursive calls for size = 10: 10, 9, 8, 7, … , 0 O(n)

Proofs

Recurrence Relationships Recurrence Relationship: Recursively defined math function: Used to: Formally establish recursive BigO Tackle complex recursion (multiple calls)

Recursion What is the Big-O? T(n) = T(n - 1) + 1 T(0) = 1

Recursion What is the Big-O? T(n) = T(n - 1) + 1 Level 1 = … = T(n - k) + k Level 1 Level 2 Level 3 Level k

Recursion What is the Big-O? Know: T(n) = T(n - k) + k T(0) = 1 So solve n – k = 0 … k = n

Recursion What is the Big-O? Given: T(n) = T(n - k) + k k = n Plug in k: 𝑇 𝑛 =𝑇 𝑛 −𝑛 + 𝑛 𝑇 𝑛 =𝑇 0 + 𝑛 𝑇 𝑛 =1 + 𝑛 𝑇 𝑛 ≈ 𝑛

Recursion What is the Big-O? T(n) = T(n/2) + 1 T(1) = 1

Recursion What is the Big-O? T(n) = T(n/2) + 1 Level 1 = … = T(n/2k) + k Level 1 Level 2 Level 3 Level k

Recursion What is the Big-O? T(n) = T(n/2k) + k Level k So what is k? How many levels are there? Level k

Recursion What is the Big-O? T(n) = T(n/2k) + k Know T(1) = 1 Need to solve n/2k = 1

Recursion What is the Big-O? T(n) = T(n/2k) + k Know T(1) = 1 Need to solve n/2k = 1 n = 2k log2(n) = k

Recursion What is the Big-O? Given: T(n) = T(n/2k) + k k = log2(n) Plug in k: 𝑇 𝑛 =𝑇 𝑛 2 𝑙𝑜𝑔 2 𝑛 + 𝑙𝑜𝑔 2 𝑛 𝑇 𝑛 =𝑇 𝑛 𝑛 + 𝑙𝑜𝑔 2 𝑛 𝑇 𝑛 =𝑇 1 + 𝑙𝑜𝑔 2 𝑛 𝑇 𝑛 =1+ 𝑙𝑜𝑔 2 𝑛 ≈ 𝑙𝑜𝑔 2 𝑛

Common Recurrences Big O's to recognize: Our focus is intuitive Math in 231/232 & Analysis of Algorithms

Derivations if you want them… T(n) = 2T(n/2) + cn + c T(n) = 2T(n/2) + n + 1 = 2(2T(n/4) + (n/2) + 1) + n + 1 = 4T(n/4) + 2n + 2 = 4T(n/4) + 2n + 2 = 4(2T(n/8) + (n/4) + 1) + 2n + 2 = 8T(n/8) + 3n + 3 = … = 2kT(n/2k) + kn + k Level 1 Level 2 Level 3 Level k

Derivations if you want them… T(n) = 2T(n/2) + cn + c Level k T(n) = 2k T(n/2k) + kn + k T(1) = 1, solve 1 = n/2k k = log2n Substitute: 𝑇 𝑛 = 2 𝑙𝑜𝑔 2 𝑛 𝑇 𝑛 2 𝑙𝑜𝑔 2 𝑛 + 𝑙𝑜𝑔 2 𝑛 𝑛+ 𝑙𝑜𝑔 2 𝑛 =𝑛𝑇 𝑛 𝑛 + 𝑛 𝑙𝑜𝑔 2 𝑛+ 𝑙𝑜𝑔 2 𝑛 =𝑛𝑇 1 + 𝑛 𝑙𝑜𝑔 2 𝑛+ 𝑙𝑜𝑔 2 𝑛 =𝑛+ 𝑛 𝑙𝑜𝑔 2 𝑛+ 𝑙𝑜𝑔 2 𝑛 ≈𝑛 𝑙𝑜𝑔 2 𝑛

Derivations if you want them… T(n) = 2T(n - 1) + c T(n) = 2T(n - 1) + 1 = 2(2T(n - 2) + 1) + 1 = 4T(n - 2) + 3 = 4T(n - 2) + 3 = 4(2T(n - 3) + 1) + 3 = 8T(n - 3) + 7 = … = 2kT(n - k) + (2k – 1) Level 1 Level 2 Level 3 Level k

Derivations if you want them… T(n) = 2T(n - 1) + c Level k T(n) = 2kT(n - k) + (2k – 1) T(1) = 1, solve 1 = n - k k = n - 1 Substitute: 𝑇 𝑛 = 2 𝑛−1 𝑇 𝑛 −(𝑛 −1) +( 2 𝑛−1 −1) = 2 𝑛−1 𝑇 1 + 2 𝑛−1 −1 = 2 𝑛−1 + 2 𝑛−1 −1 =2 2 𝑛−1 −1 ≈ 2 𝑛−1 ≈ 2 𝑛