recursion
play

Recursion 1 Recursion n A process by which a function calls itself - PowerPoint PPT Presentation

Recursion 1 Recursion n A process by which a function calls itself repeatedly Either directly. n X calls X Or cyclically in a chain. n X calls Y, and Y calls X n Used for repetitive computations in which each action is stated in terms of a


  1. Recursion 1

  2. Recursion n A process by which a function calls itself repeatedly ¨ Either directly. n X calls X ¨ Or cyclically in a chain. n X calls Y, and Y calls X n Used for repetitive computations in which each action is stated in terms of a previous result fact(n) = n * fact (n-1) 2

  3. Contd. n For a problem to be written in recursive form, two conditions are to be satisfied: ¨ It should be possible to express the problem in recursive form n Solution of the problem in terms of solution of the same problem on smaller sized data ¨ The problem statement must include a stopping condition Stopping condition fact(n) = 1, if n = 0 = n * fact(n-1), if n > 0 Recursive definition 3

  4. n Examples: ¨ Factorial: fact(0) = 1 fact(n) = n * fact(n-1), if n > 0 ¨ GCD: gcd (m, m) = m gcd (m, n) = gcd (m%n, n), if m > n gcd (m, n) = gcd (n, n%m), if m < n ¨ Fibonacci series (1,1,2,3,5,8,13,21,….) fib (0) = 1 fib (1) = 1 fib (n) = fib (n-1) + fib (n-2), if n > 1 4

  5. Factorial long int fact (int n) { if (n == 1) return (1); else return (n * fact(n-1)); } 5

  6. Factorial Execution long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } 6

  7. Factorial Execution fact(4) long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } 7

  8. Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } 8

  9. Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } 9

  10. Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } 10

  11. Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); long int fact (int n) { if (1 = = 1) return (1); if (n = = 1) return (1); else return (n * fact(n-1)); } 11

  12. Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); 1 long int fact (int n) { if (1 = = 1) return (1); if (n = = 1) return (1); else return (n * fact(n-1)); } 12

  13. Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); 2 if (2 = = 1) return (1); else return (2 * fact(1)); 1 long int fact (int n) { if (1 = = 1) return (1); if (n = = 1) return (1); else return (n * fact(n-1)); } 13

  14. Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); 2 if (2 = = 1) return (1); else return (2 * fact(1)); 1 long int fact (int n) { if (1 = = 1) return (1); if (n = = 1) return (1); else return (n * fact(n-1)); } 14

  15. Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); 6 if (3 = = 1) return (1); else return (3 * fact(2)); 2 if (2 = = 1) return (1); else return (2 * fact(1)); 1 long int fact (int n) { if (1 = = 1) return (1); if (n = = 1) return (1); else return (n * fact(n-1)); } 15

  16. Factorial Execution fact(4) 24 if (4 = = 1) return (1); else return (4 * fact(3)); 6 if (3 = = 1) return (1); else return (3 * fact(2)); 2 if (2 = = 1) return (1); else return (2 * fact(1)); 1 long int fact (int n) { if (1 = = 1) return (1); if (n = = 1) return (1); else return (n * fact(n-1)); } 16

  17. Look at the variable addresses (a slightly different program) ! void main() { Output int x,y; 4 scanf("%d",&x); F: data = 4, &data = 3221224528 y = fact(x); &val = 3221224516 printf ("M: x= %d, y = %d\n", x,y); F: data = 3, &data = 3221224480 } &val = 3221224468 int fact(int data) F: data = 2, &data = 3221224432 { int val = 1; &val = 3221224420 printf("F: data = %d, &data = %u \n F: data = 1, &data = 3221224384 &val = %u\n", data, &data, &val); &val = 3221224372 if (data>1) val = data*fact(data-1); return val; M: x= 4, y = 24 } 17

  18. Fibonacci Numbers Fibonacci recurrence: fib(n) = 1 if n = 0 or 1; = fib(n – 2) + fib(n – 1) otherwise; int fib (int n){ if (n == 0 or n == 1) return 1; [BASE] return fib(n-2) + fib(n-1) ; [Recursive] } 18

  19. int fib (int n) { Fibonacci recurrence: if (n == 0 || n == 1) return 1; fib(n) = 1 if n = 0 or 1; return fib(n-2) + fib(n-1) ; = fib(n – 2) + fib(n – 1) } otherwise; fib (5) fib (3) fib (4) fib (1) fib (2) fib (2) fib (3) fib (0) fib (1) fib (1) fib (2) fib (0) fib (1) fib (1) fib (0) 19

  20. int fib (int n) { Fibonacci recurrence: if (n == 0 || n == 1) return 1; fib(n) = 1 if n = 0 or 1; return fib(n-2) + fib(n-1) ; = fib(n – 2) + fib(n – 1) } otherwise; fib (5) fib (3) fib (4) fib (1) fib (2) fib (2) fib (3) 1 fib (0) fib (1) fib (1) fib (2) fib (0) fib (1) 1 1 1 1 1 fib (1) fib (0) 1 1 20 fib.c

  21. int fib (int n) { Fibonacci recurrence: if (n==0 || n==1) return 1; fib(n) = 1 if n = 0 or 1; return fib(n-2) + fib(n-1) ; = fib(n – 2) + fib(n – 1) } otherwise; 8 fib (5) 3 5 fib (3) fib (4) 2 3 1 2 fib (1) fib (2) fib (2) fib (3) 1 2 1 1 1 1 1 fib (0) fib (1) fib (1) fib (2) fib (0) fib (1) 1 1 1 1 1 1 1 fib (1) fib (0) 1 1 21

  22. Sum of Squares int sumSquares (int m, int n) { int middle ; if (m == n) return m*m; else { middle = (m+n)/2; return sumSquares(m,middle) + sumSquares(middle+1,n); } } 22

  23. Annotated Call Tree 355 sumSquares(5,10) sumSquares(5,10) 245 110 sumSquares(5,10) sumSquares(8,10) sumSquares(5,7) 100 49 145 61 sumSquares(10,10) sumSquares(8,9) sumSquares(7,7) sumSquares(5,6) 36 81 25 64 sumSquares(9,9) sumSquares(6,6) sumSquares(8,8) sumSquares(5,5) 49 100 36 81 25 64 23

  24. Towers of Hanoi Problem 1 2 3 4 5 LEFT CENTER RIGHT 24

  25. n Initially all the disks are stacked on the LEFT pole n Required to transfer all the disks to the RIGHT pole ¨ Only one disk can be moved at a time. ¨ A larger disk cannot be placed on a smaller disk n CENTER pole is used for temporary storage of disks 25

  26. n Recursive statement of the general problem of n disks ¨ Step 1: n Move the top (n-1) disks from LEFT to CENTER ¨ Step 2: n Move the largest disk from LEFT to RIGHT ¨ Step 3: n Move the (n-1) disks from CENTER to RIGHT 26

  27. Tower of Hanoi A B C 27

  28. Tower of Hanoi A B C 28

  29. Tower of Hanoi A B C 29

  30. Tower of Hanoi A B C 30

  31. Towers of Hanoi function void towers (int n, char from, char to, char aux) { /* Base Condition */ if (n==1) { printf (“Disk 1 : %c à &c \n”, from, to) ; return ; } /* Recursive Condition */ towers (n-1, from, aux, to) ; ……………………. ……………………. } 31

  32. Towers of Hanoi function void towers (int n, char from, char to, char aux) { /* Base Condition */ if (n==1) { printf (“Disk 1 : %c à &c \n”, from, to) ; return ; } /* Recursive Condition */ towers (n-1, from, aux, to) ; printf (“Disk %d : %c à %c\n”, n, from, to) ; ……………………. } 32

  33. Towers of Hanoi function void towers (int n, char from, char to, char aux) { /* Base Condition */ if (n==1) { printf (“Disk 1 : %c à %c \n”, from, to) ; return ; } /* Recursive Condition */ towers (n-1, from, aux, to) ; printf (“Disk %d : %c à %c\n”, n, from, to) ; towers (n-1, aux, to, from) ; } 33

  34. TOH runs void towers(int n, char from, char to, char aux) { if (n==1) Output { printf ("Disk 1 : %c -> %c \n", from, to) ; 3 return ; Disk 1 : A -> C } Disk 2 : A -> B towers (n-1, from, aux, to) ; Disk 1 : C -> B printf ("Disk %d : %c -> %c\n", n, from, to) ; Disk 3 : A -> C towers (n-1, aux, to, from) ; Disk 1 : B -> A } Disk 2 : B -> C void main() Disk 1 : A -> C { int n; scanf("%d", &n); towers(n,'A',‘C',‘B'); } 34

  35. More TOH runs Output 4 Disk 1 : A -> B void towers(int n, char from, char to, char aux) Disk 2 : A -> C { if (n==1) Disk 1 : B -> C { printf ("Disk 1 : %c -> %c \n", from, to) ; Disk 3 : A -> B return ; Disk 1 : C -> A } towers (n-1, from, aux, to) ; Disk 2 : C -> B printf ("Disk %d : %c -> %c\n", n, from, to) ; Disk 1 : A -> B towers (n-1, aux, to, from) ; Disk 4 : A -> C } Disk 1 : B -> C void main() Disk 2 : B -> A { int n; Disk 1 : C -> A scanf("%d", &n); Disk 3 : B -> C towers(n,'A',‘C',‘B'); Disk 1 : A -> B } Disk 2 : A -> C Disk 1 : B -> C 35

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend