Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Longest Increasing Subset

by gilthoniel (Novice)
on Oct 20, 2016 at 15:55 UTC ( [id://1174385]=note: print w/replies, xml ) Need Help??


in reply to Re: Longest Increasing Subset
in thread Longest Increasing Subset

This is great, but I want it to stop as soon as it gets one answer. I'm trying to do this for a super long set of numbers but the calculation time is *forever* so I'm trying to shorten it in every way possible.

Replies are listed 'Best First'.
Re^3: Longest Increasing Subset
by tybalt89 (Monsignor) on Oct 20, 2016 at 18:03 UTC

    Please give us the real problem, then, and not a "fake" small example.

    I'm suspicious that this problem is of the order O(2**N). If so, it's going to be "really" slow on "super long" sets.

      I'm suspicious that this problem is of the order O(2**N).

      I suspect it is more like O(N!).

      I also suspect that whilst inspecting the longest possible combinations for monotonicity, then in order of decreasing length so that you can short-circuit the full search space, sounds like an optimisation; in reality it will end up inspecting many more possibilities than a brute-force forward search with short-circuiting of the inner iterations.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I believe this can be reduced to LCS which is O(much better). Diff'ing the original data with its ordered copy would yield a sequence that (1) belongs to the original sequence, (2) is ordered and (3) is among the longest such sequences. (Answered below with code example).
Re^3: Longest Increasing Subset
by pryrt (Abbot) on Oct 20, 2016 at 18:35 UTC

    gilthoniel, you should be able to figure out how to use tybalt89's code as a starting point, but stopping as soon as you have a match. If you test the length of @answers at the end of the while loop, you can stop right away.

    Or you could add a similar check to GrandFather's answer — though, not having run his, I'm not positive his will run in longest-first order.

    Or, if the one to four lines of code (depending on how Perl-ly you think) that would be required to exit out of the loop when there's at least one element in @answers — if that's too difficult, you could look at my answer, which already tells you the one character you need to edit in order to get it to stop at the first match rather than listing them all. Given that my answer starts with the longest possible combination, and works its way down (inspired by your own structure, btw, to try to make it as easy for you to understand as possible), the first match will be the longest (or the first of the group that are all the same longest length, if there are multiple answers of the same length). If that won't work for you, please describe what feature of the answer does not meet your requirements.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174385]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found