Maximum Number of Collinear Points

Source: Asked to me by a friend - who was asked this question in an interview at Facebook

Problem:

Given n points on a 2D plane, find the equation of the line with maximum number of collinear points. What is the time complexity of your algorithm?








Solution:

Highlight the part between the * symbols for the answer.
* O(n^2) algorithm: the optimal line passes through at least two of the points (otherwise rotate/translate it until it does without losing collinear points). So for each point p, compute the slope from p to every other point and tally frequencies in a hash map (use exact rational slopes dy/dx reduced by gcd, plus a sentinel for vertical lines - avoid floating point). The most frequent slope from p, plus p itself, gives the best line through p. Take the maximum over all p.

Complexity: n points x O(n) slope computations and hash updates = O(n^2) expected time, O(n) space per round. (Deterministic O(n^2 log n) via sorting slopes instead of hashing.) This is worst-case optimal up to log factors - the problem is 3SUM-hard.

Solution by piyushmahesh (per-point slope tally) and an Anonymous commenter (per-pair line hashing) from the comments.
*

Comments

  1. 1) Take any point x and find the slope from x to every other point - O(n)
    2) Find the number of points for each slope by hashing or building a binary search tree - O(nlogn)
    3) Find the max number of point for any slope
    4) Repeat this for every point and find the max among them

    Total time complexity - O(n*n*logn)

    ReplyDelete
  2. a line is defined by the equation ax+b = y;

    Initialize a hashmap with key (a,b) and an integer value;

    for each edge of two points :
    compute a,b and increment the integer value associated with (a,b) in the hashmap
    O(n*n)

    compute max of the values in the hashmap and return key associated
    O(n*n)

    Total time complexity O(n*n)

    ReplyDelete
    Replies
    1. Your complexity analysis assumes that HashMap could be updated in O(1). So, overall complexity should be expected O(n*n).

      Delete
  3. a line is defined by its equation ax+b = y;

    Initialize a hashmap with key (a, b) and integer value

    for each edge of two points, compute a and b,
    increment the value associated with key (a,b)
    O(n*n)

    compute the max of values in the hashmap and return key associated
    O(n*n)

    Total time complexity : O(n*n)

    ReplyDelete
  4. Can we use something like this ?

    Sort the points based on x and y coordinates.

    Then basically use DP approach to get the longest increasing sequence with the same slope

    ReplyDelete
    Replies
    1. Replying to Unknown's comment, Can we use something like this ?Sort the points based on x and y coordinates. - Sorting does not linearize the problem: equal CONSECUTIVE slopes after sorting by x do certify collinearity, but a DP must track the slope against every previous point - the state space is O(n) slopes per element and you end up at O(n^2) states with expensive transitions. The slope-hash per anchor point is simpler and optimal at expected O(n^2). (Reply created using AI)

      Delete

Post a Comment

Popular posts from this blog

Lion and Man in a Circular Cage: Can the Lion Catch the Tamer?

Expected Tosses for Consecutive Heads: 2 Heads vs 3 Heads Puzzle

Buying Dimsums: The Chicken McNugget (Frobenius Number) Puzzle