Live data from Hacker News

How to solve the Secret Santa Problem using graph theory

medium.com

11–20 of 25 posts

Re: How to solve the Secret Santa Problem using graph theory

#11

Can't the Secret Santa problem be solved just by shuffling an array of participants, and assigning a person A[i] to give a gift to a person A[(i+1)%N]? I have a feeling that this is not correct, but cannot determine why.

I think it can as well, unless you require constraints as in the article.

Re: How to solve the Secret Santa Problem using graph theory

#12
post #9

Can't the Secret Santa problem be solved just by shuffling an array of participants, and assigning a person A[i] to give a gift to a person A[(i+1)%N]? I have a feeling that this is not correct, but cannot determine why.

Did you read the article? It adds constraints.

Yes, I asked about the unconstrained problem. Sorry, I should have mentioned that.

Re: How to solve the Secret Santa Problem using graph theory

#14
post #8

Every year, when my extended family (13 cousins) does Secret Santa, I complain “There are likely multiple cyclic graphs in this space!” But rather than engage my criticism with the mathematical rigor it clearly deserves, they just choose to restart with a random cousin who hasn’t given a gift yet when they discover the cycle. This is clearly a sub-optimal solution, and I am glad that when the ritual recurs next year,…

Why is it a problem if there is more than one cycle? Surely it would be more of a problem if you knew everyone was in a single cycle because then you'd have more information about which assignments can't exist.

Re: How to solve the Secret Santa Problem using graph theory

#15
post #2

What I don't understand is why it has to be a hamiltonian cycle. It could be simplified to just picking the set of edges in the directed graph that make every vertex have one incoming and one outgoing edge. The naive approach would be to assign sequentially from a set of available receivers, such as people passing around a hat and picking numbers from it without replacement, and not having the last picked participant…

As in many holiday events, it's just tradition that requires a Hamiltonian cycle. And tradition is not known for its reasonableness or flexibility!

The tradition says that after opening the gift they've received, they have to wear the funny Santa hat and bring the gift they brought to the person they were randomly assigned.

If the graph is not Hamiltonian, and instead goes A-B-C-A and D-E-F-D and G-H-G, there are going to be several awkward moments where someone has to arbitrarily choose someone who hasn't gone yet. It's really easy to generate these loops, and hard to get a 'proper' graph by drawing randomly from a hat, especially if you add other constraints like excluding spouses and your own kids/parents.

Re: How to solve the Secret Santa Problem using graph theory

#16
post #6

Maybe I’m not clever enough, but wouldn’t the naive implementation of the naive algorithm be O(n^2) operations? Depending on whether you have a linked list or an array, traversing to or removing a random element takes O(n) time, which you need to repeat n times.

The Fisher-Yates algorithm has O(n) running time but it is slightly different than what the author suggested, instead of removing the element from the array, you swap it with the last, that way you are sure not to get it again by simply reducing the upper bound of your random function and it's a O(1) operation so the total running time is O(n): https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#T...

Re: How to solve the Secret Santa Problem using graph theory

#18
post #8

Every year, when my extended family (13 cousins) does Secret Santa, I complain “There are likely multiple cyclic graphs in this space!” But rather than engage my criticism with the mathematical rigor it clearly deserves, they just choose to restart with a random cousin who hasn’t given a gift yet when they discover the cycle. This is clearly a sub-optimal solution, and I am glad that when the ritual recurs next year,…

What if you could show people the secret santa graph with names removed, then they could modify it to look more fun, and then show the assignments?

Re: How to solve the Secret Santa Problem using graph theory

#19
One year my family did this when we were on vacation in a foreign country. I happened to have my laptop with me and wrote a shell script that would generate a random Secret Santa graph. Each family member would go up to my laptop, it would say, “Are you Alice?” they’d press enter, and the program would say, “You are assigned to Bob. Press enter.” They’d press enter and it would clear, ready for the next person.

Re: How to solve the Secret Santa Problem using graph theory

#20
When we did secret Santa a few years back I wrote a little python script similar to the below:

    import random
    l = ['Alice', 'Bob', 'Benny', 'Dolores']
    random.shuffle(l)
    r = dict(zip(l, (l[(i + 1) % len(l)] for i in range(0, len(l)))))
    print(r)
Basically just shuffle the list, assign everybody the one next on the list (with wrap around).
Post reply on HN