General [M]ayhem

General [M]ayhem (http://www.genmay.com/index.php)
-   CompuGlobalHyperMegaNet (http://www.genmay.com/forumdisplay.php?f=20)
-   -   recursion help (http://www.genmay.com/showthread.php?t=840213)

_mike_ 03-30-2012 02:56 PM

recursion help
 
i have a program i want to rewrite using recursion if possible. i have an iterative version of the program that works (i've tested it), but it's very cumbersome and if there's a recursive solution i'd like to use it.

the problem is like this: start with a list of objects that can be classified using simple rules. i want to form all subsets of a certain size that follow the rules. for example, the resulting set can only have 2 objects of type A, 3 of type B, etc.

this is obviously easy to do iteratively using nested loops. the problem is that i want subsets of size 16, hence nested loops. here's my idea for a recursive version:

[code]
function chooseNext(Set, Subset)
if length(Set) == 1, return Set(1)
else
for each s in Set
Subset = s
return chooseNext(Set - s, Subset)
[/code]

something like this?


not homework, if i was still in school i'd be smart enough to figure this out.

DreamWarrior 03-30-2012 06:13 PM

If you have a working iterative solution I would stick with it, it is likely faster.

:ninja: 04-01-2012 12:23 AM

[QUOTE=DreamWarrior;24999957]If you have a working iterative solution I would stick with it, it is likely faster.[/QUOTE]

This. Does the solution have to be recursive? If not, don't bother. Recursion is appropriate in very special cases; otherwise it's just an attempt to be clever... which never ends well. In other words, have fun trying to understand what the code does three months from now, especially if the solution must be extended or modified. If the solution involving recursion is not immediately obvious (which it clearly is not) then file it under "being clever".


All times are GMT -7. The time now is 06:04 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2002-2015 CrowdGather, Inc.