2

Longest arithmetic progression of a set of numbers {ab1,ab2,ab3 .... abn} is defined as a subset {bb1,bb2,bb3 .... bbn} such that bi+1 - bi is constant.

I would like to extend this problems to a set of two dimensional points lying on a straight line. Lets define Dist(P1,P2) is the distance between two Points P1(X1,Y1) and P2(X2,Y2) on a line as

Dist(P1,P2) = Dist((X1,Y1),(X2,Y2)) = (X2 - X1)2 + (Y2 - Y1))2

Now For a given set of points I need to find the largest Arithmetic Progression such that Dist(Pi,Pi+1) is constant, assuming they all lie on the same line (m & C are constant).

I researched a bit but could not figure out an algorithm which is better than O(n2).

In fact currently the way I am doing is I am maintaining a Dictionary say

DistDict=dict()

and say Points are defined in a List as

Points = [(X1,Y1),(X2,Y2),....]

then this is what I am doing

for i,pi in enumerate(Points):
    for pj in Points[i+1:]:
         DistDict.setdefault(dist(pi,pj),set([])).add((pi,pj))

so all I have ended up with a dictionary of points which are of equal distance. So the only thing I have to do is to scan through to find out the longest set.

I am just wondering that this ought to have a better solution, but somehow I can't figure out one. I have also seen couple of similar older SO posts but none I can find to give something that is more efficient than O(n2). Is this somehow an NP Hard problem that we can never have something better or if not what approach could be take. Please note I came across a post which claims about an efficient divide and conquer algorithm but couldn't make any head or tail out of it.

Any help in this regard?

Note*** I am tagging this Python because I understand Python better than maybe Matlab or Ruby. C/C++/Java is also fine as I am somewhat proficient in these too :-)

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • 1
    if you have O(n^p) algorithm where p is constant, then this problem is not NP-hard assuming that P != NP – soulcheck Dec 20 '11 at 12:24

3 Answers3

1

You could study the Fast Fourier Transform methods for multiplication.O(N log N)

You might be able to do something similar with your problem.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
1

Firstly, your definition of distance is wrong. You have to take the square root. Secondly, if you know that all the points lie on a straight line, you can simply ignore the y-coordinates (unless the line is vertical) or the x-coordinates (unless the line is horizontal). Then it reduces to the problem in your first paragraph.

TonyK
  • 16,761
  • 4
  • 37
  • 72
  • 1
    I know the definition of distance is wrong. I just don't want to add an overhead of finding the square root which won't add any value but just to conform with the definition. Secondly I can strip of the y-coordinate but the points may line along X or Y axis. Also I just can't remember the term they call for square of a distance so just calling it distance for the sake. – Abhijit Dec 20 '11 at 12:18
  • Sorry I should have been more elaborate with my previous comment. Actually this is part of a larger problem, where there are some points on a plane that I have to group in a way that they are all linear and then I have to figure this out. Currently both of these I am doing in one iteration. – Abhijit Dec 20 '11 at 12:24
1

To sum things up: As @TonyK pointed out, if you assume that the points lie on a straight line, you can reduce it to the one-dimensional case that was discussed extensively here already. The solution uses Fast Fourier Transforms as mentioned by @YochaiTimmer.

Additional note: The problem is almost certainly not NP-hard as it has an efficient O(n log n) solution, so that would imply P=NP.

Community
  • 1
  • 1
Jan Pöschko
  • 5,412
  • 1
  • 28
  • 28
  • Thanks. I need some time to go through it and see if I can extend the concept beyond three evenly spaced points. – Abhijit Dec 20 '11 at 12:32
  • It is `O(n log n)` where `n` is the distance between the extreme points (assuming all distances are integers). But it is `O(n^2)` in the number of points. – TonyK Dec 20 '11 at 12:36
  • Just wondering if the said algorithm can be extended beyond 3 points. I am still brainstorming on that. If its O(n^2) for the number of points, do we get any advantage of using it, assuming the above algorithm I mentioned is actually O(n^2) where n is the number of points in the linear space. – Abhijit Dec 20 '11 at 13:19