TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Secret Santa is NP-Complete (2006)

45 pointsby caffeinewriterover 9 years ago

8 comments

spooningtamarinover 9 years ago
There&#x27;s a common reasoning mistake when it comes to reduction and conclusion of complexity class. You cannot say a problem is NP-complete if you reduced it to an NP-complete problem.<p>The problem you know is NP-complete has to be reduced to your Santa problem.<p>What OP described was mapping from Secret Santa to Hamiltonian circuit, not the other way around.<p>I&#x27;ve had a bunch of problems reduced to be solvable with approximation algorithms of NP-hard problems, that doesn&#x27;t make them NP-hard, it&#x27;s equivalent to solving a problem by reducing it to an NP-complete one with an exponential algorithm.<p>That&#x27;s why it&#x27;s not that simple to prove a problem NP-complete.<p>It might be that the problem is filled with symmetry and by modifying an algorithm for an NP-complete problem you get a polynomial one.<p>For example, Vehicle Routing Problem with Time Windows (multiple Hamiltonians with time windows) is considered NP-hard but if your locations had a lot of short time windows you can solve any instance in polynomial time (almost O(n)). But you can also solve it with any VRPTW algorithm (DP, PTAS, heuristics etc.).
评论 #10721056 未加载
评论 #10720333 未加载
btillyover 9 years ago
No, it isn&#x27;t. It is true that solving the Hamiltonian problem would solve Secret Santa. But the Secret Santa problem is more technically the &quot;disjoint cycle problem&quot; and that is in P.<p>See <a href="http:&#x2F;&#x2F;cstheory.stackexchange.com&#x2F;questions&#x2F;8563&#x2F;partition-a-graph-into-node-disjoint-cycles" rel="nofollow">http:&#x2F;&#x2F;cstheory.stackexchange.com&#x2F;questions&#x2F;8563&#x2F;partition-a...</a> for details.
评论 #10720385 未加载
评论 #10719703 未加载
ryansloanover 9 years ago
We were just talking about this at the office this week and came to a similar conclusion. However, someone pointed out later that you could have a valid Secret Santa configuration that was not a Hamiltonian Path - you could have two (non-overlapping) paths and still have a valid secret santa: For people A B C D E F, you could have the graph covered by two separate circuits:<p>A-&gt;B-&gt;C-&gt;A<p>D-&gt;E-&gt;F-&gt;D<p>Unfortunately real-life intervened and we didn&#x27;t have time to decide if this was an NP complete problem or not...
评论 #10719567 未加载
评论 #10719461 未加载
Jtsummersover 9 years ago
&gt; It turns out that this is exactly the problem I was trying to solve. Imagine my Secret Santa problem as a graph where each person is a node and there are edges between all nodes that are not blacklisted. In this view of the problem, I&#x27;m trying to find a path around the graph, [visiting] each node once.<p>This is one of the fun things, to me, about having a background&#x2F;interest in the more theoretical sides of CS&#x2F;mathematics as a programmer. The day you realize that half the problems you&#x27;re working on are really already solved (or related to) problems on graphs or some other structure, and if you reorganize your models around this you can get a much cleaner implementation. Come up with a representation of your problem using a graph, run a well-known algorithm from a graph library, solution found (hopefully before Christmas).<p>This is the one major gap I&#x27;ve noticed between professional programmers with a CS academic background versus those from an engineering academic background. The gap is easily closed, but the engineers lack the random bits of extra discrete maths or CS theory to make these kind of connections. OTOH, I&#x27;m sure programmers from an engineering background can easily say the same from the other side using some other set of knowledge.
评论 #10720646 未加载
gweinbergover 9 years ago
Some really sloppy thinking here. He didn&#x27;t really define the problem he&#x27;s trying to solve in the first place.<p>Here&#x27;s how you do secret santa: take a list of people, shuffle the list, each person buys for the next person on the list, last person buys for the first person. Polynomial time.
评论 #10720546 未加载
metatationover 9 years ago
I took a stab at this exact scenario years ago (1999?) and came to roughly the same conclusion. I ultimately decided to treat it as a travelling salesman problem where the distances between nodes (people) were weighted based on how recently they had matched with each node in the past.<p>This ultimately led me to create a TSP solving library that implemented a bunch of the known heuristic algorithms to approximate the solution. Worked out pretty well in the end and learned lots while I was at it.
contravariantover 9 years ago
The real tricky part is making an algorithm that gives a uniformly random Secret Santa. In fact even generating a random permutation is quite tricky.<p>Usually the easiest is to just use Fisher-Yates to get a random permutation and reject it if it doesn&#x27;t fit the criteria (people shouldn&#x27;t pick themselves etc.). For nearly all practical problem sizes this should be fast enough.<p>Trying to reject permutation half way and fixing it has a tendency to cause subtle biases.
oneeyedpigeonover 9 years ago
What&#x27;s wrong with <i>something like</i>:<p><pre><code> generate a hash (e.g. md5) of current year (or &#x27;draw&#x27; if you do secret santa once a month) and each person&#x27;s name sort the people by their hashes draw 1 and 2 together, 3 and 4, etc.</code></pre>
评论 #10719827 未加载