1 / 66

Recursion

Recursion. Functions – reminder. return-type name( argType1 arg1 , argType2 arg2 , …) { function body; return value; }. A function can call other functions. Return causes the execution of the function to terminate and returns a value to the calling function.

Download Presentation

Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion

  2. Functions – reminder return-type name(argType1arg1,argType2arg2, …) { function body; return value; } • A function can call other functions. • Return causes the execution of the function to terminate and returns a value to the calling function. • The type of the value returned must be the same as the return-type defined for the function.

  3. A recursive definition • C functions can also call themselves! • However, usually not with the same parameters (why?) • Some functions can be defined using smaller occurrences of themselves. • Such a definition is called a “recursive definition” of a function.

  4. Recursive calling • Example: void func(int n){ putchar(`*`); func(n); } • Infinite series of * • What is the problem ? • Look for other problem …

  5. (n-1)! Factorial • By definition : n! = 1*2*3*… *(n-1)*n • Thus, we can also define factorial the following way: • 0! = 1 • n! = n*(n-1)! for n>0 *n

  6. Example - factorial intfactRec(int n){ if (n==0 || n==1) return 1; return n*factRec(n-1); } int factorial(int n){ int fact = 1; while (n >= 1){ fact *=n; n--; } return fact; }

  7. Conclusions for Recursive calling • Every recursive function has a “boundary condition”. The function stops calling itself when it is satisfied.

  8. Recursive factorial – step by step intfactRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns…

  9. Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… 4*…

  10. FactRec(4) FactRec(3) n n 4 3 Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }

  11. FactRec(4) FactRec(3) n n 4 3 Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }

  12. FactRec(4) FactRec(3) n n 4 3 Returns… Returns… 3*… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }

  13. FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }

  14. FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }

  15. FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… 2*… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }

  16. FactRec(4) FactRec(1) FactRec(3) FactRec(2) n n n n 2 1 3 4 Returns… Returns… Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }

  17. FactRec(4) FactRec(1) FactRec(3) FactRec(2) n n n n 2 1 3 4 Returns… Returns… Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }

  18. FactRec(4) FactRec(3) FactRec(2) FactRec(1) n n n n 2 3 4 1 Returns… Returns… Returns… Returns… 1 Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }

  19. FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… 2*1 Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }

  20. FactRec(4) FactRec(3) n n 4 3 Returns… Returns… 3*2 Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }

  21. FactRec(4) n 4 Returns… 4*6 Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }

  22. הדפסת מספרים- דוגמא 1 #include <stdio.h> void print1(int n){ if (n>=0){ printf("%d ",n); print1(n-1); { { void main(){ int i = 3; print1(i); putchar('\n'); { 3 2 1 0

  23. הדפסת מספרים- דוגמא 2 #include <stdio.h> void print2(int n){ if (n>=0){ print2(n-1); printf("%d ",n); { { void main(){ int i = 3; print2(i); putchar('\n'); { 0 1 2 3

  24. הדפסת מספרים- דוגמא 3 #include <stdio.h> void print3(int n){ if (n>=0){ printf("%d ",n); print3(n-1); printf("%d ",n); { { void main(){ int i = 3; print3(i); putchar('\n'); { 0 1 2 3 3 2 1 0

  25. הדפסת מספרים- דוגמא 4 #include <stdio.h> void print4(int n){ if (n>=0){ print4(n-1); printf("%d ",n); print4(n-1); { { void main(){ int i = 3; print4(i); putchar('\n'); { 0 0 1 1 0 0 3 0 0 1 1 0 0 2 2

  26. Another example - power • Xy= x*x*…*x • Recursive definitions (assume non-negative y): • Base: x0=1 • Xy= X*(Xy-1) • Xy=(Xy/2)2(for even y’s only) y times

  27. Fibonacci Series • Fibonacci definition: • n0 = 0 • n1 = 1 • nn = nn-1 + nn-2 0 1 1 2 3 5 8 13 21 34 55 …

  28. Fibonacci Iterative void fibonacci(int n) { int Fn, Fn1, Fn2, ind; Fn2 = 0 ; Fn1 = 1 ; Fn = 0 ; if ( n == 1 ) Fn = 1 ; for (ind=2 ; ind <= n ; ind++){ Fn = Fn1 + Fn2; Fn2 = Fn1; Fn1 = Fn; } printf("F(%d) = %d \n", n, Fn); }

  29. fibonacci(5) fibonacci(4) fibonacci(3) fibonacci(3) fibonacci(2) fibonacci(2) fibonacci(1) fibonacci(2) fibonacci(1) fibonacci(1) fibonacci(0) fibonacci(1) fibonacci(0) fibonacci(1) fibonacci(0) Fibonacci Recursive int fibonacci(int n) { if (n==0) return 0; if (n==1) return 1; return fibonacci(n-1) + fibonacci(n-2); }

  30. rec_pow(2, 5) x y 2 5 Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  31. rec_pow(2, 5) x y 2 5 Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  32. rec_pow(2, 5) x y 2 5 Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  33. rec_pow(2, 5) x y 2 5 Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  34. rec_pow(2, 5) x y 2 5 Returns… 2*… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  35. rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 5 4 Returns… Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  36. rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 5 4 Returns… Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  37. rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 5 4 Returns… Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  38. rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 5 4 Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  39. rec_pow(2, 5) rec_pow(2, 2) rec_pow(2, 4) x x x y y y 2 2 2 4 2 5 Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  40. rec_pow(2, 5) rec_pow(2, 2) rec_pow(2, 4) x x x y y y 2 2 2 4 2 5 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  41. rec_pow(2, 5) rec_pow(2, 2) rec_pow(2, 4) x x x y y y 2 2 2 4 2 5 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }

  42. rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 4 5 Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)

  43. rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 5 1 4 Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)

  44. rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 5 1 4 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)

  45. rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 5 1 4 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)

  46. rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 5 1 4 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)

  47. rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 1 5 4 Returns… Returns… Returns… 2*… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)

  48. rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) rec_pow(2, 0) x x x x y y y y 2 2 2 2 1 4 5 0 Returns… Returns… Returns… Returns… 2*… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)

  49. rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) rec_pow(2, 0) x x x x y y y y 2 2 2 2 1 4 5 0 Returns… Returns… Returns… Returns… 2*… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)

  50. rec_pow(2, 4) rec_pow(2, 5) rec_pow(2, 0) rec_pow(2, 1) x x x x y y y y 2 2 2 2 5 4 0 1 Returns… Returns… Returns… Returns… square(…) 1 2*… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)

More Related