Reputation: 112
Some problems that require recursion have always put me in a fix. I am not always able to come up with a recursive algorithm, but I know that there is a recursive solution to the problem.
I find problems like factorial and fibonacci easy to implement using recursive approach. But when I face more complex problems such as generating the partition of a number http://en.wikipedia.org/wiki/Partition_%28number_theory%29, I know that there is a possible recursive approach but I get stuck right there. I can't devise a recursive algorithm. Suppose I want to print all combinations of a string or if I want to bruteforce a Coin Change problem using recursion, I can't devise a recursive approach.
Is there any particular way to think so as to come up with a recursive approach? Is there any extensive recursive algorithms tutorial out there which can help me solve more advanced problems?
Upvotes: 5
Views: 696
Reputation: 2325
Look through the Structure and Interpretation of Computer Programs book, which is highly recommended here on Stack Overflow and is freely available online. It uses the Scheme programming language to teach fundamental concepts about programming. Since Scheme is a functional programming language, recursion is widely used everywhere - not only where you would use it even in imperative programming languages such as C or PHP, but also where you would typically use a looping construct. The examples and problems in the book present recursion in its natural habitat, if you will, and not through convoluted made-up scenarios.
Upvotes: 3