CSE / ENGR 142 Programming I

Slides:



Advertisements
Similar presentations
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Advertisements

Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
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.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Prof. S.M. Lee Department of Computer Science.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
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.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Chapter 8 Recursion Modified.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Chapter 6 Questions Quick Quiz
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
RECURSIONL27-L28. To learn and the following concepts To design a recursive algorithm To solve problems using recursion To understand the relationship.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Program Development and Design Using C++, Third Edition
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
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.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Programming with Recursion
Recursion Riley Chapter 14 Sections 14.1 – 14.5 (14.6 optional)
COMP 51 Week Fourteen Recursion.
Recursion what is it? how to build recursive algorithms
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Chapter 15 Recursion.
Recursion CSE 2320 – Algorithms and Data Structures
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
University of Washington Computer Programming I
Programming with Recursion
Lesson #6 Modular Programming and Functions.
Recursion Chapter 11.
Recursion Data Structures.
Functions Recursion CSCI 230
Recursion Chapter 18.
Recursion When performing a repetitive task either: a loop recursion
Lesson #6 Modular Programming and Functions.
CSC 143 Recursion.
The structure of programming
The structure of programming
Main() { int fact; fact = Factorial(4); } main fact.
Thinking procedurally
ITEC324 Principle of CS III
Presentation transcript:

CSE / ENGR 142 Programming I Recursion © 1998 UW CSE 6/1/99

Chapter 10: Recursion 10.1 Nature of Recursion (skip “Mississippi” example 10.2) 10.2 Tracing: Read! 10.3 Recursive Math (but skip gcd example 10.6) 10.4 Skip 10.5 Skip 10.6 Towers of Hanoi: A Classic, but optional 10.7 Common Errors: Read! 6/1/99

Review: Function Calling int twice(int x) { return (2*x) } int main(void) { int x=3; x=twice(5); twice x main x Each time a function is called, memory is allocated to store value of local variables (including parameters) 6/1/99

What is Recursion? int foo(int x) { … y = foo(…); } Defn: A function is recursive if it calls itself int foo(int x) { … y = foo(…); } Questions: How can recursion possibly work? Why would I want to write a recursive function? 6/1/99

Program vs. Process Program = a set of instructions akin to a recipe akin to the blueprint for an information factory Process = activity performed by computer when obeying the instructions akin to the activity of cooking akin to operation of a working factory NO NEED FOR CEMENT OR STEEL Instead, just need to allocate memory for local variables!! CAN CREATE MULTIPLE FACTORIES AS NEEDED FROM THE SAME BLUEPRINTS!!! 6/1/99

Factorial Function Revisited function name int factorial ( int n ) { . . . function name int factorial ( int n ) { int product, i ; product = 1 ; for ( i = n ; i > 1 ; i = i - 1 ) { product = product * i ; } return (product) ; parameter local variables return type & value 6/1/99

Factorial via Recursion /* 0! = 1! = 1; for n > 1, n! = n(n-1)! */ int factorial(int n) { int t; if (n <= 1) t = 1; else t = n * factorial(n - 1); return t; } 0! is 1 1! is 1 n! is n * (n-1)!, for n>1 E.g.: 3! =3 * 2! = 3 * 2 * 1! = 3 * 2 * 1 6/1/99

Review: Function Basics Tracing recursive functions is no sweat if you remember the basics about functions: Parameters and variables declared in a function are local to it Allocated (created) on function entry. De-allocated (destroyed) on function return. Parameters are initialized by copying values of arguments when a function is called. 6/1/99

Factorial factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * 2 * 1 = 24 6/1/99

Factorial Trace n t n t n t n t 6/1/99

Insist on 'y' or 'n' char yes_or_no (void) { char answer = 'X'; while (answer != 'y' && answer != 'n') { printf ("Please enter 'y' or 'n':"); scanf (" %c", &answer); } return answer; 6/1/99

Insisting without Looping char yes_or_no (void) { char answer; printf ("Please enter 'y' or 'n':"); scanf (" %c", &answer); if (answer != 'y' && answer != 'n') answer = yes_or_no ( ); return answer; } 6/1/99

Iteration vs. Recursion Turns out any iterative algorithm can be reworked to use recursion instead (and vice versa). There are programming languages where recursion is the only choice(!) Some algorithms are more naturally written with recursion But naïve applications of recursion can be inefficient 6/1/99

When to use Recursion? Problem has one or more simple cases These have a straightforward nonrecursive solution Other cases can be redefined in terms of problems that are closer to simple cases By repeating this redefinition process one gets to one of the simple cases 6/1/99

Example: Path planning /* ‘F’ means finished! ‘X’ means blocked ‘ ’ means ok to move */ char maze[MAXX][MAXY]; int x =0, y=0; /* start in yellow */ Unless blocked, can move up, down, left, right Objective: determine if there is a path? 1 2 3 4 5 6 7 8 9 y F 0 1 2 3 4 5 6 7 x 6/1/99

Simple Cases Suppose at x,y If maze[x][y]==‘F’ If no place to go a 1 2 3 4 5 6 7 8 9 Suppose at x,y If maze[x][y]==‘F’ Then “yes!” If no place to go Then “no!” y a F 0 1 2 3 4 5 6 7 x 6/1/99

Redefining a hard problem to several simpler ones No need to go to same square twice 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 ... 1 2 3 4 5 6 7 8 9 or F F 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 F 0 1 2 3 4 5 6 7 ... 6/1/99 F 0 1 2 3 4 5 6 7

Helper function /* Returns true if <x,y> is a legal move given the maze, otherwise returns false */ int legal_mv (char m[MAXX ][MAXY], int x, int y) { return(x>=0 && x<=MAXX && y>=0 && y<= MAXY && m [x][y] != ‘X’); } 6/1/99

Elegant Solution /* Returns true if there is a path from <x,y> to an element of maze containing ‘F’ otherwise returns false */ int is_path(char m[MAXX][MAXY ], int x, int y) { if (m [x][y] == ‘F’) return(TRUE); else { m[x][y] = ‘X’; return((legal_mv(m,x+1,y) && is_path(m,x+1,y)) || (legal_mv(m,x-1,y) && is_path(m,x-1,y)) || (legal_mv(m,x,y-1) && is_path(m,x,y-1)) || (legal_mv(m,x,y+1) && is_path(m,x,y+1))) }} 6/1/99

Example is_path(maze, 7, 8) x y F int is_path(char m[MAXX][MAXY ], int x, int y) { if (m [x][y] == ‘F’) return(TRUE); else { m[x][y] = ‘X’; return((legal_mv(m,x+1,y) && is_path(m,x+1,y)) || (legal_mv(m,x-1,y) && is_path(m,x-1,y)) || (legal_mv(m,x,y-1) && is_path(m,x,y-1)) || (legal_mv(m,x,y+1) && is_path(m,x,y+1))) 1 2 3 4 5 6 7 8 9 F 0 1 2 3 4 5 6 7 6/1/99

Example Cont is_path(maze, 7, 7) x y F int is_path(char m[MAXX][MAXY ], int x, int y) { if (m [x][y] == ‘F’) return(TRUE); else { m[x][y] = ‘X’; return((legal_mv(m,x+1,y) && is_path(m,x+1,y)) || (legal_mv(m,x-1,y) && is_path(m,x-1,y)) || (legal_mv(m,x,y-1) && is_path(m,x,y-1)) || (legal_mv(m,x,y+1) && is_path(m,x,y+1))) 1 2 3 4 5 6 7 8 9 F 0 1 2 3 4 5 6 7 6/1/99

Example Cont is_path(maze, 7, 9) x y F int is_path(char m[MAXX][MAXY ], int x, int y) { if (m [x][y] == ‘F’) return(TRUE); else { m[x][y] = ‘X’; return((legal_mv(m,x+1,y) && is_path(m,x+1,y)) || (legal_mv(m,x-1,y) && is_path(m,x-1,y)) || (legal_mv(m,x,y-1) && is_path(m,x,y-1)) || (legal_mv(m,x,y+1) && is_path(m,x,y+1))) 1 2 3 4 5 6 7 8 9 F 0 1 2 3 4 5 6 7 6/1/99