Recursion.

Slides:



Advertisements
Similar presentations
A simple example finding the maximum of a set S of n numbers.
Advertisements

5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Recurrences. What is a Recurrence Relation? A system of equations giving the value of a function from numbers to numbers in terms of the value of the.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Updates HW#1 has been delayed until next MONDAY. There were two errors in the assignment Merge sort runs in Θ(n log n). Insertion sort runs in Θ(n2).
Recursion Credits: Jeff Edmonds, Ping Xuan. MULT(X,Y): If |X| = |Y| = 1 then RETURN XY Break X into a;b and Y into c;d e = MULT(a,c) and f =MULT(b,d)
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Analysis of Recursive Algorithms October 29, 2014
Project 2 due … Project 2 due … Project 2 Project 2.
ECOE 456/556: Algorithms and Computational Complexity Lecture 1 Serdar Taşıran.
CSC 413/513: Intro to Algorithms Merge Sort Solving Recurrences.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
CompSci 102 Discrete Math for Computer Science April 17, 2012 Prof. Rodger.
Introduction to Algorithms Chapter 4: Recurrences.
Chapter 4. Recurrences. Outline Offers three methods for solving recurrences, that is for obtaining asymptotic bounds on the solution In the substitution.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Foundations II: Data Structures and Algorithms
Solving Recurrences with the Substitution Method.
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
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.
1Computer Sciences. 2 GROWTH OF FUNCTIONS 3.2 STANDARD NOTATIONS AND COMMON FUNCTIONS.
1Computer Sciences Department. Objectives Recurrences.  Substitution Method,  Recursion-tree method,  Master method.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Recursion COMP T1 #6.
ECOE 456/556: Algorithms and Computational Complexity
Recursion Ali.
Fundamental Data Structures and Algorithms
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
Unit 1. Sorting and Divide and Conquer
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
Chapter 4: Divide and Conquer
COSC160: Data Structures Linked Lists
Analysis of Algorithms
CS 3343: Analysis of Algorithms
Algorithms and Data Structures Lecture III
Divide-and-Conquer 7 2  9 4   2   4   7
Richard Anderson Lecture 11 Recurrences
Richard Anderson Lecture 11 Minimum Spanning Trees
Ch 4: Recurrences Ming-Te Chi
CS 3343: Analysis of Algorithms
Divide and Conquer (Merge Sort)
CS 3343: Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
CS 332: Algorithms Quicksort David Luebke /9/2019.
Trevor Brown CS 341: Algorithms Trevor Brown
Divide & Conquer Algorithms
Lecture 30 CSE 331 Nov 12, 2012.
Introduction To Algorithms
Algorithms Recurrences.
Solving recurrence equations
Recurrences.
Design and Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Richard Anderson Lecture 12, Winter 2019 Recurrences
Richard Anderson Lecture 12 Recurrences
Presentation transcript:

Recursion

Outline -Homework due tonight -Remember: Ask lots of questions on Piazza, ask others for help, Google whatever you need to -Only requirement: write your solutions by yourself (without extensive notes) -Today: Recursion refresher -Recurrence relations! -Next homework coming soon!

The Towers of Hanoi

What if we knew how to solve part of the problem? Assume we can move k (in this case, 4) different rings

Can we do one better?

Solved for one more!

Recursion is Mathematical Induction In both, we have general and boundary conditions: The general conditions break the problem into smaller and smaller pieces. The initial or boundary condition terminate the recursion. Both take a Divide and Conquer approach to solving mathematical problems.

Recurrence Relations Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations: an = an-1 + 1; a1 = 1 => an = an-1+ 2n - 1; a1=1 => an = 2an-1; a1 = 1 => an = n an-1; a1 = 1 => an = n (linear) an = n2 (polynomial) an = 2n (exponential) an = n! (others…)

Solving Recurrences We can use mathematical induction to prove that a general function solves for a recursive one. Tn = 2Tn-1 + 1 ; T0 = 0 n = 0 1 2 3 4 5 6 7 8 Tn = Guess what the solution is?

Solving Recurrences Prove: Tn = 2n - 1 by induction: 1. Show the base case is true: T0 = 20 - 1 = 0 2. Now assume true for Tn-1 3. Substitute in Tn-1 in recurrence for Tn Tn = 2Tn-1 + 1 = 2 ( 2n-1 - 1 ) + 1 = 2n -1

Solving Recurrences No general procedure for solving recurrence relations is known, which is why it is an art. Realize that linear, finite history, constant coefficient recurrences always can be solved For: an = 2an-1 + 2an-2 +1 ; a1 = 1 ; a2 = 1 degree = 1 history = 2 coefficients = 2, 2, and 1 In the end, what is the best way to solve this? -- Use software like WolframAlpha (or Mathematica or Maple)

Why do we care? Recursive functions can be powerful tools for applying divide and conquer techniques to algorithmic problems. Recursive algorithms can easily be converted into recurrence relations for analysis. Converting a recursive algorithm to the equivalent sequential algorithm can save stack space and generally improve its speed.

Techniques Guess a solution by looking at a few small examples and prove by induction. Try back-substituting until you know what is going on. Draw a recursion tree. There are a number of fixed techniques that many classes will spend weeks on to teach you how to solve recurrence relations. In practice, there are some less mechanistic techniques that will allow you to often come up with a solution with far less pain. I’m not going to spend the weeks to teach you these techniques -- that’s not really part of this class. But I will give you an overview of how to attack this sort of problem from an intuitive perspective.

Back-substitution Example: T(n) = 3T(⌊n/4⌋) + n, T(0) = 0; T(1) = 1

Back-substitution Example: T(n) = 3T(⌊n/4⌋) + n, T(0) = 0; T(1) = 1 = 3(3T(⌊n/16⌋)+n/4) + n = 9T(⌊n/16⌋) + 3n/4 + n = 9(3T(⌊n/64⌋) +n/16) + 3n/4 + n = 27T(⌊n/64⌋)+9n/16 + 3n/4 + n =

Recursion Trees T(n) = 2 T(n/2) + n2 , T(1) = 1

Recursion Trees T(n) = 2 T(n/2) + n2 , T(1) = 1

Use induction to prove that MergeSort is an O(n log n) algorithm. Mergesort(array) n = size(array) if ( n <= 1) return array array1 = Mergesort(array[1 .. n/2]) array2 = Mergesort(array[n/2 + 1 .. n]) return Merge(array1, array2) Mergesort = T(n). How long does it take to run? Call itself twice. (T(n/2) each) Call Merge once (n each)

Induction Proof Example: Prove that T(n) = 2T(⌊ n/2⌋ ) + n , T(1) = 1 is O(n log n). Base case: n=2, c>4. We need to prove that T(n) < c n log n , for all n greater than some value. Inductive step: Assume T(n/2) ≤ c (n/2) log (n/2) Show that T(n) ≤ c n log n .

Given : T(n/2) ≤ c (n/2) log (n/2) T(n) = 2T(⌊n/2⌋ ) + n ≤ 2( c(⌊n/2⌋) log (⌊n/2⌋) ) + n ≤ 2( c(n/2) log (n/2) ) + n (dropping floors makes it bigger!) = c n log(n/2) + n = c n ( log(n) - log(2) ) + n = c n log(n) - c n + n (log22 = 1) = c n log(n) - (c - 1) n < c n log(n) (c > 1)

Example Problem 2 T(n) = T(n/3) + T(2n/3) + n Show T(n) is Ω(n log n) by appealing to the recursion tree.

Recursion Tree

Example Problem (n + a)b = ϴ(nb) Show that for any real constants a and b, where b > 0 (n + a)b = ϴ(nb) What are the two things we must show to do this? To prove O(nb), what value can should we make c1? … what is n0? To prove Ω(nb), what value can should we make c2? c1 = 2^b n0 = |a| c2 = (1/2)^b n0 = 3|a|/2