Recursion
is the solving of large-scale problems by solving small-scale problems of
similar structure. There is always one or more "base cases", which
are cases that do not need to be broken down further. A recursive solution must
repeatedly break down the problem until one of these is reached. Some people
may struggle with the idea of recursion, so here is a simple visual explanation
of it:
The biggest doll contains similar smaller dolls within it, which contain
dolls of their own, until the smallest doll is reached. Once that doll is
"solved", the process goes backwards and "solves" previous
dolls until the first one is finally reached and "solved" through the
smaller solutions. This is a great concept for computer scientists to put to
use because it goes above and beyond looping structures and can solve complex
problems with elegance and efficiency. Although many or all loops can be
converted into recursive functions with a little bit of work, it is difficult
and sometimes impossible to convert recursive functions into loops. Recursion allows
the programmer to solve problems of unknown nested depth by solving
sub-problems of sub-problems of sub-problems with changing parameters -
something that loops cannot do because of their strict and repetitious
structure.
Brilliant example! It would be fun to code a Russian doll recursion, wouldn't it?
ReplyDeleteAs long as you don't end up with a DollOverflowError!
ReplyDeleteAwesome animation! :)
ReplyDeleteHaha, thanks! I honestly wasn't expecting to find an image on Google that fit recursion as well as this; all I wanted was a GIF of a Russian doll. xD
ReplyDelete