Recursive Thinking.

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

Factorial Recursion stack Binary Search Towers of Hanoi
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
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.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion Chapter 7.
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.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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.
Data Structures and Abstractions with Java, 4e Frank Carrano
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
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.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Recursion Powerful Tool
Review of Recursion What is a Recursive Method?
Recursion.
Sections 4.1 & 4.2 Recursive Definitions,
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion CENG 707.
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.
Recursion Topic 5.
Topic 6 Recursion.
Chapter 15 Recursion.
Abdulmotaleb El Saddik University of Ottawa
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.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
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.
Recursive Definitions
Algorithm Analysis (for Divide-and-Conquer problems)
Chapter 12 Recursion (methods calling themselves)
Recursion Chapter 11.
This Lecture Substitution model
Review of Recursion What is a Recursive Method?
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Java Programming: Chapter 9: Recursion Second Edition
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 Chapter 12.
Review of Recursion What is a Recursive Method?
Presentation transcript:

Recursive Thinking

The Factorial Function n!= n(n-1)(n-2)…. 3 2 1 The standard Factorial Function written without recursion is provided below: // The Non-Recursive Factorial Function public static int simpleFactorial( int n ) { if (n < 0) return 0; // Make sure n >= 0 int product = 1; // “Base Case” Product is one. for (int k = 1; k < n; k ++) product *= k; return product; }

The Factorial Function n!= n(n-1)(n-2)…. 3 2 1 n!= n  (n-1)! Can break a difficult problem ( n! ) into a smaller, more manageable problem of a similar structure ( (n-1)! is now easier than calculating n!) (n-1)!

What Is Recursion? It is a problem-solving process Breaks a problem into identical but smaller problems Eventually you reach a smallest problem Answer is obvious or trivial Using that solution enables you to solve the previous problems Eventually the original problem is solved

Recursive Factorial Function // A Recursive Factorial Function public static int factorial( int n ) { if (n < 0) return 0; // Make sure n >= 0 if (n == 0 || n == 1) return 1; // Base Cases return ( n * factorial (n-1) ); // Recursive Call } Conclusion: recursion splits a problem Into one or more simpler versions of itself

Is it possible to solve any problem recursively?

Requirements for Recursive Solution At least one “small” case that you can solve directly (base case) A way of breaking a larger problem down into: One or more smaller sub-problems Each of the same kind as the original A way of combining sub-problem results into an overall solution to the larger problem

Writing a Recursive Method Method definition must provide parameter Leads to different cases Typically includes an if or a switch statement One or more of these cases should provide a non recursive solution The base or stopping case One or more cases includes recursive call Takes a step towards the base case

Recursive Design Example Write a recursive algorithm for finding length of a string ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’

Recursive algorithm for finding the length of a string if string is empty (no characters) return 0  base case else  recursive case compute length of string without first character return 1 + that length

Recursive algorithm for finding length of a string public static int length (String str) { if (str == null || str.equals(“”)) return 0; else return length(str.substring(1)) + 1; }

Recursive algorithm for finding length of a string Overall result length(“ace”) 3 return 1 + length(“ce”) 2 return 1 + length(“e”) 1 return 1 + length(“”)

Simple recursive example void test(int i) { if (i > 1) test(i / 2); } System.out.print("*"); How many asterisks are printed by the method call test(5)?

Carrano, Data Structures and Abstractions with Java, Second Edition Towers of Hanoi The initial configuration of the Towers of Hanoi for three disks Carrano, Data Structures and Abstractions with Java, Second Edition

Carrano, Data Structures and Abstractions with Java, Second Edition Towers of Hanoi Rules for the Towers of Hanoi game Move one disk at a time. Each disk you move must be a topmost disk. No disk may rest on top of a disk smaller than itself. You can store disks on the second pole temporarily, as long as you observe the previous two rules. Carrano, Data Structures and Abstractions with Java, Second Edition

Carrano, Data Structures and Abstractions with Java, Second Edition Towers of Hanoi The sequence of moves for solving the Towers of Hanoi problem with three disks. Carrano, Data Structures and Abstractions with Java, Second Edition

Carrano, Data Structures and Abstractions with Java, Second Edition Towers of Hanoi The smaller problems in a recursive solution for four disks Carrano, Data Structures and Abstractions with Java, Second Edition

Towers of Hanoi Algorithm solveTowers (numberOfDisks, startPole, tempPole, endPole) if (numberOfDisks == 1) Move disk from startPole to endPole else { solveTowers (numberOfDisks - 1, startPole, endPole, tempPole) solveTowers (numberOfDisks - 1, tempPole, startPole, endPole) } The smaller problems in a recursive solution for four disks Carrano, Data Structures and Abstractions with Java, Second Edition

Debugging a recursive method If a recursive method does not work, check the following: Does the method have at least one input value? Does the method contain a statement that tests an input value and leads to different cases? Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? Does these recursive calls get closer to the base case? Did you consider a base case? Carrano, Data Structures and Abstractions with Java, Second Edition

How to prove the correctness of a recursive algorithm?

Correctness of a recursive algorithm From Algorithm theory: an algorithm is correct if it satisfies the following TWO conditions: Produces correct results for all valid inputs Always terminates

Proof by Induction Prove the theorem for the base case(s): n=0 Show that: If the theorem is assumed true for n, Then it must be true for n+1 Result: Theorem true for all n ≥ 0.

Correctness of a recursive algorithm Recursive proof is similar to induction: Show base case recognized and solved correctly Show that If all smaller problems are solved correctly, Then original problem is also solved correctly Show that each recursive case makes progress towards the base case  termination properly

Pros and Cons of recursive methods Recursive code is often simpler than iterative (easier to write, read, and debug) Slower than iterative