Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can one create Unique Pairing of participants

Status
Not open for further replies.

chdavisjr

Programmer
Jan 24, 2002
85
US
Okay, I hope I can explain this correctly.

I need to set up a scheduling app of sorts.
What I will do is take say 4 People, and try to match these 4 people up in unique pairs each day, for some activity.

I need to print out these possible pairings.
The condition is for, say, the following three (4) people:

Adam, Carol, Dave, and Ed:

Day(1): Adam - Carol
Day(2): Adam - Dave
Day(3): Adam - Ed
Day(4): Carol - Dave
Day(5): Carol - Ed
Day(6): Dave - Ed

(Note: Adam - Carol is the same as the pair Carol - Adam)

I know that this is simply using Combinations and the formula:
[sub]n[/sub]C[sub]r[/sub] = n! [÷] ( (n-r)!r! )

I have a function:

Code:
function factorial(n)
  if n <= 1 then
	factorial = 1
  else
	factorial=n * factorial( n - 1 )
  end if
end function

and the formula for combinations:
As an example I am hardcoding n=4 and r=2 here for simplicity's sake:
Code:
factorial(4)/(factorial(4-2)*factorial(2))
Which returns 6, which is correct.

I am having trouble incorporating this into the application to print out the 6 pairs shown above.

Can someone show me how to do this? I am sure it uses arrays.
It must work for any number of people that might sign up for a class.
Thank you in advance,
Chalmers
 
This may be a long way around, but it should work and may give you a different perspective or other ideas.

Let's say that each student has a unique ID number(Primary Key) -- in your example we'll just use the number 1-4:

1 - Adam
2 - Carol
3 - Dave
4 - Ed
Code:
'We'll need 2 recordsets:
Dim Users_RS         'All Users
Dim Remaining_RS     'Remaining Users

'And we'll need a string variable showing us the students we have already used
Dim varUsed

'Create the Users_RS
set Users_RS = dbConn.Execute("Select tblUsers.* from tblUsers")

Do Until Users_RS.EOF
   'Now we need to populate the varUsed variable with the StudentID's that we want to eliminate from the remaining Recordset - we are assuming that the studentid is numeric
   'Because we are using the current student record, we'll eliminate them from the remaining recordset
   If Len(varUsed) = 0 then
      'this is the first record
      varUsed = Users_RS("StudentID")
   Else
      'Here we are adding the next student to the list of Used StudentID's
      varUsed = varUsed & ", " & Users_RS("StudentID")
   End If

   'Now we can generate our remaining recordset
   Set Remaining_RS = dbConn.Execute("Select tblUsers.* from tblUsers Where StudentID Not In (" & varUsed & ")")

   'Now we can loop through the remaining recordset to match up the current student.
   Do Until Remaining_RS.EOF
       Response.write Users_RS("StudentName") & " - " & Remaining_RS("StudentName")
   Remaining_RS.MoveNext
   Loop
Users_RS.MoveNext
Loop

The sql statements may need to be tweaked. Hope this helps,

TwoOdd
--------------
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner
 
Thank you, TwoOdd, but I incorrectly stated my problem.
Let me restate it here.
Assume 6 attendees named A, B, C, D, E, and F.
I need to match all the attendees together each day into different pairings, as shown below:
Day 1: A-B, C-D, E-F
Day 2: A-C, B-E, D-F
Day 3: A-D, C-E, B-F
Day 4: A-E, B-D, C-F
Day 5: A-F, B-C, D-E

I need to develop a way to manipulate any given number of attendees, but I understand there will always be an even number (4,6,8,20, etc.) Then a list will be printed out showing the daily pairings.
Hope this helps.
Also, would someone be so kind as to point me to the correct forum, in case this is not the appropriate one?
Chalmers
 
Lets say you only have 4 attendees, you would only have 6 unique pairings. You would also want all 4 attendees to participate every day, but this means you run out of unique pairings after 3 days. Will the class last the same number of days no matter how many attendees, or will the class be over when you run out of unique attendees? In other words, if the class has to last 5 days, do you start over with the pairings on day 4, OR if the class only lasts 5 days and you have 8 or more attendees, do you just not use the extra pairings?

TwoOdd
--------------
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner
 
chdavisjr,
The algorithm to do this is pretty complex because of the way a matrix would have to be constructed. To have fixed numbers is relatively simple because you could construct a nested loop function

the combination [sub]n[/sub]C[sub]k[/sub] and permutation [sub]n[/sub]P[sub]k[/sub] functions are fairly simple.

Code:
function Factorial(intNum)
'  factorial calculation
dim i
Factorial = 1
if intNum => 0 then 
	for i = 1 to intNum
		Factorial = Factorial * i 
	next
else
	Factorial = - 1
	exit function
end if
end function

function Combination(intTotal,intGroup)
Combination = Factorial(intTotal)/(factorial(intTotal-intGroup)*factorial(intGroup))
end function

function Permutation(intTotal,intGroup)
Permutation = Factorial(intTotal)/factorial(intTotal-intGroup)
end function

but to calculate all the combinations from any 2 numbers will require a matrix (array) of [sub]n[/sub]P[sub]k[/sub] elements (n - 1 columns x n rows) where each cell holds one of the pairings. You then need to get the [sub]n[/sub]C[sub]k[/sub] of unique pairings. For [sub]4[/sub]C[sub]2[/sub] this would be, 3 from the top row (0,1,2), 2 from the 2[sup]nd[/sup] row (1,2) and 1 from the 3[sup]rd[/sup] row (2)
[I'll have a consonant, please Carol] [lol]

Not that this problem interested me or anything [pc2] (there doesn't seem to be a sad geek smiley) but I keep looking at it when I get a spare half hour.




Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
Thanks, Chris, and TwoOdd.

TwoOdd:
I'll assume that after all match pairings have been used, they will start over. The class(es) will have 15-20+ attendees, most likely, so this will (most likely) not be an issue. The main thing is to assure that there are unique pairings for each day.

Chris:
I'll play around with this. If you do happen to get that 1/2 hour, and come upon something, I would gladly "listen" [bigears]
Chalmers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top