I have the n=4individuals named A, B, Cand D. Some people may belong to the same group, and some may not. This information is specified as follows (encoded in R)
comparisons = c("A-B", "A-C", "A-D", "B-C", "B-D", "C-D")
areEnemies = c(FALSE, FALSE, TRUE, FALSE, FALSE, TRUE)
From these data, individuals Aand Dare enemies and can not belong to the same group. Persons Cand Denemies cannot belong to the same groups. All other pairs of people are friends (when you someone is not your enemy, then he is your friend).
My goal is to create groups to
- The number of groups is minimized
- A person can belong to one or several groups (but must belong to at least one group)
- If two people are enemies, they should never be in the same group. If two people are friends (not enemies), then they must be in the same group at least once.
- If a person can belong to a group, then he must!
The solution (using lowercase letters for group names) for the above example is
A belongs to the group ABbelongs to group Aand groupBC belongs to the group AD belongs to the group B
I can’t wrap my head around this problem. Can you give me a hand?
If you want to write code, I welcome R, C, C ++, Java, Bash, Python, but a verbal description of the process (or pseudocode) will already be very useful. Please note that performance will not make much difference, as I usually have only 5-10 people and will not start this process too often.
source