General [M]ayhem

Go Back   General [M]ayhem > Real Time Sub-Forums > CompuGlobalHyperMegaNet
Register Members List Mark Forums Read [M]erchandise Calendar

Reply
 
Thread Tools
_mike_
 
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)
something like this?


not homework, if i was still in school i'd be smart enough to figure this out.
Old 03-30-2012, 03:56 PM _mike_ is offline  
Reply With Quote
#1  

Advertisement [Remove Advertisement]

DreamWarrior
 
DreamWarrior's Avatar
 
If you have a working iterative solution I would stick with it, it is likely faster.
Old 03-30-2012, 07:13 PM DreamWarrior is offline  
Reply With Quote
#2  

:ninja:
My cooter sweats, and reeks like rotting sea vermon.
 
:ninja:'s Avatar
 
Quote:
Originally Posted by DreamWarrior View Post
If you have a working iterative solution I would stick with it, it is likely faster.

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".
__________________
Use Linux and BSD
Old 04-01-2012, 01:23 AM :ninja: is offline  
Reply With Quote
#3  

Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -7. The time now is 09:18 PM.



Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.