CS 108 Computing Fundamentals March 31, 2015. Grades not as good as I hoped I drop the lowest of the first 3 exams Exam #3 and #4 will cover much of the.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
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.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
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.
J. Michael Moore Recursion CSCE 110 From James Tam’s material.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
RECURSION.
M180: Data Structures & Algorithms in Java
Recursion.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
IB Computer Science Unit 5 – Advanced Topics Recursion.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
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.
Sections 4.1 & 4.2 Recursive Definitions,
Recursive Algorithms Section 5.4.
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 DRILL: Please take out your notes on Recursion
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CS 108 Computing Fundamentals November 2, 2017.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Lesson #6 Modular Programming and Functions.
Lecture 17 Recursion part 1 Richard Gesick.
Fundamentals of Programming
Functions Recursion CSCI 230
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Unit 3 Test: Friday.
Lesson #6 Modular Programming and Functions.
Lecture 12 Recursion part 1 CSE /26/2018.
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion.
Presentation transcript:

CS 108 Computing Fundamentals March 31, 2015

Grades not as good as I hoped I drop the lowest of the first 3 exams Exam #3 and #4 will cover much of the same ground We will use today's review of Exam #2 as a means to clear-up some lack of understanding as well as exam strategy Review Exam #2

Pronunciation: ree-kur-zhun Function: noun Etymology: Late Latin recursion, recursio, from recurrere 1 : RETURN 2 : the determination of a succession of elements (as numbers or functions) by operation on one or more preceding elements according to a rule or formula involving a finite number of steps 3 : a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition so that successive repetitions are processed up to the critical step until the condition is met at which time the rest of each repetition is processed from the last one called to the first. Compare with ITERATION Recursion

A recursive function is a function that calls itself. Anything that can be solved iteratively can be also be solved recursively and vice versa. Recursion is sometimes useful because the recursive solution to a problem can sometimes be expressed more simply and succinctly than an iterative solution. Let’s look at a classic example Recursion

Main Entry: factorial Function: noun 1 : the product of all the positive integers from 1 to n ; symbol n! 2 : the quantity 0! arbitrarily defined as equal to 1 Example: Factorial

factorial(0) = 1 (by definition… see previous slide) factorial(1) = 1 * 1 factorial(2) = 2 * 1 *1 factorial(3) = 3 * 2 * 1 *1 factorial(4) = 4 * 3 * 2 *1 *1 factorial(5) = 5 * 4 * 3 * 2 *1 *1 factorial(6) = 6 * 5 * 4 * 3 * 2 *1 *1 factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 *1 *1 factorial(8) = 8 *7 * 6 * 5 * 4 * 3 * 2 *1 *1

First: Factorial Via Iteration #include // 1.c int factorial ( int ) ; int main (void) { int input = 0, answer = 0; printf("\n\n The program computes the factorial of an entered integer. ") ; printf("\n Enter an postive integer: ") ; scanf("%d", &input ) ; answer = factorial ( input ) ; printf("\n\n %d factorial equals: %d \n\n", input, answer ) ; return (0) ; } // continued on next slide

First: Factorial Via Iteration // continued from previous slide int factorial ( int passed_input ) { int index = 1, result = 1; for ( index = 1 ; index <= passed_input ; index++ ) { result = result * index ; } return (result) ; }

Factorial Via Recursion factorial(0) = 1 (by definition) factorial(1) = 1 * 1 is the same as 1 * factorial(0) factorial(2) = 2 * 1 * 1 is the same as 2 * factorial(1) factorial(3) = 3 * 2 * 1 * 1 is the same as 3 * factorial(2) factorial(4) = 4 * 3 * 2 * 1 * 1 is the same as 4 * factorial(3) factorial(5) = 5 * 4 * 3 * 2 * 1 * 1 is the same as 5 * factorial(4) factorial(6) = 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 6 * factorial(5) factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 7 * factorial(6) factorial(8) = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 8 * factorial(7) Is there a general way to state our observation of factorials (above) into rules for an algorithm using factorial(n) as a starting point?

Factorial Via Recursion factorial(0) = 1 (by definition) factorial(1) = 1 * 1 is the same as 1 * factorial(0) factorial(2) = 2 * 1 * 1 is the same as 2 * factorial(1) factorial(3) = 3 * 2 * 1 * 1 is the same as 3 * factorial(2) factorial(4) = 4 * 3 * 2 * 1 * 1 is the same as 4 * factorial(3) factorial(5) = 5 * 4 * 3 * 2 * 1 * 1 is the same as 5 * factorial(4) factorial(6) = 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 6 * factorial(5) factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 7 * factorial(6) factorial(8) = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 8 * factorial(7) If n = = 0 then answer = 1 … factorial(0) = 1 For all other cases, what is the relationship between factorial(n) on the left side of the assignment operator and the formula on the right side? The next slide might be more helpful.

Factorial Via Recursion factorial(0) = 1 (by definition) factorial(1) = 1 * factorial(0) factorial(2) = 2 * factorial(1) factorial(3) = 3 * factorial(2) factorial(4) = 4 * factorial(3) factorial(5) = 5 * factorial(4) factorial(6) = 6 * factorial(5) factorial(7) = 7 * factorial(6) factorial(8) = 8 * factorial(7) For all cases other than n = = 0, what is the relationship between factorial(n) on the left side of the assignment operator and the formula on the right side? factorial ( n ) = ??

Factorial Via Recursion First: answer = 1 if n = = 0 … factorial(0) = 1 Second: otherwise: factorial(n) = n * factorial( n - 1 ) Notice that the identifier "factorial" appears on both sides of the assignment operator How would we write a factorial function to implement these two rules into a C algorithm ?

Factorial Via Recursion First: 1 if n = 0 Second: factorial( n ) = n * factorial( n-1 )if n > 0 int factorial ( int n ) { if n = = 0 return 1 ; else return ( n * factorial ( n – 1 ) ); }

Understanding Recursion You can think of a recursive function call as if it were calling a completely separate function. Understand this about recursively called functions: the operations that can be performed by recursively called functions are the same, but the data that is input to each recursively called function is different The next slide might help to visualize the sameness of operations and the differences in data through the use of different (but very, very similar) functions... we’ll pass factorial_a the value 3.

Understanding Recursion int factorial_b ( int n ) { if ( n = = 0 ) return 1; else return n * factorial_c ( n - 1) ; } int factorial_c ( int q ) { if( q = = 0 ) return 1; else return q * factorial_d ( q - 1) ; } int factorial_a ( int m ) { if ( m = = 0 ) return 1; else return m * factorial_b ( m - 1 ) ; } int factorial_d ( int r ) { if( r = = 0 ) return 1; else return r * factorial_e ( r - 1) ; }

Let's develop a symbol table for the previous slide which is incorporated in this program (this program can handle 0 – 3 as inputs, which is enough to demonstrate the concepts) : Let's look at the addresses and values of the variables/parameters Notice that each version of the factorial functions does the same thing Therefore, we can condense this into something more general

Recursion Example: Factorial #include // 2.c int factorial ( int ) ; int main (void) { int input = 0, answer = 0; printf( " \n\n The program computes the factorial of an entered integer. " ) ; printf( " \n Enter an postive integer: " ) ; scanf( " %d ", &input ) ; answer = factorial ( input ) ; printf( " \n\n %d factorial equals: %d \n\n ", input, answer ) ; return 0 ; } int factorial ( int passed_input ) { if ( passed_input = = 0) // if cutting, remove the extra space between = and = return 1 ; else return ( passed_input * factorial ( passed_input - 1 ) ) ; }

Let's develop a symbol table for the previous slide which is incorporated in this program:

Questions to Guide The Use of Recursion Can you define the problem in terms of smaller problem(s) of the same type? ­ Think about the factorial problem: factorial(n) = n * factorial ( n-1 ) if n > 0 Does each recursive call reduce the size of the problem? As the problem "shrinks", will you eventually reach a point where an obvious solution presents itself? ­ Again, think about the factorial problem: factorial (0) = 1