http://www.perlmonks.org?node_id=821097


in reply to Challenge: Sorting Sums Of Sorted Series

Can't help, have to ask this question:

how would an algorithm that would deal with this problem for infinite (huge...) lists look like...?

Update: And, given a finite part of each serie, a1, ..., aN, b1, ..., bM, how many - which - terms of the result could be trustfully computed (knowing that there are other terms to follow) ?

And how far could we advance with the 'arrival' of aN+1 and/or bM+1...?

hmmmm... Is someone aware of some good readings in order to get the answers ?

Thanks,

Krambambuli
---
  • Comment on Re: Challenge: Sorting Sums Of Sorted Series

Replies are listed 'Best First'.
Re^2: Challenge: Sorting Sums Of Sorted Series
by Limbic~Region (Chancellor) on Feb 03, 2010 at 14:05 UTC
    Krambambuli,
    Before answering your question, keep in mind I already allowed for both lists to fit in memory as a given. In reality, if you had a really large list you probably wouldn't be going through these contortions - you would probably sort on disk.

    Here are a few links to help your main question

    I guess your question boils down to: How can you do this with even less than 2N + M? blokhead has shown a solution that uses no memory but takes (N*M)^2 to run. TO give you a specific answer, one would need a specific problem. As I demonstrated in How many words does it take?, the trick to solving really hard problems in the general case (NP complete) is to exploit the details of the specific case (effectively turned into O(1)).

    Cheers - L~R

Re^2: Challenge: Sorting Sums Of Sorted Series
by rubasov (Friar) on Feb 03, 2010 at 13:11 UTC
    Just some ideas on how you can answer your questions:

    For representing infinite lists I recommend you to read Mark Jason Dominus' excellent book named Higher Order Perl, especially the chapter on infinite lists (where he is using closures for iterators and promise-forcing streams). It is available online in pdf format: http://hop.perl.plover.com.

    And for your updated question: given the finite parts (a1, ..., aN), (b1, ..., bM), if you take min(a1+bM, b1+aN), you are safe to print any sum less than or equal to this threshold as a beginning sequence of the final list. Anything above this threshold cannot be considered final.

    update: First I've written this: min(a1, b1) + min(aN, bM), but this is wrong, corrected above. And minor grammatical edits, arrgh...