0% found this document useful (0 votes)
54 views

More On Recursion

The document discusses recursion and provides examples of recursive functions and solutions. It introduces the Towers of Hanoi problem and provides a recursive C function to solve it. It also gives examples of running the Towers of Hanoi function for different numbers of disks. Additionally, it presents recursive functions to find the maximum value in an array and to calculate Fibonacci numbers, discussing inefficiency in the Fibonacci solution from recomputing subproblems.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

More On Recursion

The document discusses recursion and provides examples of recursive functions and solutions. It introduces the Towers of Hanoi problem and provides a recursive C function to solve it. It also gives examples of running the Towers of Hanoi function for different numbers of disks. Additionally, it presents recursive functions to find the maximum value in an array and to calculate Fibonacci numbers, discussing inefficiency in the Fibonacci solution from recomputing subproblems.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

More on Recursion

P. P. Chakrabarti

04-02-03

P.P.Chakrabarti, IIT Kharagpur

Tower of Hanoi

04-02-03

P.P.Chakrabarti, IIT Kharagpur

Tower of Hanoi

04-02-03

P.P.Chakrabarti, IIT Kharagpur

Tower of Hanoi

04-02-03

P.P.Chakrabarti, IIT Kharagpur

Tower of Hanoi

04-02-03

P.P.Chakrabarti, IIT Kharagpur

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 */ . . . }

04-02-03

P.P.Chakrabarti, IIT Kharagpur

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) ; . . }

04-02-03

P.P.Chakrabarti, IIT Kharagpur

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) ; . }

04-02-03

P.P.Chakrabarti, IIT Kharagpur

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) ; }

04-02-03

P.P.Chakrabarti, IIT Kharagpur

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

10

04-02-03

P.P.Chakrabarti, IIT Kharagpur

More TOH runs:


void towers(int n, char from, char to, char aux) { if (n==1) { printf ("Disk 1 : %c -> %c \n", from, to) ; return ; } towers (n-1, from, aux, to) ; printf ("Disk %d : %c -> %c\n", n, from, to) ; towers (n-1, aux, to, from) ; } main() { int n; scanf("%d", &n); towers(n,'A',C',B'); }
[ppchak]$ ./a.out 4 Disk 1 : A -> B Disk 2 : A -> C Disk 1 : B -> C Disk 3 : A -> B Disk 1 : C -> A Disk 2 : C -> B Disk 1 : A -> B Disk 4 : A -> C Disk 1 : B -> C Disk 2 : B -> A Disk 1 : C -> A Disk 3 : B -> C Disk 1 : A -> B Disk 2 : A -> C Disk 1 : B -> C

11

04-02-03

P.P.Chakrabarti, IIT Kharagpur

A recursive max function:


int max (int best,int n) { int data, result ; if (n > 0) scanf("%d", &data); else return best; if (data > best) result = max (data, n-1); else result = max(best, n-1); return result; } main() { int item, numb; scanf("%d", & numb); if (numb > 0) { scanf("%d", &item); printf(" answer = %d\n", max(item, numb-1)); }}

12

04-02-03

P.P.Chakrabarti, IIT Kharagpur

A Run:

Note value 0 is false. Will cause problems for negative numbers

int max (int best,int n) { int data, result ; [ppchak]$ ./a.out if (n) scanf("%d", &data); 5 else return best; 3 if (data > best) result = max (data, n-1); 7 else result = max(best, n-1); 4 return result; } 1 main() 6 { int item, numb; Answer = 7 scanf("%d", & numb); if (numb) { scanf("%d", &item); printf(" Answer = %d\n", max(item, numb-1)); }}

13

04-02-03

P.P.Chakrabarti, IIT Kharagpur

See the way best propagates:


int max (int best,int n) [ppchak]$ ./a.out { int data, result ; 5 printf("F: n = %d, best = %d \n", n, best); 3 if (n>0) scanf("%d", &data); F: n = 4, best = 3 else return best; 5 if (data > best) result = max (data, n-1); F: n = 3, best = 5 else result = max(best, n-1); 2 return result; F: n = 2, best = 5 } 7 main() F: n = 1, best = 7 { int item, numb; 1 scanf("%d", & numb); F: n = 0, best = 7 if (numb>0) Answer = 7 { scanf("%d", &item); printf(" Answer = %d\n", max(item, numb-1)); }}

14

04-02-03

P.P.Chakrabarti, IIT Kharagpur

int fib (int n) { if (n==0 || n==1) return 1; return fib(n-2) + fib(n-1) ; }

Relook at the Fibonacci solution: fib(n) = 1 if n =0 or 1; = fib(n 2) + fib(n 1) otherwise;

fib (5) fib (3) fib (1) fib (0) fib (2) fib (1) fib (2) fib (0) fib (4) fib (3) fib (2) fib (1)

fib (1) fib (1) fib (0)

This is not efficient !! Same sub-problem solved many times.

15

04-02-03

P.P.Chakrabarti, IIT Kharagpur

You might also like