Ian Hobson
6/29/2008 6:20:00 PM
Hi Dan,
Dan __ wrote:
> Hey all,
>
> Recently, one of my projects has run into a situation where I'm 95% sure
> I need to use recursion. Its a situation like the following:
>
People more expert than I have proven that recursion is not necessary -
although it can be a great convienience.
> Out of 20 numbers, I need a set of 10. Each user has two numbers, each
> of which may or may not be part of the set. I need to get 5 users to
> complete the set of 10 desired numbers.
>
> So, if the set is 1 2 3 4 5 6 7 8 9 10, and User1 has (9, 17), while
> User2 has (3, 7), User2 could be part of the set, while User1 could not.
>
> Now, I know enough to figure out that I need some recursion here, but I
> have no idea how to implement it. Could anyone point me to some guides
> or examples that could be of help to me? Thanks very much in advance :)
>
Hi Dan,
It appears to me, that you have not defined the problem carefully enough
for us yet. :)
So lets assume the set of users is finite, and fixed - and therefore
there may not be a solution. Further, lets assume that if you have taken
somebody into your solution, can you kick them out again, if you find
you can't solve it with them in.
Without loss of generality we can assume that a user's first number is
less than his second (or swap then to make it so).
If the above is true, I would approach it like this.
report is global. (or could return an object with report and the
success/failure flag as properties)
searchroutine(aSet)
if aSet is empty then // we have a solution!
report = "Solution found: "
return success.
end if
find the lowest number in aSet.
create set of users whose first number is this lowest number.
for each user U in this set.
if ( U's second number is in set) then
create newSet = aSet without both of U's numbers.
if (searchRoutine(newSet) == success)
append U to report as part of the solution
return success // abort for loop
end if
end if
end for
return failure. (there is no U to use).
end searchroutine.