Recursion Introduction to Computing Science and Programming I.

Slides:



Advertisements
Similar presentations
Genome 559: Introduction to Statistical and Computational Genomics
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Recursion Michael Ernst UW CSE 140 To seal: moisten flap, fold over, and seal.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
Recursion Chapter 11 Chapter 11.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Csci1300 Introduction to Programming Recursion Dan Feng.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Searching/Sorting Introduction to Computing Science and Programming I.
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.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Recursion.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
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 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
1 Algorithms CSCI 235, Fall 2015 Lecture 7 Recurrences II.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
Recursion Powerful Tool
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Introduction to Computing Science and Programming I
Introduction to Recursion
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Java 4/4/2017 Recursion.
Programming with Recursion
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
And now for something completely different . . .
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Recursion Output Input
Applied Algorithms (Lecture 17) Recursion Fall-23
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Recursion UW CSE 160 Winter 2017
Recursion UW CSE 160 Spring 2018
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Topic 1: Problem Solving
PROGRAMMING IN HASKELL
Recursion UW CSE 160 Winter 2016
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion Taken from notes by Dr. Neil Moore
And now for something completely different . . .
Divide & Conquer Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 7 Recurrences II
Last Class We Covered Recursion Stacks Parts of a recursive function:
Presentation transcript:

Recursion Introduction to Computing Science and Programming I

Recursion Remember that the factorial function, x!, is defined as x * (x-1) * (x-2)…*2*1 Remember that the factorial function, x!, is defined as x * (x-1) * (x-2)…*2*1 Here’s a solution to this using a for loop Here’s a solution to this using a for loop def factorial(x): ans = 1 ans = 1 for i in range(x): for i in range(x): ans = ans * (i+1) ans = ans * (i+1) return ans return ans

Recursion There’s another way you can define the factorial function There’s another way you can define the factorial function x! = x * (x-1)! x! = x * (x-1)! = x * (x-1) * (x-2)! = x * (x-1) * (x-2)! = x * (x-1) * (x-2) *… * 1 * 0! = x * (x-1) * (x-2) *… * 1 * 0! 0! = 1 by definition 0! = 1 by definition In this way the function can be defined in terms of itself. In code you can call a function from within itself, this is recursion. In this way the function can be defined in terms of itself. In code you can call a function from within itself, this is recursion.

Recursion Recursion takes place when a function calls itself. The basic goal is to find a simpler version of the same function. Recursion takes place when a function calls itself. The basic goal is to find a simpler version of the same function. Another factorial function definition Another factorial function definition def factorial(x): def factorial(x): if (x==0): if (x==0): return 1 return 1 else: else: return x * factorial(x-1) return x * factorial(x-1)

Recursion What happens when we call factorial(3) What happens when we call factorial(3) factorial(3) makes a call to factorial(2) factorial(3) makes a call to factorial(2) factorial(2) makes a call to factorial(1) factorial(2) makes a call to factorial(1) factorial(1) calls factorial(0) factorial(1) calls factorial(0) factorial(0) returns 1 factorial(0) returns 1 factorial(1) completes and returns 1 factorial(1) completes and returns 1 Now factorial(2) can complete and returns 2 Now factorial(2) can complete and returns 2 And factorial(3) can complete and returns 6 And factorial(3) can complete and returns 6

Recursion The factorial function illustrates the two parts the every recursive function needs to have. The factorial function illustrates the two parts the every recursive function needs to have. 1. The recursion 1. The recursion def factorial(x): def factorial(x): if (x==0): if (x==0): return 1 return 1 else: else: return x * factorial(x-1) return x * factorial(x-1) The recursion is where the function calls itself where it needs the answer to a simpler (smaller) version of the problem. factorial(x) needs the answer to factorial(x-1) to complete The recursion is where the function calls itself where it needs the answer to a simpler (smaller) version of the problem. factorial(x) needs the answer to factorial(x-1) to complete

Recursion 1. The base case 1. The base case def factorial(x): def factorial(x): if (x==0): if (x==0): return 1 return 1 else: else: return x * factorial(x-1) return x * factorial(x-1) The base case gives a point where the problem doesn’t need to be broken down into a smaller version. The base case gives a point where the problem doesn’t need to be broken down into a smaller version. What would happen without a base case? What would happen without a base case? def factorial(x): def factorial(x): return x * factorial(x-1) return x * factorial(x-1)

Recursion How to set up a recursive function. How to set up a recursive function. Find a simpler subproblem Find a simpler subproblem This is the recursion. Each recursive call should bring the problem closer to the base case. This is the recursion. Each recursive call should bring the problem closer to the base case. Find a base case that doesn’t require a recursive call. Find a base case that doesn’t require a recursive call. Make sure you don’t make a recursive call before you check the base case. Make sure you don’t make a recursive call before you check the base case. def factorial(x): ans = x * factorial(x-1) if x==0: if x==0: ans = 1 ans = 1 return ans return ans

Recursion Recursion can be used to reverse the order of items in a list. Recursion can be used to reverse the order of items in a list. The subproblem isn’t as obvious as it was with the factorial example. The subproblem isn’t as obvious as it was with the factorial example. If you reverse the tail of a list (all but the first element) and then append the head (first element) you’ve reversed the list. If you reverse the tail of a list (all but the first element) and then append the head (first element) you’ve reversed the list. [1, 2, 3, 4] ---> [4, 3, 2, 1] [1, 2, 3, 4] ---> [4, 3, 2, 1] [2, 3, 4] ---> [4, 3, 2] [2, 3, 4] ---> [4, 3, 2]

Recursion So we have our recursion. We’ll repeatedly call reverse for the tail of the list. So we have our recursion. We’ll repeatedly call reverse for the tail of the list. At what point do we reach a base case? At what point do we reach a base case? A list that is empty or has one element is the reverse of itself, so this would be a good spot to end the recursion. A list that is empty or has one element is the reverse of itself, so this would be a good spot to end the recursion.

Recursion Reverse List function Reverse List function def reverseList(x): if len(x) <= 1: if len(x) <= 1: return x return x ans = reverseList(x[1:]) ans = reverseList(x[1:]) ans = ans.append(x[0]) ans = ans.append(x[0]) return ans return ans

Recursion Triangle of numbers Triangle of numbers We want to print a triangle of numbers with a given number of rows. We want to print a triangle of numbers with a given number of rows. if rows = 5 if rows =

Recursion If we want to print a triangle with 5 rows, we first need to print out the first four rows. This will be our recursive case. If we want to print a triangle with 5 rows, we first need to print out the first four rows. This will be our recursive case. Once we get down to printing out a single row we don’t need to recurse any more so this will be our base case. Once we get down to printing out a single row we don’t need to recurse any more so this will be our base case.

Recursion def numTriangle(rows): if rows == 0: if rows == 0: return return else: else: numTriangle(rows-1) numTriangle(rows-1) line = “” line = “” for i in range(rows): for i in range(rows): line = line + str(i+1) line = line + str(i+1) print line print line

Recursion Remember that the sorting algorithms we looked at had running time n 2 while the best algorithms have running time n log n Remember that the sorting algorithms we looked at had running time n 2 while the best algorithms have running time n log n One of these n log n algorithms is merge sort. Merge sort uses the idea of splitting the problem into smaller pieces and recursion to get this advantage. One of these n log n algorithms is merge sort. Merge sort uses the idea of splitting the problem into smaller pieces and recursion to get this advantage.

Recursion The basic idea behind merge sort The basic idea behind merge sort Split the list into two halves Split the list into two halves Recursively sort each half Recursively sort each half Merge th two sorted halves Merge th two sorted halves [3, 6, 9, 4, 1, 7, 2, 8] [3, 6, 9, 4, 1, 7, 2, 8] [3, 6, 9, 4] [1, 7, 2, 8] [3, 6, 9, 4] [1, 7, 2, 8] [3, 6] [9, 4] [1, 7] [2, 8] [3, 6] [9, 4] [1, 7] [2, 8] [3] [6] [9] [4] [1] [7] [2] [8] [3] [6] [9] [4] [1] [7] [2] [8] We reached the base case, so now start merging. We reached the base case, so now start merging. [3, 6] [4, 9] [1, 7] [2, 8] [3, 6] [4, 9] [1, 7] [2, 8] [3, 4, 6, 9] [1, 2, 7, 8] [3, 4, 6, 9] [1, 2, 7, 8] [1, 2, 3, 4, 6, 7, 8, 9] [1, 2, 3, 4, 6, 7, 8, 9]