How to pair socks from a pile efficiently?
Yesterday I was pairing the socks from the clean laundry and figured out the way I was doing it is not very efficient. I was doing a naive search — picking one sock and "iterating" the pile in order to find its pair. This requires iterating over n/2 * n/4 = n2/8 socks on average.
As a computer scientist I was thinking what I could do? Sorting (according to size/color/...) of course came to mind to achieve an O(NlogN) solution.
Hashing or other not-in-place solutions are not an option, because I am not able to duplicate my socks (though it could be nice if I could).
So, the question is basically:
Given a pile of n
pairs of socks, containing 2n
elements (assume each sock has exactly one matching pair), what is the best way to pair them up efficiently with up to logarithmic extra space? (I believe I can remember that amount of info if needed.)
I will appreciate an answer that addresses the following aspects:
Sorting solutions have been proposed, but sorting is a little too much : We don't need order; we just need equality groups .
So hashing would be enough (and faster).
This kind of recursive hash partitioning is actually being done by SQL Server when it needs to hash join or hash aggregate over huge data sets. It distributes its build input stream into many partitions which are independent. This scheme scales to arbitrary amounts of data and multiple CPUs linearly.
You don't need recursive partitioning if you can find a distribution key (hash key) that provides enough buckets that each bucket is small enough to be processed very quickly. Unfortunately, I don't think socks have such a property.
If each sock had an integer called "PairID" one could easily distribute them into 10 buckets according to PairID % 10
(the last digit).
The best real-world partitioning I can think of is creating a rectangle of piles : one dimension is color, the other is the pattern. Why a rectangle? Because we need O(1) random-access to piles. (A 3D cuboid would also work, but that is not very practical.)
Update:
What about parallelism ? Can multiple humans match the socks faster?
What about the element distinctness problem? As the article states, the element distinctness problem can be solved in O(N)
. This is the same for the socks problem (also O(N)
, if you need only one distribution step (I proposed multiple steps only because humans are bad at calculations - one step is enough if you distribute on md5(color, length, pattern, ...)
, ie a perfect hash of all attributes)).
Clearly, one cannot go faster than O(N)
, so we have reached the optimal lower bound .
Although the outputs are not exactly the same (in one case, just a boolean. In the other case, the pairs of socks), the asymptotic complexities are the same.
As the architecture of the human brain is completely different than a modern CPU, this question makes no practical sense.
Humans can win over CPU algorithms using the fact that "finding a matching pair" can be one operation for a set that isn't too big.
My algorithm:
spread_all_socks_on_flat_surface();
while (socks_left_on_a_surface()) {
// Thanks to human visual SIMD, this is one, quick operation.
pair = notice_any_matching_pair();
remove_socks_pair_from_surface(pair);
}
At least this is what I am using in real life, and I find it very efficient. The downside is it requires a flat surface, but it's usually abundant.
Case 1 : All socks are identical (this is what I do in real life by the way).
Pick any two of them to make a pair. Constant time.
Case 2 : There are a constant number of combinations (ownership, color, size, texture, etc.).
Use radix sort. This is only linear time since comparison is not required.
Case 3 : The number of combinations is not known in advance (general case).
We have to do comparison to check whether two socks come in pair. Pick one of the O(n log n)
comparison-based sorting algorithms.
However in real life when the number of socks is relatively small (constant), these theoretically optimal algorithms wouldn't work well. It might take even more time than sequential search, which theoretically requires quadratic time.
链接地址: http://www.djcxy.com/p/196.html上一篇: 设置“检查”的复选框与jQuery?
下一篇: 如何有效地从一堆袜子中配对?