Recursion CMSC 201. Recursion Vs. Iteration Recursion and iteration are both methods of solving problems. An iterative solution to a problem is anything.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Factorial Recursion stack Binary Search Towers of Hanoi
Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,
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.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
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)
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Recursion … just in case you didn’t love loops enough …
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Recursion. Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms Lecture.
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.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
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.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
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 Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
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.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
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.
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.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion To understand recursion, you first have to understand recursion.
Recursion.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
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
Recursion Chapter 11.
Fundamentals of Programming
Chapter 8: Recursion Java Software Solutions
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
Recursion Taken from notes by Dr. Neil Moore
Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

Recursion CMSC 201

Recursion Vs. Iteration Recursion and iteration are both methods of solving problems. An iterative solution to a problem is anything involving loops. Everything we’ve done so far has been iterative. Anything that can be done iteratively can be done recursively.

Recursion Recursion is when a function calls itself. The simplest example: def foo(): foo() This is an example of infinite recursion.

Recursion Infinite recursion: def foo(): foo() Infinite loop: while True: pass

Recursion We can get loop like behavior by passing arguments! def foo(i): print(i) foo(i-1) What does this do? How might we get it to terminate?

Recursion def foo(i): if(i > 0): return print(i) foo(i-1) Is equivalent to: while a > 0: print(a) a = a + 1

Recursion def foo(i): if(i > 0): return print(i) foo(i-1) Is equivalent to: while a > 0: print(a) a = a + 1 Termination condition, known as the “base case” Recursive call, which should always take us closer to the base case.

Recursion We use recursion when we have a problem that can be written in terms of a smaller version of the original problem. Take factorial, for example. N! = N * (N-1)! We can rewrite factorial in terms of something easy (multiplication), and a smaller factorial. One more thing! For this to work, we need to know that 1! = 1

def factorial(num): if(num == 1): return 1 return num * factorial(num-1) Factorial

Sometimes in recursion we have to pass information as arguments. When we do this, we use a helper function to keep the user from sending incorrect arguments (this will make more sense in the next slide). Helper Functions

def myMax(myList): return _myMax(myList, myList[0]) def _myMax(myList, arg1): if(len(myList) == 0): return arg1 return _myMax(myList[1:], max(arg1, myList[0]))

Hints For these exercises, remember: Figure out the base case Figure out a way of restating the original problem in terms of something simple, and a smaller version of the original problem. When writing your recursive call, you’re allowed to assume your function works!

Exercise Right a recursive function that takes a list as an argument and reverses it. def reverse(myList): #Your code here

Exercise Write a recursive function that takes a list as an argument and reverses it. def reverse(myList): if(len(myList) == 1): return myList return [myList[-1]] + reverse(myList[:-1])

Exercise Write a recursive function that takes two sorted lists, listA and listB, and merges them (so that they’re still sorted.) So merge([1, 3, 5], [2, 4, 6, 7]) should return [1, 2, 3, 4, 5, 6 7] def merge(): #your code here

Exercise def merge(listA, listB): if(len(listA) == 0): return listB if(len(listB) == 0): return listA if(listA[0] > listB[0]): return [listB[0]] + merge(listA, listB[1:]) else: return [listA[0]] + merge(listA[1:], listB)

Towers Of Hanoi This one is a bit more complicated! This is the towers of Hanoi puzzle: The goal is to move the entire stack over to the peg on the far right.

Towers Of Hanoi The Rules: No disk may be on top of a smaller disk Every disk must go from one peg to another (no storing it o the table)

Towers Of Hanoi We want to write a function, called move, that moves the entire stack, down to whatever depth we specify, to whatever peg we wish. def hanoi(depthOfDisk, movingFrom, movingTo, otherPeg): #your code The variable otherPeg is just to keep track of the free peg we’re not using. Also, you can assume the function move(from, to) moves the disk at the top of peg from to peg to

Towers Of Hanoi def hanoi(depthOfDisk, movingFrom, movingTo, otherPeg): if(depthOfDisk == 1): move(movingFrom, movingTo) else: hanoi(depthOfDisk – 1, movingFrom, otherPeg, movingTo) move(movingFrom, movingTo) hanoi(depthOfDisk – 1, otherPeg, movingTo, movingFrom)