One-way flight trip problem

后端 未结 18 1517

You are going on a one-way indirect flight trip that includes billions an unknown very large number of transfers.

  • You are not stoppi
18条回答
  •  醉话见心
    2020-12-12 17:40

    If you assume a joinable list structure that can store everything (probably on disk):

    1. Create 2 empty hash tables S and D
    2. grab the first element
    3. look up its src in D
    4. If found, remove the associated node from D and link it to the current node
    5. If not found, insert the node into S keyed on src
    6. repeat from 3 the other way src<->des, S<->D
    7. repeat from 2 with the next node.

    O(n) time. As for space, the birthday paradox (or something much like it) will keep your data set a lot smaller than the full set. In the bad luck case where it still gets to large (worst case is O(n)), you can evict random runs from the hash table and insert them at the end of the processing queue. Your speed could go to pot but as long as you can far excede the threashold for expecting collisions (~O(sqrt(n))) you should expect to see your dataset (the tables and input queue combined) regularly shrink.

提交回复
热议问题