🏗️ ΘρϵηΠατπ🚧 (under construction)

Intersecting Lines
Suppose P={(p1,1),,(pn,1)} and Q={(q1,0),,(qn,0)}. Suppose that p,pP and q,qQ, then we say that (p,q) intersects with (p,q) when either of the following two situations arise
  • p[1]p[1] and q[2]>q[2]
  • p[1]p[1] and q[2]>p[2]

Motivation & Correctness

Firstly note that P can be sorted to PS. For any pPS, let us denote the element in Q that forms a line with p by l(p), consider the tuple formed by l(PS):=(l(pα1),l(pα2),,l(pαn)), let us denote this tuple as Q, this is done so that for any index αi we note that pαi forms a line with qαi for

We remark that if Q happened to be sorted then there would be no crossings, this follows by the definition of a crossing. The dual of this situation is when we have αi<αj being indices into PS wherein qαi>qαj, the lines (pαi,qαi) and (pαj,qβj) must cross, this holds true because PS was sorted, which means that if αi<αj then we have pαi<pαj.

Observe that in the above remark, we've noted that for every instance of αi<αj such that qαi>qαj, we get exactly one line crossing between the two lines being defined. Therefore we've reduced this problem to counting the number of inversions in Q.

Since we have an algorithm that we have proven correct as well as showing that it's time complexity is O(nlog2n), we are able to conclude that by passing Q to count_inversions we know that this extended algorithm correctly returns the number of inversions in Q.

We also observe that the number of line crossings between PS and Q is exactly equal to the number of line crossings between P and Q, this can be seen because even though the order in which the points are observed has changed, the actual points included in the two sets doesn't, therefore the realize the same situation, so counting crossings in one yields the same answer as counting crossings in the other.

Runtime

We've proven that our algorithm is correct, we now turn to the time analysis.

Therefore the total runtime is O(nlog2n)

Pseudocode

        def count_line_crossings(P, Q):
            l = line_map(P, Q)
            P_s = sort(P)
            Q_prime = map(l, P_s)

            return count_inversions(Q_prime)
    

Note that line map can be thought of as a function which takes a point in P and takes it to it's corresponding point in Q it is connected to.