4

So I'm currently trying to grasp the concept of recursion, and I understand most of the problems that I've encountered, but I feel as though its use wouldn't be applicable to too many computing issues. This is just a novice's assumption though, so I'm asking, are there many practical uses for recursion as a programmer? And also, what typical problems can be solved with it? The only ones that I've seen are heap sort and brain teaser-type problems like "The Towers of Hanoi which just seems very specific and lacking broad use.

Thanks

skaffman
  • 398,947
  • 96
  • 818
  • 769
Sam
  • 2,309
  • 9
  • 38
  • 53
  • Actually, the "Towers of Hanoi" properly understood, is not spefic, but has very broad use. One trick of problem solving is to make a problem look like another problem - especially one that you know how to solve. Navigating a tree structure (as in a database, or perhaps a lexical tree that a compiler might build) is a lot like navigating the Towers of Hanoi, and the solution has a great deal of similarity to it. – Zeke Hansell May 02 '11 at 16:48
  • 2
    possible duplicate of http://stackoverflow.com/questions/5859571/relevance-of-recursion (sorry, obligatory joke) – Jason Orendorff May 03 '11 at 14:12

7 Answers7

3

There are a plethora of uses for recursion in programming - a classic example being navigating a tree structure, where you'd call the navigation function with each child element discovered, etc.

John Parker
  • 54,048
  • 11
  • 129
  • 129
3

Here are some fields which would be almost impossible without recursion:

  • XML, HTML or any other tree like document structure
  • Compilation and parsing
  • Natural Language Processing
  • Divide and conquer algorithms
  • Many mathematical concepts, e.g. factorials

Recursion can lead to brilliantly elegant solutions to otherwise complex problems. If you're at all interested in programming as an art, you really should delve deeper.

Oh and if you're not sure, here's a solid definition of recursion:

Recursion (noun): See "Recursion"

Oliver Emberton
  • 951
  • 4
  • 14
1

It depends on what you're going to be doing I suppose. I probably write less than one recursive function a year as a C#/ASP.NET developer doing corporate web work. When I'm screwing around with my hobby code (mostly stat research) I find a lot more opportunities to apply recursion. Part of this is subject matter, part of it is that I'm much more reliant on 3rd party libraries that the client has already decided on when doing corporate work (where the algorithms needing recursion are implemented).

heisenberg
  • 9,665
  • 1
  • 30
  • 38
  • 1
    Uh yeah, thanks for that. Actually I have a very solid understanding of recursion and my statement stands. – heisenberg May 02 '11 at 16:31
  • 1
    Do you deal with things like XML? Do you deal with databases? XML has a very recursive definition, and databases, especially those with a tables that have links back to themselves, are very prime problems for recursive solutions. – Zeke Hansell May 02 '11 at 16:42
  • 1
    Apologies for the negative sounding remark. I just have been computing for a long time, and I spend much of my time educating novices about stuff that isn't being taught very well in schools any more. – Zeke Hansell May 02 '11 at 16:45
0

There are are several languages that don't support loops (ie. for and while), and as a result when you need repeating behavior, you need to use recursion(I believe that J does not have loops). In many examples, recursion requires much less code. For example, I wrote an isPrime method, it took only two lines of code.

public static boolean isPrime(int n) { return n!=1&&isPrime(n,2); }

public static boolean isPrime(int n,int c) { return c==n||n%c!=0&&isPrime(n,c+1); }

The iterative solution would take much more code:

public static boolean isPrime(int n)
{
    if(n==1) return false;
    int c=2;
    while(c!=n)
    {
        if(n%c==0) return false;
    }
    return true;
}

Another good example is when you are working with ListNodes, for example if you would like to check if all the elements in a ListNode are the same, a recursive solution would be much easier.

public static <E> boolean allSame(ListNode<E> list)
    {
        return list.getNext()==null||list.getValue().equals(list.getNext().getValue())&&allSame(list.getNext());
    }

The iterative solution would look something like this:

public static <E> boolean allSame(ListNode<E> list)
{
    while(list.getNext()!=null)
    {
        if(!list.getValue().equals(list)) return false;
        list=list.getNext();
    }
    return true;
}

As you can see, in most cases recursive solutions are shorter than iterative solutions.

faeophyta
  • 323
  • 5
  • 16
0

It's not something you use every day. But many algorithms about searching and sorting data can make use of it. In general, most recursive algorithms can also be written using iteration; oftentimes the recursive version is simpler.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

If you check the questions which are listed as "Related" to this question, you will find a "plethora" of stuff about recursion that will help you to understand it better.

Recursion isn't something new, and it is not just a toy concept. Recursive algorithms have been around since before there were computers.

The classic definition of "factorial" is a prime example:

fact(x) =
if x < 0 then fact(x) is undefined
if x = 0 then fact(0) = 1
if x > 0 then fact(x) = x * fact(x-1)

This isn't something that was created by computer geeks who thought that recursion was a cool toy. This is the standard mathematical definition.

Zeke Hansell
  • 665
  • 2
  • 6
  • 11
0

Call recursion, as a program construct, is something that should almost never be used except in extremely high-level languages where you expect the compiler to optimize it to a different construct. Use of call recursion, except when you can establish small bounds on the depth, leads to stack overflow, and not the good kind of Stack Overflow that answers your questions for you. :-)

Recursion as an algorithmic concept, on the other hand, is very useful. It's key to working with any recursively-defined data formats (like HTML or XML, or a hierarchical filesystem) as well as for implementing important algorithms in searching, sorting, and (everyone's favorite) graphics rendering, among countless other fields.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711