views:

95

answers:

3

People can belong to one or many groups. What is a good algorithm to output common memberships?

ie, Persons A and B are in Groups C, D, and E ... etc

My preferred language would be Ruby (or maybe Python), but any code or pseudocode would be greatly appreciated.

A: 

Are you trying to find anything in particular about the memberships? Or are you just trying to find all memberships... Ie:

A - No group
B - Groups 1, 2, 3
C - Groups 2, 5
D - Groups 2, 3, 4

If it's the latter, I don't think there's a special algorithm to do this; as long as verifying that a person is in a group takes O(1) your best bet is the O(M * N) brute force algorithm.

For each person O(N) {
   Create a set for this person
   For each group O(M) {
       if the person is in the group, add this group to the set O(1) when using maps/hashed structures
   }
   output the set
}

If you're looking for the intersection of sets, there are many other algorithms out there... but this particular problem is nothing special.

Malaxeur
+1  A: 

Did you mean something like the below? (python):

>>> a_groups = set(["A", "B", "C"])
>>> b_groups = set(["B", "C", "D"])
>>> print a_groups & b_groups
set(['C', 'B'])
>>>
lazy1
+1  A: 

It's a very simple algorithm, actually (at least for reasonable numbers of users and groups).

Consider each user to be a set whose elements are the groups of which they are a member. To find the groups two users have in common, simply take the intersection of those two users' membership sets.

So, if Person A is in Group K, M, and N, and Person B is in K, N, and P, you would have the following sets:

A := {K, M, N}
B := {K, N, P}
intersect(A, B) = {K, N}

In Ruby, you can use the standard library class Set to perform these calculations:

require 'set'
memberships_a = Set[:K, :M, :N]
memberships_b = Set[:K, :N, :P]
shared = memberships_a.intersection(memberships_b)
# you can also use the '&' operator as shorthand for 'intersection'
shared_2 = memberships_a & memberships_b
rcoder