7. RECURSIONS Rocky K. C. Chang October 12, 2015.

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

C++ Programming:. Program Design Including
MAT 4 – Kompleks Funktionsteori MATEMATIK 4 INDUKTION OG REKURSION MM 1.4 MM 1.4: Induktion og Rekursion Topics: Mathematical induction Example of Towers.
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Factorial Recursion stack Binary Search Towers of Hanoi
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 2 Recursion:
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.
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.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Recursion. Binary search example postponed to end of lecture.
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.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
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.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
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.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
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.
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
1 Storage Classes, Scope, and Recursion Lecture 6.
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 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Fall 2015 COMP 2300 Discrete Structures for Computation Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1.
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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.
ISOM MIS 215 Module 4 – Recursion. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Recursion Chapter 11. How it works “A recursive computation solves a problem by using the solution of the same problem with simpler inputs” Big Java,
15. MORE RECURSIONS Rocky K. C. Chang November 22, 2015.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Recursion CENG 707.
All-pairs Shortest paths Transitive Closure
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Towers of Hanoi Move n (4) disks from pole A to pole C
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursive Thinking.
Recursion: The Mirrors
Presentation transcript:

7. RECURSIONS Rocky K. C. Chang October 12, 2015

Objectives To be able to design recursive solutions to solve "simple" problems. To understand the relationship between recursion and mathematical induction. To understand that recursion is a postponement of work. To know how to design recursive algorithms systematically.

WHAT IS RECURSION?

Example 1: Computation of n! Standard method: fact(n) = 1 * 2 * … * n Recursive method: fact(n) = n * fact(n-1) fact(n-1) = (n-1) * fact(n-2) … fact(2) = 2 * fact(1) fact(1) = 1 * fact(0) fact(0) = 1

Implement a recursive function to compute n!. EXERCISE 7.1

Example 2: Birthday cake cutting Question: how many pieces of a circular cake as a result of n cuts through the center? Write down your answer here.

Example 2 Recursive approach:

Example 3: Fibonacci numbers The rabbit population problem: Start out with a male-female pair of rabbits. It takes two months for each pair to be reproductive. Each pair reproduces another male-female pair in each subsequent month. How many pairs of male-female rabbits after n months if no rabbits die during this period? Standard method: 1 st month: 1 2 nd month: 1 3 rd month: 2 4 th month: 3 …

Example 3 Recursive method: Just prior to the start of n th month, there are fib(n-1) pairs. However, only fib(n-2) pairs can reproduce new pairs. Therefore, fib(n) = ?

RECURSION AND MATHEMATICAL INDUCTION

Parallel w. mathematical induction Mathematical induction is a very powerful theorem- proving technique. Proving that a statement T(n) is true for n ≥ 1 requires the proofs for (Base) T(1) is true. (Induction) For every n > 1, if T(n-1) is true, then T(n) is true.

Mathematical induction, e.g., Prove that for all natural numbers x and n, x n – 1 is divisible by x – 1. Base: when n = 1, x n – 1 is divisible by x – 1. Induction: For n > 1, assume that x n-1 – 1 is divisible by x – 1. Write x n - 1 = x(x n-1 – 1) + (x – 1). Therefore, x n – 1 is also divisible by x – 1.

Mathematical induction, e.g., Prove that if n is a natural number and 1 + x > 0, then (1 + x) n ≥ 1 + nx. Base: for n = 1, LHS = RHS = 1 + x. Induction: For n ≥ 1, assume that (1 + x) n ≥ 1 + nx. (1 + x) n+1 = (1 + x)(1 + x) n (1 + x) n+1 ≥ (1 + x)(1 + nx) = 1 + (n + 1)x + nx 2 (1 + x) n+1 ≥ 1 + (n + 1)x

The parallels A recursive process consists of two parts: A smallest case that is processed without recursion (base case) A general method that reduces a particular case into one or more of the smaller cases (induction step) Factorials Base: Induction: The birthday cake cutting problem Base: Induction: Fibonacci numbers Base: Induction:

RECURSION AS A POSTPONEMENT OF WORK

Tracing the recursive calls

A recursive tree for fact(n)

How does recursion work? When a program is run, each execution of a method is called an activation. The data objects associated with each activation are stored in the memory as an activation record. Data objects include parameters, return values, return address and any variables that are local to the method. A stack is a data structure to store all activation records in a correct order.

A recursive tree for fib(n)

Memory requirement for recursions

HOW TO DESIGN RECURSIVE ALGORITHMS?

Several important steps Find the key step. How can this problem be divided into parts? Find a stopping rule. It is usually the small, special case that is trivial or easy to handle without recursion. Outline your algorithm. Combine the stopping rule and the key step, using an if statement to select between them. Check termination. Verify that the recursion will always terminate. Draw a recursion tree.

MORE COMPLEX EXAMPLES

Tower of Hanoi Problem: To move a stack of n disks from pole A to pole B, subjecting to 2 rules: Only one disk can be moved at a time. A larger disk can never go on top of a smaller one. n = 1, trivial n = 2, trivial again n = 3: trivial to some people? n = 4: trivial to fewer people? n = 64?

Think recursively …

A recursive solution solveTowers(n, A, B, C) n: the number of disks moving the disks from A to B via C no larger disks put on top of smaller disks A recursive approach: Base: trivial and solved for n = 1 Induction; for n > 1, solveTowers(n, A, B, C) can be solved by solveTowers(n - 1, A, C, B) // the top n-1 disks solveTowers(1, A, B, C) // the largest disk solveTowers(n - 1, C, B, A) // the top n-1 disks

A recursion tree for n = 3

Computing  Write R(n) = Q(1) × Q(2) × Q(3) × … × Q(n), where Q(1) = sqrt[½+ ½ sqrt(½)], Q(2) = sqrt[½+ ½ Q(1)], …, Q(n) = sqrt[½+ ½ Q(n – 1)], n > 1. Therefore, Base: R(1) = Q(1) Induction: Given R(n – 1) and Q(n – 1), Q(n) = sqrt[½+ ½ Q(n – 1)] and R(n) = R(n – 1) × Q(n).

The recursion tree

Summary Recursion is a divide-and-conquer approach to solving a complex problem. Divide, conquer, and glue Similar to mathematical induction, recursion has a “ base ” case and an “ induction ” step. A recursion implementation essentially postpones the work until hitting the base case. Recursions appear in many, many, …, different forms.

Acknowledgments The figures on pp are taken from Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall, Inc., The figures on pp are taken from F. Carrano and J. Prichard, Data Abstraction and Problem Solving with JAVA Wall and Mirrors, First Edition, Addison Wesley, 2003.

END