CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

A simple example finding the maximum of a set S of n numbers.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
CS4413 Divide-and-Conquer
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Recursion & Merge Sort Introduction to Algorithms Recursion & Merge Sort CSE 680 Prof. Roger Crawfis.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Recursion.
CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
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
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
CS 253: Algorithms Chapter 4 Divide-and-Conquer Recurrences Master Theorem Credit: Dr. George Bebis.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Project 2 due … Project 2 due … Project 2 Project 2.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
Getting Started Introduction to Algorithms Jeff Chastine.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Data Structure Introduction.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
1 CSE 326: Data Structures A Sort of Detour Henry Kautz Winter Quarter 2002.
Foundations II: Data Structures and Algorithms
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
1 Algorithms CSCI 235, Fall 2015 Lecture 7 Recurrences II.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
CSE 326: Data Structures Lecture #3 Asymptotic Analysis Steve Wolfman Winter Quarter 2000.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
CSE 326: Data Structures Class #4 Analysis of Algorithms III Analysis of Recursive Algorithms Henry Kautz Winter 2002.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Lecture #3 Analysis of Recursive Algorithms
Introduction to Algorithms: Divide-n-Conquer Algorithms
Design and Analysis of Algorithms
UNIT- I Problem solving and Algorithmic Analysis
Algorithm Analysis The case of recursion.
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
CS 3343: Analysis of Algorithms
CSE 2010: Algorithms and Data Structures
Divide & Conquer Algorithms
Analysis of Algorithms
David Kauchak cs161 Summer 2009
Algorithms Recurrences.
Recurrences.
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001

Analyzing RecursionCSE 326 Autumn Recursion Rules Rules for using recursion effectively (Weiss 1.3): 1. Base cases Must have some non-recursive base case(s). 2. Making progress Recursive call must progress toward a base case. 3. Design rule Assume the recursive calls work. 4. Compund interest rule Don’t duplicate work in separate recursive calls.

Analyzing RecursionCSE 326 Autumn Recurrence Equations  A recursive procedure can often be analyzed by solving a recurrence equation of the form: T(n) = base case: some constant recursive case: T(subproblems) + T(combine)  Result depends upon  how many subproblems  how much smaller are subproblems  how costly to combine solutions (coefficients)

Analyzing RecursionCSE 326 Autumn Example: Sum of Queue SumQueue(Q) if (Q.length == 0 ) return 0 else return Q.dequeue() + SumQueue(Q) One subproblem Linear reduction in size (decrease by 1) Combining: constant (cost of 1 add) T(0)  b T(n)  c + T(n – 1) for n>0

Analyzing RecursionCSE 326 Autumn Sum of Queue Solution T(n)  c + c + T(n-2)  c + c + c + T(n-3)  kc + T(n-k) for all k  nc + T(0) for k=n  cn + b = O(n) T(0)  b T(n)  c + T(n – 1) for n>0 Equation: Solution:

Analyzing RecursionCSE 326 Autumn Example: Binary Search BinarySearch(A, x) if (A.size == 1) return (x == A[0]) mid = A.size / 2 if (x == A[mid]) return true else if (x < A[mid]) return BinarySearch( A_LowerHalf, x) else if (x > A[mid]) return BinarySearch( A_UpperHalf, x) T(1)  b T(n)  T(n/2) + c for n>1 Equation: Search a sorted array for a given item, x  If x == middle array element, return true  Else, BinarySearch lower (x mid) sub-array  1 subproblem, half as large

Analyzing RecursionCSE 326 Autumn Binary Search: Solution T(n)  T(n/2) + c  T(n/4) + c + c  T(n/8) + c + c + c  T(n/2 k ) + kc  T(1) + c log n where k = log n  b + c log n = O(log n) T(1)  b T(n)  T(n/2) + c for n>1 Equation: Solution:

Analyzing RecursionCSE 326 Autumn Recursion Tree for Binary Search n n/2 n/4 … 1 O(1) … log n Θ(log n) O(1) Problem sizeCost per stage

Analyzing RecursionCSE 326 Autumn Example: MergeSort Split array in half, sort each half, merge sub-arrays  2 subproblems, each half as large  linear amount of work to combine T(n)  2T(n/2)+cn  2(2(T(n/4)+cn/2)+cn = 4T(n/4) +cn +cn  4(2(T(n/8)+c(n/4))+cn+cn = 8T(n/8)+cn+cn+cn  2kT(n/2k)+kcn  2kT(1) + cn log n where k = log n = O(n log n)

Analyzing RecursionCSE 326 Autumn Recursion Tree for Merge Sort n n/2 n/4 ……… 111 O(n) … log n Θ(n log n) Problem sizeCost per stage

Analyzing RecursionCSE 326 Autumn Example: Recursive Fibonacci Fibonacci numbers seems like a great use of recursion  Simple, elegant-looking recursive code … Fibonacci(i) if (i == 0 or i == 1) return 1 else return Fibonacci(i - 1) + Fibonacci(i - 2)

Analyzing RecursionCSE 326 Autumn Fibonacci Call Tree Fib(5) Fib(4) Fib(3) Fib(2) Fib(3) Fib(1) Fib(0) Fib(1) Fib(2) But what actually happens is combinatorial explosion

Analyzing RecursionCSE 326 Autumn Analyzing Recursive Fibonacci  Running time: Lower bound analysis T(0), T(1)  1 T(n)  T(n - 1) + T(n - 2) + c if n > 1  Note: T(n)  Fib(n)  Weiss shows in that Fib(n) < (5/3) n  Similar logic shows that Fib(n)  (3/2) n for N>4  In fact: Fib(n) =  ((3/2) n ), Fib(n) = O((5/3) n ), Fib(n) = θ(  n ) where  = (1 +  5)/2

Analyzing RecursionCSE 326 Autumn Non-Recursive Fibonacci Avoid recursive calls  Keep a table of calculated values  each time you calculate an answer, store it in the table  Before performing any calculation for a value n  check if a valid answer for n is in the table  if so, return it Memoization  a form of dynamic programming How much time does memoized Fibonacci take?