Recursion: when do you use it?

Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Recursion: when do you use it?

 
0
  #1
Mar 27th, 2005
Hi ladies and gents,

I made this exercise in wich recursion has to be used and it worked as it should:

  1. void ggd(short x, short y)
  2. {
  3. short a = 0;
  4.  
  5. if (y == 0)
  6. {
  7. cout<<"The largest common divider is: "<< x <<endl;
  8. }
  9.  
  10. else
  11. {
  12. a = (x% y);
  13. x = y;
  14. y = a;
  15. x% a != 0 ? ggd(x, y) :cout<<"The largest common divider is: "<< a <<endl;
  16. }
  17. }
  18.  
  19. int main()
  20. {
  21. ggd(1147, 851);
  22.  
  23. cin.get();
  24.  
  25. return 0;
  26. }

Now, I read the tutorial Dany made about this and she mentions:
Although recursion should generally be avoided, there are still algorithms which can only be solved with its use.
Wich algorithms are they, can someone give me some examples
I mean, when do you really know you HAVE TO USE recursion
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Recursion: when do you use it?

 
0
  #2
Mar 27th, 2005
My experience in the world of programming is very short. However, I think every recursive function can be transformed into an iterative version. But often recursive functions are easily understood(not for all though ) and makes the code shorter. So unless you worry too much (and I mean too much) about the effieciency of your program you can adhere to the recursive solution. One thing to remember is that whether the choice is better or not depends on the problem at hand.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Recursion: when do you use it?

 
0
  #3
Mar 27th, 2005
Originally Posted by Asif_NSU
My experience in the world of programming is very short. However, I think every recursive function can be transformed into an iterative version.
I know, that's what Dani wrote in her tutorial aswell

But often recursive functions are easily understood(not for all though ) and makes the code shorter.
I understand, I wrote with this exercise also a function wich didn't use recursion and I needed to write more code then with the use of recursion :!:

So unless you worry too much (and I mean too much) about the effieciency of your program you can adhere to the recursive solution.
What do you mean by this

One thing to remember is that whether the choice is better or not depends on the problem at hand.
Well that's the question I asked, when do you know it's better to use RECURSION
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Recursion: when do you use it?

 
0
  #4
Mar 27th, 2005
>when do you know it's better to use RECURSION
Most of the time, binary search trees are better written with recursive algorithms. Sure, it may be a fun exercise to do them iteratively, but anything but a simple binary search tree will be dreadfully complex without recursion. I haven't met many people who are up to the challenge of writing, say, a fully featured AVL tree without recursion.

So when is it better to use recursion? It's better when you can guarantee two things:

1) Each recursive step breaks down the problem into a smaller problem of the same type.
2) Each recursive step reduces the problem significantly.

The first is a general rule of recursion. If each step doesn't break the problem down into a smaller problem of the same type, it's harder to write a recursive function and guarantee that it will terminate. The second is my personal guideline. I generally won't write a recursive function unless it divides the problem in half with each step. This way I can verify with relative ease that the algorithm will be efficient.

>when do you really know you HAVE TO USE recursion
There are no situations where you have no choice but to use recursion. Any recursive algorithm can be converted to an interative algorithm through the use of a stack. The only question is how much work it takes to do this.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Recursion: when do you use it?

 
0
  #5
Mar 27th, 2005
Thanks for the clear and informative explanation Narue :!:

>Most of the time, binary search trees are better written with recursive algorithms.
One question about this tough, could you explain what a binary search tree actually is
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Recursion: when do you use it?

 
0
  #6
Mar 27th, 2005
>could you explain what a binary search tree actually is
I can, and I have.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Recursion: when do you use it?

 
0
  #7
Mar 28th, 2005
> I can, and I have.
Thanks Narue, that's a detailed explanation :!:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC