Another problem to solve…

Slides:



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

Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
CS252: Systems Programming Ninghui Li Program Interview Questions.
CIT 590 Recursion. A function calling itself Factorial def factorial(n): if n == 0: return 1 return n * factorial(n-1)
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Factorial Recursion stack Binary Search Towers of Hanoi
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
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.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
Recursion Apan Qasem Texas State University Spring 2011.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
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.
10 Recursion © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
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.
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.
EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
©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.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
The last CS 5 lecture you’ll ever need! Inducing labor for the machine! == Reducing labor for humans! On Warner Brothers' insistence, we affirm that this.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CPS Today’s topics Programming Recursion Invariants Reading Great Ideas, p Brookshear, Section Upcoming Copyrights, patents, and.
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.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion ● Recursion or Iteration ● A Common Recursive Design Pattern ● Computing factorials ● Searching a filesystem tree ● Faster exponentiation ● Slow.
CS212: Data Structures and Algorithms
Recursion.
Chapter 4 Stacks
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter Topics Chapter 16 discusses the following main topics:
Topic: Recursion – Part 2
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Abdulmotaleb El Saddik University of Ottawa
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Chapter 15 Recursion.
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
EECS 110: Lec 4: Functions and Recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Applied Algorithms (Lecture 17) Recursion Fall-23
Algorithm Analysis (for Divide-and-Conquer problems)
Data Structures & Algorithms
Recursion.
CS1120: Recursion.
Another problem to solve…
EECS 110: Lec 4: Functions and Recursion
CSC 143 Recursion.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 19: Recursion.
Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Another problem to solve…
Recursion.
Recursive Thinking.
Lecture 6 - Recursion.
Presentation transcript:

Another problem to solve… We want to move a pile of discs of different size from one pole to another, with the condition that no larger disc can sit on top of a smaller disc! href="https://commons.wikimedia.org/wiki/File:Tower_of_Hanoi.jpeg#/media/File:Tower_of_Hanoi.jpeg

Watch a video … https://en.wikipedia.org/wiki/Tower_of_Hanoi#/media/File:Tower_of_Hanoi_4.gif

A Python solution to this problem

How to compute factorial ? F(n) = n * F(n-1) for n > 0 F(0) = 1

These types of problems lead to a category of solution called recursion! While some examples of recursion may be more complicated than the level of CSCI 203, we will learn the basic strategy in this class. Recursion will be studied in more depth in other CS courses.

Two important features of a recursive solution A recursive solution must have one or more base cases (when to stop) E.g., factorial(0) == 1 A recursive solution can be expressed in the exact same solution with a smaller problem size E.g., factorial(n) = n * factorial(n-1)

In the case of computing factorial F(n) = n * F(n-1) for n > 0 F(0) = 1 Base case Recursion with smaller problem

In the case of Tower of Hanoi Implicit base case when height < 1 Two recursions with smaller problems

Recursion in nature The key: self-similarity How is a tree defined recursively? A tree consists of a main branch with smaller trees extending, until the subtree is too small to extend further – a leaf! The key: self-similarity

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… >>> fac(1) Result: 1 The base case is No Problem!

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5)

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) 5 * fac(4)

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3)

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2)

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * fac(1)

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) "The Stack" 5 * fac(4) 4 * fac(3) 3 * fac(2) Remembers all of the individual calls to fac 2 * fac(1) 1

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * 1

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * 2

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) 5 * fac(4) 4 * 6

Behind the curtain… def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) 5 * 24

Behind the curtain… Look familiar? def fac(n): if n <= 1: return 1 else: return n * fac(n-1) Behind the curtain… fac(5) Result: 120 0 N*** -> X 1 0 x*** -> N 0 Base Case Look familiar? Recursive Step

Let recursion do the work for you! Exploit self-similarity Less work ! Produce short, elegant code def factorial(n): if n <= 1: return 1 else: return n * factorial(n-1) You handle the base case – the easiest possible case to think of! Recursion does almost all of the rest of the problem! Always a “smaller” problem!

Question: Sum How to modify factorial function to give us a sum of integers from 1 to n? def factorial(n): if n <= 1: return 1 else: return n * factorial(n-1) def sumn(n): if n <= 1: return 1 else: return n +sumn(n-1)

Exercise 1 Write a function called myLen that computes the length of a string Example: myLen("aliens")  6 What is the base case? Empty string myLen('')  0 Recursive call: 1 + myLen(s[1:])

myLen def myLen(s): """ input: any string, s output: the number of characters in s """ if s == '': return 0 else: rest = s[1:] lenOfRest = myLen( rest ) totalLen = 1 + lenOfRest return totalLen

myLen def myLen(s): """ input: any string, s output: the number of characters in s """ if s == '': return 0 else: rest = s[1:] return 1 + myLen( rest )

myLen def myLen(s): """ input: any string, s output: the number of characters in s """ if s == '': return 0 else: return 1 + myLen(s[1:])

Behind the curtain: how recursion works... myLen('csi') 3 def myLen(s): if s == '': return 0 else: return 1 + myLen(s[1:]) Behind the curtain: how recursion works... myLen('csi') 3 1 + myLen('si') 3 1 + myLen('i') 2 1 1 + myLen('')

MORE RECURSION!! def sumOfDigits(s): """ input: a string of numbers -‘252674’ output: the sum of the numbers """ if else: