How to find a closest integer point to intersection of two lines? - MathOverflow most recent 30 from http://mathoverflow.net 2013-05-19T11:52:09Z http://mathoverflow.net/feeds/question/22777 http://www.creativecommons.org/licenses/by-nc/2.5/rdf http://mathoverflow.net/questions/22777/how-to-find-a-closest-integer-point-to-intersection-of-two-lines How to find a closest integer point to intersection of two lines? shvedsky 2010-04-27T20:38:08Z 2011-06-15T11:18:15Z <p>Hello. Here's a <a href="http://stackoverflow.com/questions/2708615/given-two-lines-on-a-plane-how-to-find-integer-points-closest-to-their-intersect">question that originates from StackOverflow</a> (and the SO crowd isn't really qualified to solve it).</p> <p>We're given two lines on the plane, each of them has at least two integer points (i.e. points $(b,a)$ where $b$ and $a$ are in $\mathbb{Z}$). The lines aren't parallel, but their intersection is not necessarily in an integer point. We also are given a point $(x,y)$ (also integer) that doesn't belong to both lines.</p> <p>The problem is to find a point $(x',y')$ in the same quarter as $(x,y)$ (as separated by the lines) closest to intersection of the lines. It may be $(x,y)$ itself, but most likely it's closer.</p> <p>The $(x,y)$ point is seemingly needed just to make the problem NP-complete instead of NP-hard. The problem is claimed to have a polynomial solution, but I doubt that it really persists. Could anyone solve it or provide a proof that it's NP-complete?</p> http://mathoverflow.net/questions/22777/how-to-find-a-closest-integer-point-to-intersection-of-two-lines/22779#22779 Answer by anonymous for How to find a closest integer point to intersection of two lines? anonymous 2010-04-27T21:03:06Z 2010-04-27T21:09:22Z <p>Doesn't the following work? UPDATE: Sorry, no it doesn't. I missed the requirement that the lattice point be on the correct side of the two lines.</p> <p>Find the exact coordinates, $(u,v)$ of the intersection. These are rational numbers, and can be found by solving two linear equations. Then the nearest lattice point is <code>$(\mathrm{ROUND}(u), \mathrm{ROUND}(v))$</code> where $\mathrm{ROUND}$ rounds to the nearest integer. </p> <p>To see that this is the closest point, translate <code>$(\mathrm{ROUND}(u), \mathrm{ROUND}(v))$</code> to the origin. So we need to show that, if $|u|$ and $|v|$ are $&lt; 1/2$, then $(u,v)$ is closer to $(0,0)$ than to any other lattice point $(a,b)$. The cases of $(a,b) = (0, \pm 1)$, $(\pm 1, 0)$ or $(\pm 1, \pm 1)$ can be checked by hand. For any other $a$, $b$, one of $|u-a|$ and $|v-b|$ is greater than $1$, so the distance from $(u,v)$ to $(a,b)$ is at least $1$; which is much greater than $\sqrt{u^2+v^2} \leq \sqrt{2}/2$.</p> <p>For a formal complexity analysis, the size of your input is the log of the number of digits needed to specify the two lines. All the arithmetic operations need to solve linear equations can be done in time polynomial in this logarithm.</p> <p>This question is much more interesting for lattices other than the square grid. See <a href="http://en.wikipedia.org/wiki/Voronoi_decomposition" rel="nofollow">Voronoi decomposition</a> for details.</p> http://mathoverflow.net/questions/22777/how-to-find-a-closest-integer-point-to-intersection-of-two-lines/22915#22915 Answer by Victor Miller for How to find a closest integer point to intersection of two lines? Victor Miller 2010-04-29T00:46:41Z 2010-05-02T17:32:51Z <p>I don't believe that the problem is NP-complete, because you're working in a fixed dimension. Since most of us believe that the hardest case is when the angle between the two lines is very small, then you can take the line L half way in between the two lines, and orthogonally project onto it for the objective function. Then we have an integer programming problem in 2 dimensions -- the two inequalities specify the proper side of the two lines. Hendrik Lenstra in "Integer Programming with a fixed number of variables" in Mathematics of Operations Research, showed that when the dimension is fixed there is a polynomial time algorithm for IP (using a variant of the L^3 lattice basis reduction). There's also the paper <a href="http://www.math.uni-klu.ac.at/or/doctoralschool/deloera.pdf" rel="nofollow">http://www.math.uni-klu.ac.at/or/doctoralschool/deloera.pdf</a> "Integer Polynomial Optimization in Fixed Dimension" which mentions that a convex polynomial objective function also has a polynomial time algorithm in fixed dimension, so that should do it for this problem.</p> <p>[Added Comments] Using the two papers that I mentioned in my comments <a href="http://mpi-inf.mpg.de/~soeren/pubs/2ip.ps" rel="nofollow">http://mpi-inf.mpg.de/~soeren/pubs/2ip.ps</a> <a href="http://homepages.cwi.nl/~aardal/journal_rev.ps" rel="nofollow">http://homepages.cwi.nl/~aardal/journal_rev.ps</a> one can proceed as follows: We're going to have upper and lower supporting lines orthogonal to the midpoint line L. These will always have the property that we know that there is at least one lattice point in the quadrilateral bounded by the the original lines and the supporting line (though at the beginning, it has degenerated into a triangle). At the beginning the lower supporting line is the one passing through the intersection of the two original lines. The upper supporting line can be calculated by noticing that any disk of radius > sqrt(2)/2 must contain a lattice point, so you can find the smallest distance along the midline where you can place the center of such a disk -- it will be where the line from center orthogonal to each of the original lines has distance sqrt(2)/2. By simple trigonometry you can see that if the number of bits in the original number is N, then we need at most 2N bits to specify this point (i.e. if the denominators are around n for the originals, then the denominators for the above points are at most n^2). Now use the algorithm in first paper which tells you in time linear in the number of bits of the problem whether or not there are any lattice points in the quadrilateral. Do a binary search by looking at the a test line half way in between the supporting lines, and testing each of the two quadrilaterals. After only about N steps (remember that N is the log of the coefficients) you'll be down to a quadrilateral with a small area and width. At that point you can quickly enumerate all lattice points in it and test them for the minimum. This algorithm probably runs in time O(N^2) where N is the number of bits in the original coefficients.</p> <p>[another addition to simplify things]:</p> <p>The idea in solving the problem is to do the following:</p> <p>1) Find a good enough approximation to the minimum distance, and the lattice point attaining that, so that you can enclose that region in a rectangle whose sides are parallel to the $x$ and $y$ axes, with a small enough area, $A$ and perimeter $P$. It's easy to see that you can enumerate all lattice points inside of such a rectangle in time $A+P$, and then check those to see which ones give you the minimum. [Changing notation] Let $n$ be the number of bits to specify the problem and <code>$N=2^n$</code>. Let the angle between the two lines be denoted by $2 \theta$, and the midline by $L$.</p> <p>2) As I mentioned, any disk with radius <code>$&gt; \sqrt{2}/2$</code> must contain a lattice point. We use this by placing such a disk in between the two lines, and as close to the point of intersection as possible. We see that the distance from the intersection point to the center of the disk which just fits, is <code>$\sqrt{2}/2 \csc \theta$</code>. If $\theta \ge \pi/6$ (say) we can enclose the whole triangle bounded by the lines and the tangent line to the disk orthogonal to $L$ in a rectangle of constant size and perimeter, so we just try all of those points. Note that in any case <code>$\theta \ge c/N$</code> for some absolute constant $c$ since when $\theta$ is small enough <code>$\cos \theta \approx 1 - \theta^2/2$</code>, and $\cos 2 \theta$ is given by a dot product between the coefficients of the lines and so has at most $2n$ bits.</p> <p>3) Otherwise we call the algorithm described in <a href="http://mpi-inf.mpg.de/~soeren/pubs/2ip.ps" rel="nofollow">http://mpi-inf.mpg.de/~soeren/pubs/2ip.ps</a> just once, with the objective function the dot product between $(x,y)$ and a vector parallel to $L$, and the four constraints: between the two lines, and the dot product with $L$ is $\ge 0$ and $\le$ the bound we get by placing the disk. Because $\theta$ is small enough and bounded away from 0, we can now draw a rectangle enclosing the point produced by the algorithm whose area and perimeter are bounded, independent of the number bits, which must contain the answer, and we again enumerate all points in that rectangle.</p> http://mathoverflow.net/questions/22777/how-to-find-a-closest-integer-point-to-intersection-of-two-lines/22951#22951 Answer by Robby McKilliam for How to find a closest integer point to intersection of two lines? Robby McKilliam 2010-04-29T07:24:50Z 2011-06-15T11:18:15Z <p>If the point $(x,y)$ is in an obtuse quadrant between the lines the problem is easily solved by enumerating the lattice points in a closed sphere of radius $\sqrt{2}$ about the intersection point so I will only consider the case that $(x,y)$ lies in a acute quadrant.</p> <p>The problem can be converted to one that is very similar to inhomogeneous Diophantine approximation. I know of at least one algorithm that will definitely find the correct answer in $O\left(\tfrac{1}{\theta}\right)$ operations where $2\theta$ is the acute angle between the lines. The algorithm is a minor modification of that given on page 19 of <a href="http://www.itee.uq.edu.au/~vaughan/Publications/thesis.pdf" rel="nofollow">Vaughan Clarkson's thesis</a>. I am very confident that Cassels' algorithm (or a minor modification of it) will solve the problem in $O\left(1 + \log\tfrac{1}{\theta}\right)$ operations (page 34 of <a href="http://www.itee.uq.edu.au/~vaughan/Publications/thesis.pdf" rel="nofollow">1</a>), but I am not quite sure how to show it, or what modification needs to be made. Before I describe this, you need to know a little bit about inhomogeneous Diophantine approximation.</p> <p>Let $\alpha$ and $\beta$ be real numbers. Define the function</p> <p>$F(p,q) = |\alpha p - q - \beta|$.</p> <p>The problem of inhomogeneous Diophantine approximation involves minimising $F(p,q)$ over integers $p$ and $q$ where $q$ is positive. The pair $(p, q)$ is called a <em>best approximation</em> if $F(p,q) &lt; F(p',q')$ for all $q' &lt; q$. The best approximations describe the minima found as $q$ and the magnitude of $p$ increase. There are various algorithms than can enumerate all of the best approximations. Two examples are the 'naive algorithm' (page 19 of <a href="http://www.itee.uq.edu.au/~vaughan/Publications/thesis.pdf" rel="nofollow">1</a>) and Cassel's algorithm (page 34 of <a href="http://www.itee.uq.edu.au/~vaughan/Publications/thesis.pdf" rel="nofollow">1</a>). The OP's problem is not exactly the same as this, but it is so similar that the algorithms (at least the naive algorithm) carry over.</p> <p>Let our two lines be $\ell_1$ and $\ell_2$ and let $2\theta &lt; \pi/2$ be the angle between them. It will be easier to describe the approach if we set the intersection of these lines to be at the origin and we look for the nearest point in the translated lattice $\mathbb{Z}^2 + t$ where $t$ is the appropriate translation. Define $\ell$ to be the unique line that passes through the origin and bisects the acute angle between $\ell_1$ and $\ell_2$. The angle between $\ell$ and $\ell_1$ and $\ell$ and $\ell_2$ is $\theta$. The problem can now be stated as:</p> <blockquote> <p>Find the point $x \in \mathbb{Z}^2 + t$ that is nearest to the origin such that the angle between $x$ and $\ell$ is less than or equal to $\theta$</p> </blockquote> <p>Let $A(x)$ denote the angle between $\ell$ and $x$. Our motivation is now very similar to that in Diophantine approximation. That is, find all of the best approximations for $A(x)$, our problem is solved by the best approximation that first yields $A(x) \leq \theta$. It so happens that $A(x)$ is a very similar function to $F(p,q)$. To give this some context I will say that $F(p,q)$ is, in a sense, computing an <em>inner product</em> between two vectors, whereas $A(x)$ is computing an <em>angle</em>. In this context it is not surprising that the algorithms for Diophantine approximation can be used. </p> <p>I will only consider the 'naive algorithm' and I'll just give some geometric insight as to its functionality, this should be enough to convince most people. Working through this rigorously is really beyond a typed answer on MO, but all the required machinery is in <a href="http://www.itee.uq.edu.au/~vaughan/Publications/thesis.pdf" rel="nofollow">1</a>. The 'naive algorithm' enumerates every point in $\mathbb{Z}^2 + t$ that is a nearest lattice point to any point in the line $\ell$. In other words it consecutively locates (starting from the origin) every lattice point in $\mathbb{Z}^2 + t$ whose Voronoi cell (in this case squares) intersect $\ell$. A picture might be useful</p> <p><img src="http://robbymckilliam.github.com/pictures/linemo.png" alt="alt text"></p> <p>It is not difficult to devise an algorithm which does this, just start at the origin and check where $\ell$ next crosses a boundary of a Voronoi cell. It is also easy to see that the points it locates are a super set of the best approximations for $A(x)$. The first point that the algorithm finds such that $A(x) &lt; \theta$ is the solution to the problem (the blue circle).</p> <p>This algorithm is called 'naive' because it checks a lot of lattice points that are not best approximations. Cassels' algorithm improves this substantially for the function $F(p,q)$. It's likely that a similar improvement is possible for $A(x)$ and someone might wish to work it out.</p> <p>The OP (particularly on stack overflow, but also here) seems to have thrown quite a number of red herrings into the problem statement (rather annoying). For example, knowledge of the point (x,y) does nothing other than tell you which quadrant you are looking in. The statement about it converting the problem to NP-complete rather than NP-hard doesn't make any sense. Also, the fact that the lines pass through integer points appears to be irrelevant.</p>