-1

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.

ekad
  • 14,436
  • 26
  • 44
  • 46
user94154
  • 16,176
  • 20
  • 77
  • 116
  • 2
    Please clarify. are you loooking for all pairs? pairs with 2 or more given people? How is the data represented in memory (i.e. data structure.) Do people objects know about groups? Do group objects know about people? – CodePartizan Sep 18 '09 at 16:45

3 Answers3

1

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'])
>>>
Miki Tebeka
  • 13,428
  • 4
  • 37
  • 49
1

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
  • 12,229
  • 2
  • 23
  • 19
0

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
  • 5,973
  • 1
  • 36
  • 34