0% found this document useful (0 votes)
4 views15 pages

L16 Recursion

The document outlines the key concepts of recursion, including the need for a recursive definition, reduction of problem size, and stopping criteria. It provides examples of recursive solutions for printing series, calculating factorials, and finding the maximum value in an array. Additionally, it discusses the importance of parameters and local/global variables in recursion, along with practice problems for further exploration.

Uploaded by

datapaw737
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

L16 Recursion

The document outlines the key concepts of recursion, including the need for a recursive definition, reduction of problem size, and stopping criteria. It provides examples of recursive solutions for printing series, calculating factorials, and finding the maximum value in an array. Additionally, it discusses the importance of parameters and local/global variables in recursion, along with practice problems for further exploration.

Uploaded by

datapaw737
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Recursion Concepts

Recursion : Three Main Things


• For solving any problem recursively:
1. The solution of the problem must be defined recursively:
e.g. fact of n = n * fact of n-1
sum of arr of n numbers = arr[n-1]+ sum of array of n-1 numbers
Tower of Hanoi problem:
2. In the recursive call, the size of the problem must reduce
e/g/ all D&C problems (binary search, merge sort, quick sort)
3. There must be a non-recursive definition of the solution for
some boundary which is called stopping criteria
e.g. fact (n) = n*fact (n-1) if n>=2
= 1 otherwise

2
3
Recursive Solutions Design
• Q1: Print series n ..1
• Find the recursive defn as per above three rules
• printseries(n) = do nothing if n <=0
print one value and recursively call for
reduced size if n>0
= cout <<n;
printseries(n-1);
void printseries1(int n)
{ if (n<=0) return;
else{ printf("%d ", n);
printseries1(n-1);} }
4
Recursive Solutions Design
• Q1: Print series 1..n
• Find the recursive defn as per above three rules
• printseries(n) = do nothing if n <=0
print one value and recursively call for
reduced size if n>0
= cout <<n;??
printseries(n-1);
void printseries2(int n)
{ if (n<=0) return;
else{ printseries1(n-1);
printf("%d ", n); } }
5
How recursion works internally
For ex, factorial calculated recursively:
int fact(int n)
{ int x; if (n>1) {x=fact(n-1); x= n*x; return x;}
else return 1;
}
• If we call fact(4), fact(4)’s execution will be stopped at
statement x=fact(3), and a new call to fact (3) will start;
• Similarly, now fact(3) will be stopped and fact(2) called
• fact(2) starts and stopped, and fact(1) is called
• Now n is 1, condition false, and return 1 executed (no
further recursion)
6
Recursive Problems
• Find recursively sum of series 1..n
• Find recursive sum of series 1+1/2+1/4..1/2^n
• Find recursively max out of an array of n numbers
*Using recursion over n-1 array and one value:
arrmax(n) = maximum of [nth] value and arrmax(n-1)
OR maximum of [1] and arrmax(2..n)
//parameters need change for recursive implementation
*Using D&C, like binary search
arrmax(1..n) =
max (a[mid], arrmax(1..mid-1), arrmax(mid+1..n))

7
8
Recursion: Parameters and local/global var
• Parameters are must in recursion to stop infinite exec
• All local variables (& memory for parameters) will be
created for each recursive call, but same copy of global var
will be shared among all calls.
• Let us see the code of arrmax using binary search logic:

9
Max of Array
int findmax(int arr[], int start, int end )
{ int mid,v1,v2;
if (start <=end)
{ mid = (start+end)/2;
v1=findmax(arr, start, mid-1);
v2=findmax(arr,mid+1,end);
if ((arr[mid] >= v1) && (arr[mid]>=v2))
return arr[mid];
else if ((v1>=arr[mid]) && (v1>=v2)) return v1;
else return v2;
}
return INT_MIN; } 10
Max of Array: using Global var
int mid,v1,v2;
int findmax(int arr[], int start, int end )
{ if (start <=end)
{ mid = (start+end)/2;
v1=findmax(arr, start, mid-1);
v2=findmax(arr,mid+1,end);
if ((arr[mid] >= v1) && (arr[mid]>=v2))
return arr[mid];
else if ((v1>=arr[mid]) && (v1>=v2)) return v1;
else return v2;
} return INT_MIN; }
• Let us See in DevCpp 11
Output After execution
main()
{int a[] = {14,6,15,18,10,12};
int res;
res=findmax(a,0,5);//try with size as 3 or 2
printf("%d\n", res);
}
What will be output?
• Printed Output: 12
• If call for v2 is made before v1, what will be o/p?
• Printed Output: 14

12
Max of Array: using Global var
int mid,v1,v2;
int findmax(int arr[], int start, int end )
{ …
v1=findmax(arr, start, mid-1);
v2=findmax(arr,mid+1,end); …
}
• If v1,v2 are global, last call to findmax overwrites
previously calculated v1, v2 of all pending recursive
calls and thus wrong results
• If max out of a binary tree was to be found, taking max as
global var can work easily.

13
Recursive Problems: Binary tree
• Inorder traversal, preorder, postorder
• Find max of a binary tree
• Find sum of all nodes of a binary tree
• Copy a binary tree
• Find mirror image of a binary tree
• Count leaf nodes, non-leaf nodes of a binary tree
• Find whether a binary tree is left-skewed or not?
• Right skewed or not?
• Find whether a binary tree is skewed or not in one scan

14
Practice Problems
• Super Digit (hackerrank):
https://www.hackerrank.com/challenges/super-digit/problem
• Fit Squares in a Triangle(codechef):
https://www.codechef.com/problems/TRISQ
• Kingdom of Fire & Ice (codechef):
https://www.codechef.com/problems/FICE?tab=statement

15

You might also like