in reply to Can I speed this up? (repetitively scanning ranges in a large array)

I trying to approach your problem in a quite different way, which means that my calculations aren't just a refactoring of your code, but worked out from scratch. I've been producing results broadly similar to yours for a while, and I get identical results for the 3-point/max 10 dataset.

For the larger test-set, I always get some discrepancies, and have been attributing them to errors in my algorithm. But I could never work out were I was going wrong.

I'm now convinced that your algorithm above is wrong! (Or I'm misunderstanding the question completely?)

Given the following set of points with a MAX of 35:

[ 25, 1 ][ 31, 1 ][ 29, 9 ][ 35, 9 ][ 28, 10 ][ 28, 10 ][ 31, 11 ] [ 34, 18 ][ 35, 20 ][ 21, 35 ][ 22, 35 ][ 13, 31 ][ 19, 31 ][ 21, 31 ] [ 30, 31 ][ 22, 30 ][ 19, 26 ][ 24, 26 ][ 7, 25 ][ 7, 24 ][ 16, 24 ] [ 17, 22 ][ 19, 22 ][ 1, 20 ][ 6, 20 ][ 10, 16 ][ 10, 16 ][ 14, 16 ] [ 2, 15 ][ 1, 13 ][ 4, 12 ][ 11, 12 ][ 6, 10 ][ 1, 9 ][ 5, 6 ]

The OP code produces this set of half-distances:

9 9 8 8 7 8 9 10 10 11 10 9 8 8 9 10 9 8 7 8 9 10 9 8 7 6 7 8 7 6 6 5 +6 7 8

In order to test this hypothesis, I knocked up a script to produce a diagram similar to your ascii art in the OP, for any given data&results set, so that I could manually verify the results. This is what it produces for the above:

c:\test>868765-plot -MAX=35 -S=20 -R=35 -DIST=op.results 1234567890123456789012345678901234567890 *|||||||||||||||||||||||***********|[ 25, 1 ] *|||||||||||||||||||||||||||||*****|[ 31, 1 ] *********|||||||||||||||||||*******|[ 29, 9 ] *********|||||||||||||||||||||||||*|[ 35, 9 ] **********|||||||||||||||||********|[ 28, 10 ] **********|||||||||||||||||********|[ 28, 10 ] ***********|||||||||||||||||||*****|[ 31, 11 ] ******************|||||||||||||||**|[ 34, 18 ] ********************||||||||||||||*|[ 35, 20 ] ||||||||||||||||||||***************|[ 21, 35 ] |||||||||||||||||||||**************|[ 22, 35 ] ||||||||||||*******************|||||[ 13, 31 ] ||||||||||||||||||*************|||||[ 19, 31 ] ||||||||||||||||||||***********|||||[ 21, 31 ] |||||||||||||||||||||||||||||**|||||[ 30, 31 ] |||||||||||||||||||||*********||||||[ 22, 30 ] ||||||||||||||||||********||||||||||[ 19, 26 ] |||||||||||||||||||||||***||||||||||[ 24, 26 ] ||||||*******************|||||||||||[ 7, 25 ] ||||||******************||||||||||||[ 7, 24 ] |||||||||||||||*********||||||||||||[ 16, 24 ] ||||||||||||||||******||||||||||||||[ 17, 22 ] ||||||||||||||||||****||||||||||||||[ 19, 22 ] ********************||||||||||||||||[ 1, 20 ] |||||***************||||||||||||||||[ 6, 20 ] |||||||||*******||||||||||||||||||||[ 10, 16 ] |||||||||*******||||||||||||||||||||[ 10, 16 ] |||||||||||||***||||||||||||||||||||[ 14, 16 ] |**************|||||||||||||||||||||[ 2, 15 ] *************|||||||||||||||||||||||[ 1, 13 ] |||*********||||||||||||||||||||||||[ 4, 12 ] ||||||||||**||||||||||||||||||||||||[ 11, 12 ] |||||*****||||||||||||||||||||||||||[ 6, 10 ] *********|||||||||||||||||||||||||||[ 1, 9 ] ||||**||||||||||||||||||||||||||||||[ 5, 6 ] +-------->||||||||||||||||<--------| 9 -+-------->||||||||||||||||<-------| 9 --+------->||||||||||||||||||<-----| 8 ---+------->||||||||||||||||||<----| 8 ----+------>||||||||||||||||||||<--| 7 -----+------->||||||||||||||||||<--| 8 ------+-------->||||||||||||||||<--| 9 -------+--------->||||||||||||||<--| 10 --------+--------->||||||||||||||<-| 10 ---------+---------->||||||||||||<-| 11 <---------+--------->||||||||||||||| 10 ||<--------+-------->||||||||||||||| 9 ||||<-------+------->||||||||||||||| 8 |||||<-------+------->|||||||||||||| 8 |||||<--------+-------->|||||||||||| 9 |||||<---------+--------->|||||||||| 10 |||||||<--------+-------->|||||||||| 9 |||||||||<-------+------->|||||||||| 8 |||||||||||<------+------>|||||||||| 7 |||||||||||<-------+------->|||||||| 8 |||||||||||<--------+-------->|||||| 9 |||||||||||<---------+--------->|||| 10 |||||||||||||<--------+-------->|||| 9 |||||||||||||||<-------+------->|||| 8 |||||||||||||||||<------+------>|||| 7 |||||||||||||||||||<-----+----->|||| 6 |||||||||||||||||||<------+------>|| 7 >||||||||||||||||||<-------+-------| 8 >||||||||||||||||||||<------+------| 7 >||||||||||||||||||||||<-----+-----| 6 ->||||||||||||||||||||||<-----+----| 6 ->||||||||||||||||||||||||<----+---| 5 --->||||||||||||||||||||||<-----+--| 6 ----->||||||||||||||||||||<------+-| 7 ------->||||||||||||||||||<-------+| 8

If you consider position 1.

  1. Moving right (wrapping), you have to go to at least position 21 in order to escape the clutches of the 9th range 35, 20 , or the 24th range: 1, 20 ;

    Which would be a half-distance of 20.

  2. Moving left (wrapping around) you need to move to position 24 to escape the 1st range: 25, 1 ;

    Which is a half-distance of 12.

Which means the final half-distance for position 1 should be 12; not 9 as produced by the OP code.

Similarly, considering position 35

  1. Moving right, you have to go to at least position 21 in order to escape the clutches of the 9th range 35, 20 ;

    Which would be a half-distance of 21.

  2. Moving left you need to move to position 24 to escape the 1st range: 25, 1 ;

    Which is a half-distance of 11.

Which means the final half-distance for position 1 should be 11; not 8 as produced by the OP code.

You can work the other out for yourself.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Can I speed this up? (Algorithm error)
by daverave (Scribe) on Nov 03, 2010 at 20:11 UTC
    I think you misunderstood the question, probably due my poor explanation. You have to look both sides at the same time in a single range. You can't pick one range that has a far right side and another that has a far left side. In other words: for each position i we are looking for the (half) size of the minimal (odd-sized) window which is centered around i, and is not contained by any (single) range. You can't 'split' the containment between two ranges.

    Going back to the example of position 1:

    half size 0: there exists a range that covers 1,1
    half size 1: there exists a range that covers 35,2
    half size 2: there exists a range that covers 34,3
    ...
    half size 8: there exists a range that covers 28,9 (e.g. 28,10)
    half size 9: there is NO range that covers 27,10. DONE!

    I hope it's more clear now.

    (p.s. in your ASCII art it seems as if MAX=36, not 35, doesn't it?)

      Thanks for the clarification. Back to my drawing board :)

      (p.s. in your ASCII art it seems as if MAX=36, not 35, doesn't it?)

      No. The last vertical line of '|'s is just a border. Notice that there are no stars in in it.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.