Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If k is very close to 1 or N, any algorithm that generates the sorted sums lazily could simply be run until the kth or N-kth item pops out.</p> <p>In particular, I'm thinking of best-first search of the following space: (a,b) means the ath item from A, the first list, added to the bth from B, the second. </p> <p>Keep in a best=lowest priority queue pairs (a,b) with cost(a,b) = A[a]+B[b].</p> <p>Start with just (1,1) in the priority queue, which is the minimum. </p> <pre><code>Repeat until k items popped: pop the top (a,b) if a&lt;|A|, push (a+1,b) if a=1 and b&lt;|B|, push (a,b+1) </code></pre> <p>This gives you a saw-tooth comb connectivity and saves you from having to mark each (a,b) visited in an array. Note that cost(a+1,b)>=cost(a,b) and cost(a,b+1)>=cost(a,b) because A and B are sorted.</p> <p>Here's a picture of a comb to show the successor generation rule above (you start in the upper left corner; a is the horizontal direction):</p> <pre><code>|------- |------- |------- </code></pre> <p>It's just best-first exploration of (up to) all |A|*|B| tuples and their sums.</p> <p>Note that the most possible items pushed before popping k is 2*k, because each item has either 1 or 2 successors. Here's a possible queue state, where items pushed into the queue are marked <code>*</code>:</p> <pre><code>|--*---- |-*----- *------- </code></pre> <p>Everything above and to the left of the <code>*</code> frontier has already been popped.</p> <p>For the <code>N-k&lt;k</code> case, do the same thing but with reversed priority queue order and exploration order (or, just negate and reverse the values, get the (N-k)th least, then negate and return the answer).</p> <p>See also: sorted list of pairwise sums <a href="https://stackoverflow.com/questions/826/efficiently-get-sorted-sums-of-a-sorted-list">on SO</a>, or the <a href="http://maven.smith.edu/~orourke/TOPP/P41.html" rel="nofollow noreferrer">Open problems project</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload