Curious Case of Recursion

| Comments

Recursion has always been imaginative to me. How do I write the recursive function so that it solve the problem, what all cases it would uncover as recursion progress. As input value increases things gets complex and I am lost.

To understand it better I was looking around internet and I found very ordinary yet powerful statement.

Functions can call other functions – this is a fact that most programmers know. Recursion is simply a special case where instead of calling another function, a function calls itself.

This is pretty good insight. This statement helps you to forget imaging the complex cases where you have high input value, what happens behind the scenes and so on.

Lets take an example

Write a function, for a given number, print out all different ways to make this number, by using addition and any number equal to or smaller than this number and greater than zero.

For example, given a 5, we have the following different ways to make up 5:
1st 1, 1, 1, 1, 1
2nd: 1, 4
3rd : 1, 1, 1, 2
4th : 1, 1, 3
5th : 2, 3
6th : 1, 2, 2
7th : 5

There are two ways of thinking about this problem
1. 1 + 1 = 2
2. 2 = 1 + 1

You would be wondering they both look pretty same. They both are same when result matters but they are different in approach.

Lets consider input as 3
Using approach 1, 3 be written as 1 + 1 + 1
But, using approach 2, 3 will be written as 3 = 1 + 2 and now how 2 can be written. Yes, 2 = 1 + 1

Finally, 3 = 1 + 2 = 1 + (1 + 1)
4 = 1 + 3 = 1 + (1 + 2) = 1 + (1 + ( 1 + 1))
and so on….

We can clearly see recursion pattern forming here. In this case now all we need to worry is given input just subtract 1 and call the same function again.

Recursion1 Run Code
 1
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 void printSeq(int num, int a[], int opIndx,int s){
 7     if(num <= 0){
 8         // Display the result
 9         for(int j=0;j<opIndx;j++)
10             cout<<a[j]<<",";
11         cout<<endl;
12         return;
13     }
14 
15     a[opIndx] = 1;
16     // Call the function again, remember 3 = 1 + 2
17     printSeq(num-1, a, opIndx+1, 1);
18 }
19 
20 int main(){
21     int a[5];
22     printSeq(5,a,0,1);
23     return 0;
24 }

We just saw case 1 but there are other combinations of 5. Lets consider the next combinations which are
5 = 2 + (3). Later 3 will have its own combinations which don’t need to bother.
5 = 3 + (2)
5 = 4 + (1)
5 = 5 + (0)

Combination Run Code
 1
 2  #include <iostream>
 3  
 4  using namespace std;
 5  
 6  void printSeq(int num, int a[], int opIndx,int s){
 7      if(num <= 0){
 8          //Display the output
 9          for(int j = 0; j < opIndx; j++)
10              cout << a[j] << ",";
11          cout << endl;
12          return;
13      }
14  
15      // s starts from 1 and later it will change to
16      // 2, 3, 4, 5.
17      // It is the case$
18      // 5 = 2 + (3)
19      // 5 = 3 + (2)
20      // 5 = 4 + (1)
21      // 5 = 5 + (0)
22      for(int i = s; i <= num; i++){
23          a[opIndx] = i;
24          printSeq(num-i, a, opIndx + 1, i);
25      }
26  }   
27  
28  int main(){
29      int a[15];
30      printSeq(5, a, 0, 1);
31      return 0;
32  }





References
Recursion on reddit

recursion

« Singleton - Design Pattern Boot FreeBSD »

Comments