Recursion H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Recursion Problem Solving & Program Design in C Sixth Edition.
Advertisements

Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Recursion Problem Solving and Program Design in C 5th Edition.
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.
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++
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 9: Recursion Problem Solving & Program Design in C Seventh.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 15 Recursion.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
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.
Recursion CENG 707.
Recursion CENG 707.
Topic 6 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng.
Recursion: The Mirrors
Recursion Chapter 12.
Chapter 10 Recursion.
Recursion Output Input
Recursion: The Mirrors
Programming application CC213
Chapter 10: Recursion Problem Solving and Program Design in C 5th Edition by Jeri R. Hanly and Elliot B. Koffman.
Recursion: The Mirrors
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

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 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(n-1) … fact(0) multiply all fact nfact(n)

C. Hundhausen, A. O’Fallon5 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 SIZE || 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; }

16 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.