Open pcuci opened 8 years ago
Trying various scenarios by hand, looks like an adjacency matrix is generalizable to conspiracy Santa too, the more complicated version of the game where teams can form to find someone a present. This implementation can also identify one person who needs to be Santa for 2+ people, in a future version, if needed (should the constraints be enforced to a maximum).
This solution doesn't readily support subgroups to gift other subgroups, as in a couple (together) buying a (one) present to another couple (happens in real life, all the time!). However, this exotic scenario may be abstracted/reduced to a simpler graph problem; further investigation required. Out of scope for now.
After randomizing the players into giftees we need to (stably) sort them by out-degree (number of people not to gift, e.g.: self and maybe the better half if not single). Then, we can mechanically go through the matrix left to right and discover if there is a solution, and if yes, provide a possible answer.
Sorting a the randomized giftees list (a copy of all the players) ensure the randomness of secret santa matches.
Came across an edge case which the random sort solution fails, seems like the problem is NP-hard (if I stumbled across the correct isomorph). The edge case is 5 players, A-B, C, D, E. Random sort will always pick C>A, D>B, A>C, B>D, and we're left with E which can't link to itself...
The challenge is to find a path through all nodes that are not connected (i.e. without passing through the paired paths).
Seems to be equivalent to the longest path problem: https://en.wikipedia.org/wiki/Longest_path_problem
The minimal number of players for paired-play is 4, either 2 pairs or one pair and two singles is required for a solution to exist.
Disallow matching between lovers.
Update matching algorithm to exclude conjointes.