Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Counting the bits Analysis of Algorithms Will it run on a larger problem? When will it fail?
A simple example finding the maximum of a set S of n numbers.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
April 9, 2015Applied Discrete Mathematics Week 9: Relations 1 Solving Recurrence Relations Another Example: Give an explicit formula for the Fibonacci.
Recursion.
CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
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
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Recursion Chapter 5 Outline Induction Linear recursion –Example 1: Factorials –Example 2: Powers –Example 3: Reversing an array Binary recursion –Example.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
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.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Foundations of Algorithms, Fourth Edition
Recursive Algorithm (4.4) An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Modeling with Recurrence Relations
Chapter 4 Divide-and-Conquer
Intro to Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Algorithm Analysis (for Divide-and-Conquer problems)
CSCE 411 Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
Data Structures and Algorithms
CS201: Data Structures and Discrete Mathematics I
Solving Recurrence Relations
At the end of this session, learner will be able to:
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

Week 6 - Monday

 What did we talk about last time?  Exam 1!  Before that:  Recursion

Recursion

Infix to Postfix Converter

 Recursion is a great technique  One of its strengths is in writing concise code to solve a problem  Some recursive solutions are very efficient  Some are not  It pays to be aware of both

 Find the sum of the integers 1 through n  Example: n = 8  sum(8) =  sum(8) = 36

 Base case (n = 1):   Recursive case (n > 1): 

public static int sum( int n ) { if( n == 1 ) return 1; else return n + sum( n – 1 ); } Base Case Recursive Case

 Recursive summing takes linear time (summing n takes n function calls)  Is there another way to find this sum?  Closed form equation   Constant time!  Remember the story of young Gauss

 The sequence: …  Studied by Leonardo of Pisa to model the growth of rabbit populations

 Find the n th term of the Fibonacci sequence  Simple approach of summing two previous terms together  Example: n = 7 

 Base cases (n = 1 and n = 2):  Result = 1  Recursive case (n > 2):  Result = fibonacci(n – 1) + fibonacci(n – 2)

public static int fib( int n ) { if( n <= 2 ) return 1; else return fib(n – 1) + fib(n – 2); } Base Case Recursive Case

 Example: fib(6) fib(6) fib(4) fib(5) fib(4) fib(3) fib(2) fib(1) fib(2) fib(1) fib(3) fib(2) fib(1) Uh oh.

 For most cases, calling fib() makes calls two more calls to fib(), which each make two more calls to fib(), and so on…  Many values are redundantly computed  The final running time is O(2 n/2 )

 The recursion is fine from a mathematical perspective  We just need to avoid recomputing lower terms in the sequence  We can use the idea of carrying along both the (n – 1) term and the (n – 2) term in each recursive step

public static int fib2( int a, int b, int n ) { if( n <= 2 ) return b; else return fib2(b, a + b, n - 1); } //proxy method int fib( int n ) { return fib2(1, 1, n); } Base Case Recursive Case

 We want to raise a number x to a power n, like so: x n  We allow x to be real, but n must be an integer greater than or equal to 0  Example: (4.5) 13 =

 Base case (n = 0):  Result = 1  Recursive case (n > 0):  Result = x ∙ x (n – 1)

public static double power( double x, int n ) { if( n == 0 ) return 1; else return x * power( x, n – 1); } Base Case Recursive Case

 Each call reduces n by 1  n total calls  What's the running time?  O(n)

 We need to structure the recursion differently  Instead of reducing n by 1 each time, can we reduce it by a lot more?  It’s true that x n = x ∙ x (n – 1)  But, it is also true that x n = x (n/2) ∙ x (n/2)

 Assume that n is a power of 2  Base case (n = 1):  Result = x  Recursive case (n > 1):  Result = (x (n/2) ) 2

public static double power2( double x, int n ) { double temp; if( n == 1 ) return x; else { temp = power2( x, n/2 ); return temp * temp; } } Base Case Recursive Case

 Each call reduces n by half  log 2 (n) total calls  Just like binary search  Can we expand the algorithm to even and odd values of n?

 Base case (n = 1):  Result = x  Recursive cases (n > 1):  If n is even, result = (x (n/2) ) 2  If n is odd, result = x ∙ (x ((n – 1)/2) ) 2

public static double power3( double x, int n ) { double temp; if( n == 1 ) return x; else if( n % 2 == 0 ) { temp = power3( x, n/2 ); return temp * temp; } else { temp = power3( x, (n – 1)/2 ); return x * temp * temp; } } Base Case Recursive Cases

 Beautiful divide and conquer  Base case: List has size 1  Recursive case:  Divide your list in half  Recursively merge sort each half  Merge the two halves back together in sorted order

 Great. Now, how long does it take?

 Recursive running time  Master Theorem

 Keep working on Project 2  Keep working on Assignment 3