How can I pair socks from a pile efficiently?

后端 未结 30 958
旧巷少年郎
旧巷少年郎 2020-11-27 08:37

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

相关标签:
30条回答
  • 2020-11-27 09:29

    Cost: Moving socks -> high, finding/search socks in line -> small

    What we want to do is reduce the number of moves, and compensate with the number of searches. Also, we can utilize the multithreded environment of the Homo Sapiens to hold more things in the descision cache.

    X = Yours, Y = Your spouses

    From pile A of all socks:

    Pick two socks, place corresponding X sock in X line, and Y sock in Y line at next available position.

    Do until A is empty.

    For each line X and Y

    1. Pick the first sock in line, search along the line until it finds the corresponding sock.

    2. Put into the corresponding finished line of socks.

    3. Optional While you are searching the line and and the current sock you are looking at is identical to the previous, do step 2 for these socks.

    Optionally to step one, you pick up two sock from that line instead of two, as the caching memory is large enough we can quickly identify if either sock matches the current one on the line you are observing. If you are fortunate enough to have three arms, you could possibly parse three socks at the same time given that the memory of the subject is large enough.

    Do until both X and Y is empty.

    Done

    However, as this have simillar complexity as selection sort, the time taken is far less due to the speeds of I/O(moving socks) and search(searching the line for a sock).

    0 讨论(0)
  • 2020-11-27 09:32

    The theoretical limit is O(n) because you need to touch each sock (unless some are already paired somehow).

    You can achieve O(n) with radix sort. You just need to pick some attributes for the buckets.

    1. First you can choose (hers, mine) - split them into 2 piles,
    2. then use colors (can have any order for the colors, e.g. alphabetically by color name) - split them into piles by color (remember to keep the initial order from step 1 for all socks in the same pile),
    3. then length of the sock,
    4. then texture, ....

    If you can pick a limited number of attributes, but enough attributes that can uniquely identify each pair, you should be done in O(k * n), which is O(n) if we can consider k is limited.

    0 讨论(0)
  • 2020-11-27 09:32

    This is how I actually do it, for p pairs of socks (n = 2p individual socks):

    • Grab a sock at random from the pile.
    • For the first sock, or if all previously-chosen socks have been paired, simply place the sock into the first "slot" of an "array" of unpaired socks in front of you.
    • If you have one or more selected unpaired socks, check your current sock against all the unpaired socks in the array.
      • It is possible to separate socks into general classes or types (white/black, ankle/crew, athletic/dress) when building your array, and "drill-down" to only compare like-for-like.
      • If you find an acceptable match, put both socks together and remove them from the array.
      • If you do not, put the current sock into the first open slot in the array.
    • Repeat with every sock.

    The worst-case scenario of this scheme is that every pair of socks is different enough that it must be matched exactly, and that the first n/2 socks you pick are all different. This is your O(n2) scenario, and it's extremely unlikely. If the number of unique types of sock t is less than the number of pairs p = n/2, and the socks in each type are alike enough (usually in wear-related terms) that any sock of that type can be paired with any other, then as I inferred above, the maximum number of socks you will ever have to compare to is t, after which the next one you pull will match one of the unpaired socks. This scenario is much more likely in the average sock drawer than the worst-case, and reduces the worst-case complexity to O(n*t) where usually t << n.

    0 讨论(0)
  • 2020-11-27 09:33

    This question is actually deeply philosophical. At heart it's about whether the power of people to solve problems (the "wetware" of our brains) is equivalent to what can be accomplished by algorithms.

    An obvious algorithm for sock sorting is:

    Let N be the set of socks that are still unpaired, initially empty
    for each sock s taken from the dryer
      if s matches a sock t in N
        remove t from N, bundle s and t together, and throw them in the basket
      else
        add s to N
    

    Now the computer science in this problem is all about the steps

    1. "if s pairs with a sock t in N". How quickly can we "remember" what we've seen so far?
    2. "remove t from N" and "add s to N". How expensive is keeping track of what we've seen so far?

    Human beings will use various strategies to effect these. Human memory is associative, something like a hash table where feature sets of stored values are paired with the corresponding values themselves. For example, the concept of "red car" maps to all the red cars a person is capable of remembering. Someone with a perfect memory has a perfect mapping. Most people are imperfect in this regard (and most others). The associative map has a limited capacity. Mappings may bleep out of existence under various circumstances (one beer too many), be recorded in error ("I though her name was Betty, not Nettie"), or never be overwritten even though we observe that the truth has changed ("dad's car" evokes "orange Firebird" when we actually knew he'd traded that in for the red Camaro).

    In the case of socks, perfect recall means looking at a sock s always produces the memory of its sibling t, including enough information (where it is on the ironing board) to locate t in constant time. A person with photographic memory accomplishes both 1 and 2 in constant time without fail.

    Someone with less than perfect memory might use a few commonsense equivalence classes based on features within his capability to track: size (papa, mama, baby), color (greenish, redish, etc.), pattern (argyle, plain, etc.), style (footie, knee-high, etc.). So the ironing board would be divided into sections for the categories. This usually allows the category to be located in constant time by memory, but then a linear search through the category "bucket" is needed.

    Someone with no memory or imagination at all (sorry) will just keep the socks in one pile and do a linear search of the whole pile.

    A neat freak might use numeric labels for pairs as someone suggested. This opens the door to a total ordering, which allows the human to use exactly the same algorithms we might with a CPU: binary search, trees, hashes, etc.

    So the "best" algorithm depends on the qualities of the wetware/hardware/software that is running it and our willingness to "cheat" by imposing a total order on pairs. Certainly a "best" meta-algorithm is to hire the worlds best sock-sorter: a person or machine that can aquire and quickly store a huge set N of sock attribute sets in a 1-1 associative memory with constant time lookup, insert, and delete. Both people and machines like this can be procured. If you have one, you can pair all the socks in O(N) time for N pairs, which is optimal. The total order tags allow you to use standard hashing to get the same result with either a human or hardware computer.

    0 讨论(0)
  • 2020-11-27 09:33

    Consider a hash-table of size 'N'.

    If we assume normal distribution, then the estimated number of 'insertions' to have atleast one sock mapped to one bucket is NlogN (ie, all buckets are full)

    I had derived this as a part of another puzzle,but I would be happy to be proven wrong. Here's my blog article on the same

    Let 'N' correspond to an approximate upper-bound on the number of number of unique colors/pattern of socks that you have.

    Once you have a collision(a.k.a : a match) simply remove that pair of socks. Repeat the same experiment with the next batch of NlogN socks. The beauty of it is that you could be making NlogN parallel comparisons(collision-resolution) because of the way the human mind works. :-)

    0 讨论(0)
  • 2020-11-27 09:34

    Pick up a first sock and place it on a table. Now pick another sock; if it matches the first picked, place it on top of the first. If not, place it on the table a small distance from the first. Pick a third sock; if it matches either of the previous two, place it on top of them or else place it a small distance from the third. Repeat until you have picked up all the socks.

    0 讨论(0)
提交回复
热议问题