Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.

Slides:



Advertisements
Similar presentations
CSC 205 Programming II Lecture 10 Towers of Hanoi.
Advertisements

Factorial Recursion stack Binary Search Towers of Hanoi
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
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.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
M180: Data Structures & Algorithms in Java
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion To understand recursion, you first have to understand recursion.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion Powerful Tool
Review of Recursion What is a Recursive Method?
CS212: Data Structures and Algorithms
Sections 4.1 & 4.2 Recursive Definitions,
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Abdulmotaleb El Saddik University of Ottawa
Recursion: The Mirrors
Recursion Chapter 12.
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 10.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 11.
Programming application CC213
Fundamentals of Programming
Recursion Data Structures.
Recursion Chapter 18.
Review of Recursion What is a Recursive Method?
The Hanoi Tower Problem
Recursion.
CSC 143 Recursion.
Review of Recursion What is a Recursive Method?
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion.
Review of Recursion What is a Recursive Method?
Presentation transcript:

Lecture 11 Recursion

A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative to iteration A recursive solution is generally less efficient in terms of system overhead, due to the overhead of extra function calls; however recursive functions – allow us to think of solutions to problems that may be recursive in nature – allow us to work with data structures that are best accessed recursively A recursive problem Imagine a computer environment that does not support the multiplication operator (*), but it does support addition (+)

Multiplication using the + operator public int multiply (int m, int n) // IN: m and n, values to be multiplied // PRE: m and n are defined and n > 0 // POST: returns m * n // RETURNS: Product of m * n if n is positive; otherwise, returns m { if (n <= 1) return m; else return m + multiply (m, n - 1); }

Tracing the code Trace: In order to display multiply(6(m), 4(n)), we need to know and therefore call – 6 + multiply (6, 3) – in order to calculate this result we need to know and therefore call 6 + multiply (6, 2) in order to calculate this result we need to know – 6 + multiply (6, 1) we know the result of multiply (6, 1) because n = 1, 6 is returned to the calling function – (returned) = 12 and is returned to the calling function (returned) = 18 and is returned to the calling function – (returned) = 24 and is returned to the calling function in main() 24 is displayed notice the multiply() function was called 5 times

Algorithm of Recursive Functions The recursive functions we will work with will generally consist of an if statement with the form shown below if the stopping case is reached// if (n <= 1) solve the problem// return m else reduce the problem using recursion // return m + multiply(m, n - 1 ) Problems that lend themselves to recursive solution have the following characteristics – one or more simple cases of the problem (stopping cases) have a straightforward, non-recursive solution – For the other cases, there is a process (using recursion) for substituting one or more reduced cases of the problem closer to a stopping case – Eventually the problem can be reduced to stopping cases only

Factorial = 6! A factorial is a number n such that n = n * (n-1) * (n - 2) * (n - 3) … (n - (n - 2)) * ( n - (n -1)) * 1 or n = n * factorial(n - 1) 6 factorial 6! = 6 * 5 * 4 * 3 * 2 * 1 or recursively 6! = 6 * 5! -> 5 * 4! -> 4 * 3! -> 3 * 2! -> 2 * 1 1! = 1 by definition (the stopping case)

int Factorial(int n) // IN: n, value to find the factorial of // PRE: n is defined and n > 0 // POST: returns n! // RETURNS: Factorial of n if n is positive; otherwise, returns 1 { if (n == 0) return 1; else return n * Factorial (n - 1); }

Head Recursion If the recursive call happens at the beginning of the code, then we have a case of head recursion Eg: public void foo(int n){ if (n>0) foo(n-1); System.out.println(n); }

Tail Recursion If the recursive call happens at the end of the code, then we have a case of tail recursion Eg: public void foo(int n){ if (n>0) System.out.println(n); foo(n-1); } Equivalent to running a loop Exercise: convert the code to an iterative loop

Tower of Hanoi Problem Suppose we have 3 pegs named A, B and C We have 3 disks named (1-largest, 2-next, 3- smallest) in peg A. How do we move 3 pegs from A to C using B? – We can only move one disk at a time – We cannot place a larger disk on the top of a smaller one Now how do we generalize this algorithm? A BC

A Recursive Algorithm If you have one disk, then the problem is solved – Move disk from source to destination If you have 2 disks how would you solve the problem? If you have n disks (n > 1), then divide and conquer – Move (n-1) disks from source to intermediate – Move one disk from source to destination – Move (n-1) disks from intermediate to destination This gives us a very elegant solution

Tower of Hanoi Code // TOWER OF HANOI ALGORITHM public void hanoi(int n, char source, char dest, char inter) { if (n==1) System.out.println("move disk ” + n + " from “ +source+" to “+dest) ; else { hanoi(n-1,source,inter,dest); System.out.println (“move disk ”+ n + " from “+source+" to “+dest) ; hanoi(n-1,inter,dest,source); } A=source, B=destination, C=intermediate A BC

Understanding Hanoi Recursion (n=3) left call - source to inter using dest – right call - inter to dest using source Hanoi(3,A,B,C) Hanoi(2,A,C,B)Hanoi(2,C,B,A) Hanoi(1,A,B,C)Hanoi(1,B,C,A)Hanoi(1,C,A,B)Hanoi(1,A,B,C) A to B A to CC to B A to B B to CC to AA to B