Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / What makes recursion useful?

Author
Message
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 17th Aug 2012 03:17
Here's an example of quicksort in DBPro:



I just posted a more commented version in the code snippets board if you're wondering how the algorithm works (though wikipedia would be better than my comments probably), but basically recursionn is the most straightforward way to think about it.


Here's an implementation (in C++) of a quadtree:


That code should compile (i haven't tried it, I modified it a bit removing the application-specific code I was using it for). To see a quadtree in action, go to http://mathandcode.com/programs/javagrav/ and click "start" and "show quadtree".

There's also this DBPro application but it's more complicated than the C++ version (in my mind at least) because it has to be procedural not object oriented http://forum.thegamecreators.com/?m=forum_view&t=186678&b=6


If you want examples of some hard recursion problems (whose solutions are almost always elegant I might add!) check out codingbat:
http://codingbat.com/java/Recursion-2
If you know java (VERY similar to c++ here because there's just one function) you can run the code on that site and have it test it for you, otherwise you'll have to code it up in some other language and verify for yourself that it works. I think there's also a python version.

Plystire
22
Years of Service
User Offline
Joined: 18th Feb 2003
Location: Staring into the digital ether
Posted: 17th Aug 2012 07:12
I don't understand why someone would insist on not using recursion. Honestly I prefer it over loops. 99 out of 100 times, using the recursive route will turn 100 lines into 10 lines.


~Plystire

A rose is only a rose until it is held and cherished -- then it becomes a treasure.
Dark Java Dude 64
Community Leader
14
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 17th Aug 2012 07:30
Phewey... After my being awake for like 33 hours I'll go to bed and look this thread over tomorrow. Thanks for all of your answers guys!

Your signature is being eras
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 17th Aug 2012 08:33 Edited at: 17th Aug 2012 08:36
It's easier to see what's happening logically by adding a couple of print statements.



So calling itself, is no different than calling one function from another. The new functions locals are allocated on the stack and Execution/control transfers to the new function. Upon completion, the locals are expunged and execution picks up back after the original call.

In the example (which does nothing useful) it's digging 5 levels deep before reaching our predefined cut of point. In a real routine there would have to be some cut off point also, where the routine naturally stops digging (hopefully ).

The process of finding all files is an easy one to visualize. So we'd give the function are starting folder/path. The function proceeds to get the file/folder names in this path. If it finds a FileName it'll stick that in our list/array of files found, but if it's a folder name (excluding "." ".."), we call simply call our function again with this new folder as the search path. This will make it dig down as low as need be through the devices file/folder structure.

Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 17th Aug 2012 10:38
Handy tip: If you're at a job interview for a programming job and the interviewer asks you to write some code for a function, first try to do it the recursive way. They're always looking to make sure you grasp recursion


Senior Developer - CBS Interactive Music Group
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Aug 2012 00:34
Recursion seems like the next step after learning functions.
When you start programming you hard-code everything; then you learn you only need one copy of a routine (subroutines); then you learn you only need one version of the routine (functions); then you learn you only need one instance of the routine (recursion).

Shh... you're pretty.
Dark Java Dude 64
Community Leader
14
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 20th Aug 2012 05:47
Interesting example you have kevin! I'll try to study that and make sense of it as much as possible! I should look at all the other codez people have written too!

Your signature is being eras
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 20th Aug 2012 08:01
And here's the flood fill function:



It's directly from wikipedia: http://en.wikipedia.org/wiki/Flood_fill

TheComet

"if you don't understand recursion than you probably don't understand recursion." ~Jerico2day
Dazzag
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Cyprus
Posted: 20th Aug 2012 13:53
Quote: "and that gives me OSD"
On Screen Display?

I don't know why the argument and confusion though. 99% of the time recursion really isn't needed for a problem. 1% of the time it makes things a *lot* easier (ie. quicker and time is money) to code, and for someone to decode in the future (ie. it's a *lot* more easy to read on a very complex problem). It makes what is a really complex programming problem into a much easier to solve problem. But if you can really easily solve that problem without recursion and the code still looks good and easy to read then why bother?

As for the stack overflow problems then it's not much more complicated than handling large arrays etc. ie. at some point you have to work an alternative method due to memory limitations. For example a long time ago I wrote a game in DBP that required the colour of every single colour (16m - I wasn't limiting things) used by my game to be mapped to a colour type (it was a rotating 2.5D game that required pixel perfect collision by scanning and if necessary recalculting every pixel colour I used based on the object type. ie. all colours used by each object type had to be unique to that object type). It worked pretty well but the way I was using it meant about 150mb of memory was used up in a huge array. Not a great idea especially at the time (about 2002 I think). So I rewrote the code to use a binary tree instead to store all the colour data. Used about 15mb of memory if I remember rightly and didn't lose any FPS (used very slightly more processing but binary tree searches are very quick). Point is you choose the best method for the problem. Sometimes recursion is the best method. It's just a lot rarer than what normally a loop and a function or two can sort out.

You look at the problem, you decide if recursion is a viable alternative. Easy. Our XML project was perfect for it with only slight memory enhancements to the Unix sessions, but perhaps your problem is really huge and would struggle on most devices. Considering it's easy to program then is no problem testing it out.

Cheers

Current fave quote : Cause you like musicians and I like people with boobs.

Login to post a reply

Server time is: 2025-05-18 21:38:23
Your offset time is: 2025-05-18 21:38:23