RECURSION. Objectives: become familiar with the idea of recursion Examine examples to show the wide variety of situations to which recursion can be applied.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

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.
Introduction to Recursion and Recursive Algorithms
Recursion Chapter 11. Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar with the binary.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion.
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
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.
Recursion. Binary search example postponed to end of lecture.
Recursion Chapter 11 Chapter 11.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
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 Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
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.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion.
Using recursion for Searching and Sorting
Recursion Topic 5.
Chapter 15 Recursion.
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Data Structures.
Basics of Recursion Programming with Recursion
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion.
ITEC324 Principle of CS III
Presentation transcript:

RECURSION

Objectives: become familiar with the idea of recursion Examine examples to show the wide variety of situations to which recursion can be applied. – triangular numbers – factorials, – recursive binary search, – the Towers of Hanoi puzzle. strengths and weaknesses of recursion

RECURSION – A method of programming whereby a function directly or indirectly calls itself – Problems: stop recursion?

Designing Algorithms There is no single recipe for inventing algorithms There are basic rules: – Understand your problem well – may require much mathematical analysis! – Use existing algorithms (reduction) or algorithmic ideas There is a single basic algorithmic technique: Divide and Conquer In its simplest (and most useful) form it is simple induction – In order to solve a problem, solve a similar problem of smaller size The key conceptual idea: – Think only about how to use the smaller solution to get the larger one – Do not worry about how to solve the smaller problem (it will be solved using an even smaller one)

Recursion A recursive method is a method that contains a call to itself Technically: – All modern computing languages allow writing methods that call themselves Conceptually: – This allows programming in a style that reflects divide-n- conquer algorithmic thinking – At the beginning recursive programs are confusing – after a while they become clearer than non-recursive variants

Recursion Example:Triangular Numbers 1, 3, 6, 10, 15, 21, … What is the next member of this series? The nth term in the series is obtained by adding n to the previous term. The numbers in this series are called triangular numbers because they can be visualized as a triangular arrangement of objects

Recursion int triangle(int n) { if(n==1) return 1; else return( n + triangle(n-1) ); } The condition that leads to a recursive method returning without making another recursive call is referred to as the base case. It’s critical that every recursive method have a base case to prevent infinite recursion and the consequent demise of the program.

Recursive Methods Must Eventually Terminate A recursive method must have at least one base, or stopping, case. A base case does not execute a recursive call –stops the recursion Each successive call to itself must be a "smaller version of itself” –an argument that describes a smaller problem –a base case is eventually reached

Key Components of a Recursive Algorithm Design 1.What is a smaller identical problem(s)? lDecomposition 2.How are the answers to smaller problems combined to form the answer to the larger problem? lComposition 3.Which is the smallest problem that can be solved easily (without further decomposition)? lBase/stopping case

RECURSION

Factorial (N!) N! = (N-1)! * N [for N > 1] 1! = 1 3! = 2! * 3 = (1! * 2) * 3 = 1 * 2 * 3 Recursive design: –Decomposition: (N-1)! –Composition: * N –Base case: 1!

factorial Method int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; // composition else // base case fact = 1; return fact; }

int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; }

int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; }

int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; } int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return fact; }

int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; } int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return 1; }

int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return fact; } int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return 1; }

int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return 2; }

int factorial(int 3) { int fact; if (n > 1) fact = 2 * 3; else fact = 1; return fact; } int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return 2; }

int factorial(int 3) { int fact; if (n > 1) fact = 2 * 3; else fact = 1; return 6; }

Execution Trace (decomposition) public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } factorial(4) factorial(3) 4

Execution Trace (decomposition) public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } factorial(4) factorial(3) 4 factorial(2) 3

Execution Trace (decomposition) public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } factorial(4) factorial(3) 4 factorial(2) 3 factorial(1) 2

Execution Trace (composition) public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } factorial(4) factorial(3) 4 factorial(2) 3 factorial(1)->1 2 * * *

Execution Trace (composition) public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } factorial(4) factorial(3) 4 factorial(2)->2 3 * *

Execution Trace (composition) public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } factorial(4) factorial(3)->6 4 *

Execution Trace (composition) public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } factorial(4)->24

This shows the pattern of function calls used to evaluate Factorial(5) recursively To evaluate Factorial(5) evaluate 5 * Factorial(4) To evaluate Factorial(4) evaluate 4 * Factorial(3) To evaluate Factorial(3) evaluate 3 * Factorial(2) Factorial(2) is 2 Return 2 Evaluate 3 * 2 Return 6 Evaluate 4 * 6 Return 24 Evaluate 5 * 24 Return 120

Remember: Key to Successful Recursion if-else statement (or some other branching statement) Some branches: recursive call –"smaller" arguments or solve "smaller" versions of the same task (decomposition) –Combine the results (composition) [if necessary] Other branches: no recursive calls –stopping cases or base cases

Warning: Infinite Recursion May Cause a Stack Overflow Error Infinite Recursion –Problem not getting smaller (no/bad decomposition) –Base case exists, but not reachable (bad base case and/or decomposition) –No base case Stack: keeps track of recursive calls by JVM (OS) –Method begins: add data onto the stack –Method ends: remove data from the stack Recursion never stops; stack eventually runs out of space –Stack overflow error

look up a name in the phone book? One Possible Way -Binary Search Search: middle page = (first page + last page)/2 Go to middle page; If (name is on middle page) done; //this is the base case else if (name is alphabetically before middle page) last page = middle page //redefine search area to front half Search //same process on reduced number of pages else //name must be after middle page first page = middle page //redefine search area to back half Search //same process on reduced number of pages

Binary Search Algorithm Searching a list for a particular value –sequential and binary are two common algorithms Binary search: –more efficient than sequential –but the list must be sorted first! Why it is called binary - each unsuccessful test for the target value reduces the remaining search list by 1/2.

BINARY SEARCH: Recursion

Where is the composition? If no items – not found (-1) Else if target is in the middle – middle location Else – location found by search(first half) or search(second half)

Binary vs. Sequential Search Binary Search –log 2 N + 1 comparisons (worst case) Sequential/Linear Search –N comparisons (worst case) Binary Search is faster but –array is assumed to be sorted beforehand Faster searching algorithms for “non-sorted arrays” –More sophisticated data structures than arrays –Later

Divide-and-Conquer Algorithms The recursive binary search is an example of the divide-and-conquer approach. You divide the big problem into two smaller problems and solve each one separately. The solution to each smaller problem is the same: You divide it into two even smaller problems and solve them. The process continues until you get to the base case, which can be solved easily, with no further division into halves. The divide-and-conquer approach is commonly used with recursion, although, you can also use a non-recursive approach. A divide-and-conquer approach usually involves a method that contains two recursive calls to itself, one for each half of the problem. In the binary search, there are two such calls, but only one of them is actually executed. (Which one depends on the value of the key.) The mergesort, which we’ll see later actually executes both recursive calls (to sort two halves of an array).

RECURSION: Hanoi tower

The Towers of Hanoi is an ancient puzzle consisting of a number of disks placed on three columns, as shown above. The disks all have different diameters and holes in the middle so they will fit over the columns. All the disks start out on column A. The object of the puzzle is to transfer all the disks from column A to column C. Only one disk can be moved at a time, and no disk can be placed on a disk that’s smaller than itself. The Towers of Hanoi puzzle can be solved recursively by moving all but the bottom disk of a subtree to an intermediate tower, moving the bottom disk to the destination tower, and finally moving the subtree to the destination.

There’s an ancient myth that somewhere in India, in a remote temple, monks labor day and night to transfer 64 golden disks from one of three diamond-studded towers to another. When they are finished, the world will end. Any alarm you may feel, however, will be dispelled when you see how long it takes to solve the puzzle for far fewer than 64 disks.

The Value of Recursion Recursion can be used to replace loops. Recursively defined data structures, like lists, are very well-suited to processing by recursive procedures and functions A recursive procedure is mathematically more elegant than one using loops. Sometimes procedures that would be tricky to write using a loop are straightforward using recursion.

Recursion: Final Remarks The trick with recursion is to ensure that each recursive call gets closer to a base case. Recursion can always be used instead of a loop. (This is a mathematical fact.) In declarative programming languages, like Prolog, there are no loops. There is only recursion. Recursion is elegant and sometimes very handy, but it is marginally less efficient than a loop, because of the overhead associated with maintaining the stack.

Recursive Versus Iterative Methods All recursive algorithms/methods can be rewritten without recursion. Iterative methods use loops instead of recursion Iterative methods generally run faster and use less memory--less overhead in keeping track of method calls

So When Should You Use Recursion? Solutions/algorithms for some problems are inherently recursive –iterative implementation could be more complicated When efficiency is less important –it might make the code easier to understand Bottom line is about: –Algorithm design –Tradeoff between readability and efficiency

Exercise 1: Recursion Convert number from H10->H

Summary Recursive call: a method that calls itself Powerful for algorithm design at times Recursive algorithm design: Decomposition (smaller identical problems) Composition (combine results) Base case(s) (smallest problem, no recursive calls) Implementation –Conditional (e.g. if) statements to separate different cases –Avoid infinite recursion Problem is getting smaller (decomposition) Base case exists and reachable –Composition could be tricky