25-2 Recursive Functions Related Chapter: ABC 5.14, 5.15.

Slides:



Advertisements
Similar presentations
The Towers of Hanoi or Apocalypse When?.
Advertisements

Introduction to Algorithms
More Recursion: Permutations and Towers of Hanoi COP 3502.
Factorial Recursion stack Binary Search Towers of Hanoi
The Tower of Hanoi Ben Epstein Special Topics 2003.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Review of Recursion. Definition: Recursion - The process of a function calling itself.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © Cengage Learning. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
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.
Recursion Jordi Cortadella Department of Computer Science.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
 STACK STACK  STACK OPERATIONS STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  USES OF STACK USES OF STACK  THE TOWER.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
UNIT 17 Recursion: Towers of Hanoi.
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.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
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.
Recursion.
Recursion.
Recursion.
Chapter 4 Stacks
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Java Software Structures: John Lewis & Joseph Chase
CSCE 3110 Data Structures & Algorithm Analysis
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 12 Recursion (methods calling themselves)
Recursion When performing a repetitive task either: a loop recursion
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

25-2 Recursive Functions Related Chapter: ABC 5.14, 5.15

25-3 Recursive functions are defined in terms of themselves; i.e., a function is recursive if it contains calls back to itself or to another function that calls the original function. Recursive programming usually requires more memory and runs more slowly than non-recursive programs. This is due to the cost of implementing the “stack”. However, recursion is often a natural way to define a problem.

Problem Definition Write a factorial function. 0! = 1 and n! = n*(n-1)!. Use “recursion” to implement the factorial function. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = Non-negative integers as input. Output= return the factorial of the input value. Note: that 69! = x so our function will only work for small integer values. It would be better to return a value of data-type double (Why?)

25-5 double fact(int n) { if (n = =0) /* Voom! */ return 1.0; else return (n * fact(n - 1)); /* recursive call */ } In main we can call the factorial function by the following command: printf("%lf", fact(3)); or by (if x is of data-type int and y is of data-type double) : x = 3; y = fact(x);

(push) return ( 3 * fact(2)); (n = 3) (push) return ( 2 * fact(1)); (n = 2) (push) return ( 1 * fact(0)); (n = 1) (push/pop) return 1.0; (n = 0) (pop) return ( 1 * 1.0); (n = 1) (pop) return ( 2 * 1.0); (n = 2) (pop) return ( 3 * 2.0); (n = 3) return 1.0; (n = 0) return ( 1 * fact(0)); (n = 1) return ( 2 * fact(1)); (n = 2) return ( 3 * fact(2)); (n = 3) (STACK) return ( 1 * 1.0); (n = 1) return ( 2 * fact(1)); (n = 2) return ( 3 * fact(2)); (n = 3) return ( 2 * 1.0); (n = 2) return ( 3 * fact(2)); (n = 3) return ( 3 * 2.0); (n = 3) printf("%lf", fact(3) );

Problem Definition Write a solve_maze function. Read the maze from a file, “maze.txt” and display one path that traverses the maze. Use “recursion” to implement the maze function. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = The file “maze.txt” contains the following ********** ** **** * *O* * * *** * * *X* * ** *** * * * * * *** ** * * * * ********** Your program will have to find its way through a 10x10 maze where the symbols “ * ”, “O” and “X” denote: “ * ” are solid walls through which you cannot travel "O" denotes the starting position, "X" is the exit for which you are looking for

********** ** ******O* ***** ***X* ******* *** ******* *** **********

Refine, Generalize, Decompose the problem definition Input (continued): Read the maze into the 2D array, ********** ** **** * *O* * * *** * * *X* * ** *** * * * * * *** ** * * * * ********** char maze[NUMROWS][NUMCOLS]; where NUMROWS and NUMCOLS are constants with value 10. The upper left-hand corner of the maze has the value maze[0][0]. If we want to test whether the cell in the fourth row and fourth column contains a wall then it's enough to use an if statement like this: if (maze[3][3] == ‘ * ')

Refine, Generalize, Decompose the problem definition Output : Display a solution as follows: ********** * OOOOO* ****O* *O* * *O* *** * *O* *X* * **O***O* * OO *O* * ***O**O* * * OOOO* **********

Develop Algorithm (processing steps to solve problem) Step 1 Read in the “maze” and find the starting Row and Column (the position of the “O”). Use variables “curRow” and “curCol” to keep track of the current position as we traverse through the maze (the 2D matrix “maze”). Step 2 Display the maze. Step 3 Check to see if current position is an “X” then we are done. Otherwise first try to go up and if not then down and if not then left and if not then right and if you can go up/down/left/right then go back to Step 2. Otherwise, go back to the previous position [curRow,curCol] and try another direction.

#include #define NUMROWS 10 #define NUMCOLS 10 /* prototypes */ int read_maze (char[][], int *, int *); void display_maze (char[][]); void solve_maze (char[][], int, int);

void main (void) { int startRow, startCol; /* Starting point in maze. */ char maze[NUMROWS][NUMCOLS]; /* Stores maze read from input file. */ if (read_maze(maze, &startRow, &startCol) == 0) { printf ("Error reading maze from file maze.txt!\n"); return; } solve_maze(maze, startRow, startCol); } /*end of main */

void solve_maze(char maze[][NUMCOLS], int curRow, int curCol) { int i; display_maze(maze); /* Check if solution found. */ if ((maze[curRow - 1][curCol] == 'X') || (maze[curRow + 1][curCol] == 'X') || (maze[curRow][curCol + 1] == 'X') || (maze[curRow][curCol - 1] == 'X')) exit (0); /* Recurse in each possible direction that is empty. */ /* Move up */ if (maze[curRow - 1][curCol] == ' ') { maze[curRow - 1][curCol] = 'O'; solve_maze(maze, curRow - 1, curCol); maze[curRow - 1][curCol] = ' '; } /* continued on next slide */

/* Move down */ if (maze[curRow + 1][curCol] == ' ') { maze[curRow + 1][curCol] = 'O'; solve_maze (maze, curRow + 1, curCol); maze[curRow + 1][curCol] = ' '; } /* Move left */ if (maze[curRow][curCol - 1] == ' ') { maze[curRow][curCol - 1] = 'O'; solve_maze (maze, curRow, curCol - 1); maze[curRow][curCol - 1] = ' '; } /* Move right */ if (maze[curRow][curCol + 1] == ' ') { maze[curRow][curCol + 1] = 'O'; solve_maze (maze, curRow, curCol + 1); maze[curRow][curCol + 1] = ' '; } usleep (600000); return; }

/* Display the maze passed as a parameter to standard output. */ void display_maze (char maze[ ][NUMCOLS]) { int i, row, col; for (row = 0; row < NUMROWS; row++) { for (col = 0; col < NUMCOLS; col++) { printf ("%c", maze[row][col]); } printf ("\n"); } usleep (600000); printf ("\n"); }

int read_maze (char maze[ ][NUMCOLS], int *sRow, int *sCol) { FILE *fpMaze; int row, col; char endofline; /* end of line character */ /* Open maze text file, make sure it opens OK. */ if ((fpMaze = fopen ("maze.txt", "r")) == NULL) return 0; for (row = 0; row < NUMROWS; row++) /* Loop through the rows. */ { for(col=0;col<NUMCOLS;++col) /* Loop through columns */ { fscanf(fpMaze,"%c",&maze[row][col]); if (maze[row][col] == 'O') /*Check if this is the starting position.*/ { *sRow = row; *sCol = col; } } /* end of for(col=... loop */ fscanf(fpMaze,"%c",&endofline); } /* end of for(row=... loop */ fclose(fpMaze); return 1; }

25-18 According to legend, in the great temple of Benares, beneath the dome which marks the center of the world, rests a brass plate on which are fixed three diamond needles. On one of these needles at creation, there were placed 64 discs of pure gold, the largest disc resting on the brass plate and the others getting smaller up to the top one. This is the TOWERS OF HANOI. Day and night, the people on duty move the discs from one needle to another, according to the two following laws: Law 1: Only one disc at a time may be moved. Law 2: A larger disc may never rest on a smaller disc. The workers labor in the belief that once the tower has been transferred to another needle there will be heaven on earth, so they want to complete the task in the least number of moves.

25-19 Actually, the Tower of Hanoi puzzle was invented in 1883 by the French mathematician Edouard Lucas ( ), who made up the legend to accompany it.

25-20 An elegant and efficient way to solve this problem is to think recursively. Suppose that you, somehow or other, have found the most efficient way possible to transfer a tower of n-1 disks one by one from one pole to another obeying the restriction that you never place a larger disk on top of a smaller one. Then, what is the most efficient way to move a tower of n disks from one pole to another?

25-21 Assume we know how to move n-1 disks from one peg to another.Then can we move n disks from peg 1 to peg 3 ? 1. Move n-1 disks from peg 1 to peg 2, peg 3 is just a temporary holding area 2. Move the last disk(the largest) from peg 1 to peg 3 3. Move the n-1 disks from peg 2 to peg 3, peg 1 is just a temporary holding area n disks

25-22 Click on Picture to start game 1 2 3

25-23 /* function prototype*/ void hanoi( int origin, int dest, int spare, int how_many); void main(void) { int how_many; printf("\n\tHow many disks initially on peg1? "); scanf(“%i”, &how_many); hanoi(1, 3, 2, how_many); }

25-24 void hanoi( int origin, int dest, int spare, int how_many) { if(how_many == 1) { printf(“\n\n\tMove top disk from peg %i to peg %i.”, origin, dest); return; } hanoi(origin, spare, dest, how_many - 1); printf("\n\n\t Move top disk from peg %i, to peg %i.\n ", origin, dest); hanoi(spare, dest, origin, how_many - 1) ; }

25-25 Going back to the legend, suppose the workers rapidly move one disk every second. As shown earlier, the minimum sequence of moves must be : The minimum number of moves needed to transfer a tower of n disks from peg1 to peg3 The minimum number of moves needed to transfer n-1 disks from peg1 to peg2 The minimum number of moves needed to transfer the n th disk from peg1 to peg3 The minimum number of moves needed to transfer n-1 disks from peg2 to peg3 on top of the n th disk Therefore, the recurrence relation is moves(n) = 2*moves(n-1) + 1 and initial case is moves(1) = 1 second. For example, moves(2) = 2*moves(1) + 1 = 3, moves(3) = 2*moves(2) + 1 = 7, or in general, moves(n) = 2 n -1 Then, the time to move all 64 disks from one peg to the other, and end the universe would be moves(64) seconds or billion years!! = + +

25-26 Since the executable(program) for a function is located in memory it is logical that the value of function name is an address, that is, function names are pointers. Similarly we can define a pointer to a function in C.

Problem Definition Write a function named trapz that computes the definite integral using trapezoids. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = the trapz has five parameters, first: the function (an address), second: the lower limit of integration, third: the upper limit of integration. fourth: the relative tolerance (error) fifth: the largest length of a sub-interval

Design---Develop Algorithm We will use the formula for the area of a trapezoid: area = ((b-a)/2)*(f(b)+f(a)) (area under solid red line) ab f(a) y=f(x) f(b)

Design---Develop Algorithm We will use recursion. The algorithm: Find the midpoint between a and b and sum the area of the two trapezoids. If the difference between the area of the original (large) trapezoid minus the sum of the area of the two trapezoids is less than some specified tolerance then we are done. (Voom!) Else, call the trapz function for each of the two smaller trapezoids and sum the area of these two. amidpointb y=f(x)

25-30 double trapz(double (*f)(double), double a, double b, double tol, double h) { double midpoint = (a+b)/2; double delta = (b-a)/2; double area = delta * (f(a)+f(b)); double area_left_half = delta * (f(a)+f(midpoint))/2; double area_right_half = delta * (f(midpoint)+f(b))/2; if (fabs(area-(area_left_half+area_right_half)) < tol && (b-a) < h ) return area_left_half + area_right_half; /* Voom!!! */ else return trapz(f,a,(a+b)/2,tol) + trapz(f,(a+b)/2,b,tol); } In main we can call the trapz function by the following command: printf("\n %lf \n", trapz(sin,0, /2, 1.0e-12,.1));

25-31 double (*f)(double); We defined a pointer named f, Note how this mirrors the prototype for sin, double sin(double);