Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey guys!

A question :

Say I have a group of 8 people. I want to write a method that makes one person point to a different one in the same group for example :

P1 > P6
P2 > P3
P3 > P1
P4 > P5
P5 > P2
P6 > P8
P7 > P4
P8 > P7

If a person 'points' to another person (for example person 1 points to person 6) no other person may point to person 6. Besides that, persons cannot point to themselves. Also I want to be able to create rules for combinations that are not allowed :
P2 cannot point to P5 and P8 cannot point to P1

For now, I got this working but with a trial on error method. Try to get things working in a loop and if it's not working, re-enter the loop. If method fails for a couple of times I return an error.

I wonder if there's a fail-safe way to do this that allows me to check the amount of possibilities so I can raise an error when a rule is added that makes the entire process impossible. And that also makes it possible to run the routine only once, and results in a valid combination set.
Posted

This is a very interesting question: vote => 5. It's also a question I think would be very appropriate to put in the CodeProject 'Algorithms Forum, if you care to move it.

Once you go beyond the simple goal of a no-overlap shuffle:
group of 8 people. I want to write a method that makes one person point to a different one in the same group
and start adding in rules for specific valid/invalid "pointing" cases, then I view this as the kind of problem that "decision tables" (references below) are designed to model. Similar problems arise in creating language parsers, or state-machines for interface design. imho, the really interesting "bits" in decision-table theory/algorithms are in how one tests the validity (internal consistency) of all rules, or any potential new rule.

In your specific problem, you do have two "simplifying" conditions: P#n cannot point to P#n where #n is the same for both P; and, once a P is "pointed to," they are no longer available as "pointee." Also simplifying your problem is that you evidently don't wish a random ordering of the first item of the matched pairs.

If you change your task so you require a random ordering (sequence) of both elements in each pair, the problem becomes much more interesting.

If you think of the initial pool of "pointing possibilities," as being #56 ... each of the eight persons could point to seven other people ... then ... if only two people in the pool of eight are "pointing," the pool of possible pointers and pointees has shrunk to six, with only #30 valid possible "pointings" left. That kind of exponential decrease in possibilities suggests, to me, as W. Balboos proposes in his answer: a "removal" strategy.

Decision Tables on CodeProject: [^]. Of particular interest may be David Killian's October 2013 article.

Information on Decision Tables provided by Visual-Paradigm, a commercial software company: [^]. Note: I do not work for them, or own their products.

Wikipedia: Decision Tables [^].

Google: Decision Tables + C# [^].
 
Share this answer
 
v6
Comments
Eduard Keilholz 28-Oct-13 3:16am    
Thanks a lot for you reply. I will look into the decision tables and see where they bring me. Once I've got everything up and running I'll see if the stuff is interesting enough to write something about it here.
A way to avoid the problem of not assigning an element more than once is to create two arrays: A the source, B the assignee pool. As you assign an element in B to A, remove it from list B. How you remove it from the array depends upon how you select elements: you can drop an element altogether or use a flag to mark as eligible/ineligible.

(The removal-from-array is particularly useful for random assignments as you simply pick a random element, remove it from the array, and work with an every shrinking source array and random value range)

Per the other rules: aside for checking for self-assignment, there's not enough info here to suggest anything.

 
Share this answer
 
Comments
BillWoodruff 26-Oct-13 2:48am    
Excellent +5.
Eduard Keilholz 28-Oct-13 3:20am    
Thanks for your reply! If it's not for the additional rules (person a cannot point to person c) the algorithm is fairly simple and I has a solution similar to arrays (Generic lists) but with the same thought. My problem is where we must be able to define rules about the 'picking' process. Maybe BillWoodruff's answer explains my problem better as het perfectly points out the issues I run into.

Once again, thanks for the input.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900