Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Recursion: Examples, Nature, and Applications, Study notes of Computer Science

An introduction to recursion, a powerful problem-solving and programming tool. It covers simple examples like countdown timers and multiplication, mathematical definitions, and the nature of recursion. The document also includes examples of recursive functions for a palindrome check and fibonacci sequence, as well as an explanation of recursion in the context of the towers of hanoi problem.

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-zml
koofers-user-zml 🇺🇸

10 documents

1 / 20

Toggle sidebar

Related documents


Partial preview of the text

Download Understanding Recursion: Examples, Nature, and Applications and more Study notes Computer Science in PDF only on Docsity! Introduction  Recursion is a function invoking itself, either directly or indirectly.  Many algorithms and mathematical properties are naturally expressed in a recursive manner.  It can be used as an alternative to iteration.  Recursion is an important and powerful tool in problem solving and programming. It is a programming technique that naturally implements the divide-and-conquer problem solving methodology.  Some programming languages are inherently recursive (Lisp, Scheme). In its simplest form the idea of recursion is straightforward: Example 1: Simple countdown timer Recursion - 1 Recursion void count_down(int n) { if (n <= 0) printf(“\nBlast off.\n”); else{ printf(“%d! “, n); count_down(n-1); } } int main () { count_down(10); } Example 2: Multiplication of natural numbers. a * b = a added to itself b times. (iterative definition) a * b = a if b=1 (recursive definition) a* b = a * (b-1) + a if b > 1 e.g.  6 * 3 = 6 * 2 + 6 = 6 * 1 + 6 + 6 = 6 + 6 + 6 = 18 Example 3: Factorial Function n! = n * (n-1) * (n-2) * … * 2 * 1 5! = 5 * 4 * 3* 2 * 1 0! = 1 Recursion - 2 int multiply(int a, int b) { if (b == 1) /* stopping case */ return a; else /* recursive step */ return ( a + multiply(a, b-1)); } Example 1: Tracing the function multiply x = multiply(6,3): Recursion - 5 a = 6 b = 3 3 <= 1? false return (6 + multiply(6,2)) a = 6 b = 2 2 <= 1? false return(6 + multiply(6,1)) a = 6 b = 1 1 <= 1? true return(6) Example: Tracing a Recursive Function A palindrome is a string of characters that reads the same backwards and forwards (e.g. level, deed, mom) palindrome(5) reads 5 characters and prints them in reverse order. Recursion - 6 void palindrome(int n) { char next; if (n == 1) { /* stopping case */ scanf("%c",&next); printf("%c", next); } else { scanf("%c", &next); palindrome(n-1); printf("%c",next); } return; } int main() { printf("Enter a string: "); palindrome(5); printf("\n"); } Example 3: Trace of palindrome: for input abc palindrome(3); Recursion - 7 n=3 3 <= 1? false read next : a palindrome(2) write a return n=2 2 <= 1? false read next : b palindrome(1) write b return n=1 1 <= 1? true read next : c write c return Proof by Induction Induction proofs are most often used to establish theorems that hold for positive integers. That is, you can establish the validity of a conjecture such as: n  0, and n  integer numbers, it is true that    n 0i i 2 )1n(n  by showing that the conjecture is true for one or more base values of n (generally, n = 0 is sufficient – though not always). Once this base case is proven true, you assume that the conjecture is true for all values of n from the base case through m, where m is an arbitrary integer greater than or equal to the largest value of n covered in the base case; this is the inductive hypothesis. Finally, using the assumption of the inductive hypothesis you prove that the conjecture is true for the next value of n (i.e., m + 1). This final step is the induction step. To summarize: An induction proof consists of three parts: 1. Induction base: Show by example that the conjecture is true for one or more values of n. Typically n = 0 is used. 2. Induction Hypothesis: Assume the conjecture is true for all values of n between the base case value and some arbitrary integer m. 3. Induction Step: Prove that the conjecture is true (or possibly not true) when n = m +1. Example: Prove by induction the conjecture from above. Conjecture: n  0, and n  integer numbers, it is true that    n 1i i 2 )1n(n  Basis: n=1, by definition    n 1i i    1 1i i 1, by substitution    1 1i i 2 )11(1  = 2 2 = 1 So the base case is true! Recursion - 10 Inductive Hypothesis: n=k, assume    n 1i i 2 )1k(k  is true. Inductive Step: prove conjecture is true for n = k+1 Must prove that:     1k 1i i 2 ]1)1k)[(1k(  = 2 )2k)(1k(  Note that     1k 1i i   k 1i i + (k +1) = 2 )1k(k  + (k + 1) Rewriting gives: 2 kk2  + k + 1 = 2 k2 + 2 k + 2 k2 + 2 2 2 k2 + 2 k + 2 k2 + 2 2 = 2 2k3k 2  = 2 )2k)(1k(  Thus the proof is completed and our conjecture is true for all integer numbers. At first glance, a proof by induction appears to be a circular proof, in that you establish a result by assuming that it is correct. However, an induction proof is not a circular proof for the same reason that a recursive definition is not circular. A correct proof by induction has an induction base that is similar to the base case of a recursive definition. The induction step proves the correctness using the correctness for smaller values of n. Repeated application of the induction step reduces the proof to one that is solely in terms of the base. Common Errors with Recursion Recursion - 11  It may not terminate if the stopping case is not correct or is incomplete (stack overflow: run-time error)  Make sure that each recursive step leads to a situation that is closer to a stopping case. Comparison of Iteration and Recursion  In general, an iterative version of a program will execute more efficiently in terms of time and space than a recursive version. This is because the overhead involved in entering and exiting a function is avoided in iterative version.  However a recursive solution can be sometimes the most natural and logical way of solving a problem.  Conflict: machine efficiency versus programmer efficiency  It is always true that recursion can be replaced with iteration and a stack. Recursion - 12 Problem: Solve the Towers of Hanoi for N disks. Analysis: Solution consists of a printed list of individual disk moves. We need recursion that can be used to move any number of disks from one tower to another, using the third tower as a temporary tower. Inputs: n: integer, start:’A’, ‘B’, or ‘C’, finish:’A’, ‘B’, or ‘C’ temp:’A’, ‘B’, or ‘C’ Output: a list of individual disk moves. Algorithm C Version void tower(int n, char start, char finish, char temp) { if (n == 1) printf(“Move from %c to %c\n”, start, finish); else { tower(n-1, start, temp, finish); printf(“Move from %c to %c \n”, start, finish); tower(n-1, temp, finish, start); } } Recursion - 15 if (n == 1) /* stopping case */ move a single disk from start to finish else -Move n-1 disks from start to temp using finish as temporary tower. - Move a single disk from start to finish - Move n-1 disks from temp to finish using start as temporary tower Test: tower(3,‘A’,’C’, ’B’); Output: Move from A to C Move from A to B Move from C to B Move from A to C Move from B to A Move from B to C Move from A to C Practice Problems 1) Trace the following recursive function: Recursion - 16 #include <stdio.h> int f(char *s) { if (*s == '\0') return 0; else return (1 + f(s+1)); } int main() { char a[20] = "Computer Science I"; printf("%d\n",f(a)); } 2) Trace the following recursive function: Recursion - 17 #include <stdio.h> int f(int c) { if (!(c > 10)) { printf("%d\n", c); f(c + 1); } } int main() { f(0); }
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved