Topic: Recursion – Part 1

Slides:



Advertisements
Similar presentations
New Mexico Computer Science For All
Advertisements

Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Recursion CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
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.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Concepts of Algorithmic Thinking Iterations, or Loops Once is not Enough D.A. Clements, Lecturer Information School 2/23/2016 D.A. Clements, MLIS, UW Information.
CMPT 120 Topic: Sorting Algorithms – Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n -> O(n) i.e., has.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Identify the Appropriate Method for Handling Repetition
Programming and Data Structures
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:
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: Introduction to Computing Science and Programming + Algorithm
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Recursion – Part 2
Topic: Functions – Part 1
Topic: Programming Languages and their Evolution + Intro to Scratch
Introduction to Recursion
CMPT 120 Topic: Python Modules.
CMPT 120 Topic: Searching – Part 2
Abdulmotaleb El Saddik University of Ottawa
Topic: Introduction to Computing Science and Programming + Algorithm
Lesson #6 Modular Programming and Functions.
Topic: Python’s building blocks -> Statements
Topic: Conditional Statements – Part 1
Topic: Python’s building blocks -> Variables, Values, and Types
Lesson #6 Modular Programming and Functions.
CMPT 120 Topic: Searching – Part 1
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
CMPT 120 Topic: Functions – Part 4
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
CMPT 120 Topic: Python strings.
Topic: Functions – Part 2
University of Washington Computer Programming I
Lesson #6 Modular Programming and Functions.
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Recursion Chapter 11.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Design and Implementation
Lesson #6 Modular Programming and Functions.
Recursion Taken from notes by Dr. Neil Moore
Yan Shi CS/SE 2630 Lecture Notes
Intro to Computer Science CS1510 Dr. Sarah Diesburg
ICS103 Programming in C Lecture 11: Recursive Functions
Hour of Code Code.org/lightbot
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 15 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Lecture 20 – Practice Exercises 4
Lecture 20 – Practice Exercises 4
CMPT 225 Lecture 7 – Stack.
Presentation transcript:

Topic: Recursion – Part 1 CMPT 120 Topic: Recursion – Part 1

Last Lectures Developing Software that incorporates functions Way 1 – the program does not exist yet Way 2 – the program has already been written Illustrated using a Case study In the process, we pointed out a few guidelines: Decomposition Function Interface Design Generalization Composition Encapsulation And incremental development

Learning outcomes At the end of this course, a student is expected to: Describe the concept of recursion, of recursive definitions and recursive functions. Use recursion to solve problems: Use box tracing to predict the result of simple recursive code (drawing a call stack) Design recursive code Design recursive functions that recurse, for example, on lists and strings

Today’s Menu Recursion Examples of recursion occurring in the real world Examples of recursion occurring in the mathematical world What is recursion Iterative code versus Recursive code Demo of recursion

Recursion in the real world Russian dolls

Recursion in the real world Searching for the word “guffaw” in a dictionary Source: http://www.eslstation.net/ESL310L/310L_dict.htm

Recursion in the real world Droste Effect Posted on our course web site

Recursion in the mathematical world Multiply two numbers

Recursion in the mathematical world Compute factorials

Recursion - Definition Recursion occurs when an object or a process is defined in terms of itself (or a version of itself)  In mathematics and computer science, a kind of objects or processes exhibit recursive behavior when they can be defined by two properties: A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer A set of rules that reduce all other cases toward the base case Adapted from http://en.wikipedia.org/wiki/Recursion

Definition of recursion applied to Russian dolls A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer A set of rules that reduce all other cases toward the base case

Definition of recursion applied to multiplying two #’s A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer A set of rules that reduce all other cases toward the base case

Definition of recursion applied to factorials A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer A set of rules that reduce all other cases toward the base case

Let’s now have a look at recursion in the computer/software world

So far -> iterative statements So far, when solving problems, we have achieved repetition (i.e., repeating statements in our code) by using iterative statements -> loops By putting statements we wanted to repeat in a loop

Recursive functions Another way of achieving repetition (i.e., repeating statements in our code) is by putting statements we want to repeat in a function and calling the function itself a certain number of times Directly: Indirectly: functionA # recursive call functionA(…) function1 # recursive call function2(…) function 2 # recursive call function1(…)

Recursion –Demo using the Visualizer

Example - recursive factorial (slide with animation – to be seen in PowerPoint) Let’s apply what we have learnt to an example 5! = 24 120 5 * 4! 6 24 4 * 3! 2 Tada!!! 6 3 * 2! 1 2 2 * 1! 1 Base case When the base case is reached, the execution “recurses back”

Summary Recursion Examples of recursion occurring in the real world Examples of recursion occurring in the mathematical world What is recursion Iterative code versus Recursive code Demo of recursion

Next Lecture Recursion How to solve a problem using recursion