Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Comparing 2-D co-ordinates

by dragonchild (Archbishop)
on Jul 31, 2003 at 15:33 UTC ( [id://279617]=note: print w/replies, xml ) Need Help??


in reply to Comparing 2-D co-ordinates

You're not working with 2-D coordinates ... you're working with 1-D coordinates. You're working on a line, not a plane. So, if you want to deal with finding the longest, calculate the length found.

Now, for the overlap ... it sounds like you need to do something like the following:

  1. Calculate length for each coordinate pair.
  2. Loop through the pairs, keeping each pair that provides unique coverage. If the pair's start or end coordinate is within the coverage provided by a previously-seen pair, that spot is kept by the pair with the longest coverage.
  3. Once you have determined that you're replacing pair A with pair B, you have to make sure that you're catching all pairs that are eliminated by pair B. For example, if your current list of acceptable pairs is: (1,3), (4, 6) and you come across (2,5) ... both (1,3) and (4,6) would have to be replaced with (2,5). (Assuming, of course, that I've understood your spec.)
If you want, I can help provide some code for you. /msg me if you'd like.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: Comparing 2-D co-ordinates
by belg4mit (Prior) on Jul 31, 2003 at 15:49 UTC
    I could be wrong but this doesn't quite solve the problem. See Re**4: Comparing 2-D co-ordinates. As a non-CS person I could be off-base but I'd call this a 1-D knapsack.

    --
    I'm not belgian but I play one on TV.

      Thanks,

      Not just for solving the problem but for pointing out that it is actually one of the class of NP-complete problems and so not making me feel too bad about not being able to come up with a simple solution.

      Now I think I just to to google for a solution.

      A.A.

Re: Re: Comparing 2-D co-ordinates
by aging acolyte (Pilgrim) on Jul 31, 2003 at 15:45 UTC
    Close but no coconut

    Not that I am criticising - the problem is mine for not explaing the specifications clearly enough.

    A final stab at defining it: Given a set of 1-D coordinates how can I calculate the maximum coverage using discrete non-overlapping sets.

    e.g. for the set (1,3),(4,6),(2-5) the mximum coverage I can find is (1,3) + (4,6).

    Is that good enough or should I just stop digging?

    :-)

      Given that definition, belg4mit is absolutely right - this is a 1-D knapsack. Look in any upper-level algorithms book or just google "knapsack algorithm". My algorithm would have given you the smallest number of largest individual segements without taking into account the total coverage. (It can be easily extended to that by figuring out the all the overlaps for a given new segment, then comparing the total coverage between the new segment and the old segment(s). However, I would go with the algorithm that PhD's have optimized. If you cannot figure out how to write it, I'd be glad to provide an implementation of my naive version.)

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2025-06-14 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.