160 likes | 352 Views
Recursion H&K Chapter 10. Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University. Recursive functions. A function that calls itself is said to be recursive. Here are examples of “famous” recursively defined functions in math: factorial and fibonacci. Base
E N D
Recursion H&K Chapter 10 Instructor – GokcenCilingir Cpt S 121 (July 21, 2011) Washington State University
Recursive functions • A function that calls itself is said to be recursive. Here are examples of “famous” recursively defined functions in math: factorial and fibonacci Base case/step Recursive case/step • The ability to invoke itself enables a recursive function to be repeated with different parameter values
C implementation to factorial function //iterative implementation int fact (int n) { int i, ans = 1; for(i = n; i > 1; i--) ans = ans * i; return ans; } //recursive implementation int fact (int n) { int ans; if (n == 0) ans = 1; else ans = n * fact(n-1); return ans; }
Recursion • Recursion is a method where the solution to a problem depends on solutions to smaller instances of the same problem. • A common computer programming tactic is to divide a problem into sub-problems of the same type as the original, solve those problems, and combine the results. fact fact(n-1) … fact(0) n fact(n) multiply all
Recursion (2) • Problems that may be solved using recursion have these attributes: • One or more simple cases have a straightforward, non-recursive solution • The other cases may be defined in terms of problems that are closer to the simple cases • Through a series of calls to the recursive function, the problem eventually is stated in terms of the simple cases image source: H&K, figure 10.1
Recursion (3) • The recursive algorithms we’re going to write will generally have the form: if this is a simple (base) case: solve it else redefine the problem using recursion
Example 1 • Write a function that performs multiplication through addition with the prototype: int multiply(int m, int n); • Let’s recursively define multiply: base case: multiply(m,1) is m recursive case: multiply(m, n) is n + multiply(m, n-1)
Example 1 (cont’d) Here is the recursive C implementation of mult: int multiply (int m, int n) { int ans; if (n == 1) ans= m; // base case else ans= n + multiply (m, n – 1); // recursive case return ans; }
Example 2 • Write a function to count the number of times a particular character appears in a string. Here is the function prototype: int count (char ch, const char str[]); • Let’s recursively define count: base case: If str has 0 length, then count(ch,str) is 0 recursive case: if str[0] is ch, then count(ch,str) is 1+ count(ch, &str[1]) else, count(ch,str) is count(ch, &str[1])
Example 2 (cont’d) • Figure below illustrates a common thinking strategy while designing a recursive solution to a problem: image source: H&K, figure 10.3
Example 2 (cont’d) Here is the recursive C implementation of mult: int count (char ch, const char str[]) { int ans; if (strlen(str) == 0) //base case ans = 0; else //recursive case { if (ch == str[0]) ans = 1 + count(ch, &str[1]); else ans = count(ch, &str[1]); } return ans; }
Tracing a recursive function image source: H&K, figure 10.5
Example 3 • Develop a program in C to count pixels(picture elements) belonging to an object in a photograph. The data are in a two-dimensional grid of cells, each of which may be empty (value 0) or filled (value 1). The filled cells that are connected form a blob (an object). • Write a function blob_check that takes as parameters the grid and x-y coordinates of a cell and returns as its value the number of cells in the blob to which the indicated cell belongs. image source: H&K, figure 10.27
Example 3 (cont’d) • Prototype of blob_check : int blob_check(int grid[][SIZE], int x, int y); • Let’s recursively define blob_check: Base case If the cell (x,y) is not on the grid, or the cell (x,y) is empty, then blob_check returns 0. Recursive case If the cell is on the grid and filled, then blob_check returns: 1 + blob_check (grid, x+1, y) + blob_check (grid, x-1, y) + blob_check (grid, x, y+1) + blob_check (grid, x, y-1) + blob_check (grid, x+1, y-1) + blob_check (grid, x+1, y+1) + blob_check (grid, x-1, y-1) + blob_check (grid, x-1, y+1) • NOTE: To avoid counting a filled cell more than once, mark a cell as empty once you have counted it
Example 3 (cont’d) int blob_check(int grid[][SIZE], int x, int y) { int ans, i, j, newX, newY; int adjustment[3] = {-1, 0, 1}; if (x < 0 || x > SIZE || y < 0 || y > SIZE || grid[x][y] == 0) ans = 0; else { ans = 1; grid[x][y] = 0; /*mark it as zero not to count it twice later*/ for(i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { newX = x + adjustment[i]; newY = y + adjustment[j]; ans = ans + blob_check(grid,newX,newY); } } } return ans; }
References • J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6th Ed.), Addison-Wesley, 2010 • P.J. Deitel & H.M. Deitel, C How to Program (5th Ed.), Pearson Education , Inc., 2007.