Search algorithm if triangles formed by a set of points contain an origin or not and give a general score?

Input: S = {p1, . . . , pn}, npoints on the 2D-plane, each point is defined by its coordinates x and y.

For simplicity, suppose:

The origin (0, 0) is NOT in S. Any line L passing through (0, 0) contains at most one point in S. No three points in S lie on the same line. If we select any three points from S, we can form a triangle. Thus, the total number of triangles that can be formed in this way is Θ (n ^ 3).

Some of these triangles contain (0, 0), some do not.

Task: calculate the number of triangles containing (0, 0).

You can assume that we have an O (1) time function Test(pi, pj , pk), which, given the three points pi, pj, pk in S, returns 1 if the triangle formed {pi, pj , pk}contains (0, 0) and returns 0 otherwise. Its trivial to solve the problem in Θ (n ^ 3) time (just list and check all the triangles).

Describe the algorithm for solving this problem with O (n log n) runtime.


My analysis of the above problem leads to the following conclusion

There are 4 coordinates (+, +), (+, -), (-, -), (-, +) {x and y coordinatesa> 0 or not}.

Let

  • s1 = coordinate x <0 and y> 0
  • s2 = x> 0, y> 0
  • s3 = x <0, y <0
  • s4 = x> 0, y <0

Now we need to test the points between the sets of the following combinations:

  • S1 S2 S3
  • S1 S1 S4
  • S2 S2 S3
  • S3 S3 S2
  • S1 S4 S4
  • S1 S3 S4
  • S1 S2 S4
  • S2 S3 S4

(, s1, s2 s3 ) , (0,0), ( ).

- ?

, , (s1, s2, s4) (0,0), (s1, s1, s3) .

Quadrants s1, s2, s3 and s4

+4
2

, ( ), , , , . n log n, , , Dynamic Programming/DaC.

. , .


-, . , , , .

Triangle containing origin

.

Triangle Vectors

, , , , .

Triangles from Origin

, , , .

, , , . , , " (0,0) S", , .


, , , , , , , , . , S , , .

, .

Normalized vectors form points along the unit circle.

.

The red arc defines all possible points that a triangle with two points can form.

, , , , , .


, :

  • S. A, (atan2 (x, y)) A (0 ≀ Ai ≀ 2Ο€). , O (n)

  • . O (n log n), , Merge Sort.

  • , (Ai, Aj). , Ai + Ο€ ≀ Ak ≀ Aj + Ο€. , , Ai + Ο€ Aj + Ο€, O (2 log n) = O (log n)

, n ^ 2 , O (log n), O (n ^ 2 log n). , .

Ai < Aj, , Tij , . , Ak > Aj, , Tij ≀ Tik, Ai + Ο€ Ak + Ο€ , Ai + Ο€ Aj + Ο€. , Ai + Ο€ Aj + Ο€, Aj + Ο€ Ak + Ο€. Ai + Ο€ Aj + Ο€, - Aj + Ο€ Ak + Ο€, . , :

A (n) = count (A (n), A (n-1)) + count (A (n-1), A (n-2)) +... + count (A (1), (0))

, n ^ 2 , - n-1.


, psuedocode.

int triangleCount(point P[],int n)
    int A[n], C[n], totalCount = 0;  

    for(i=0...n)
        A[i] = atan2(P[i].x,P[i].y);

    mergeSort(A);

    int midPoint = binarySearch(A,Ο€);

    for(i=0...midPoint-1)
        int left = A[i] + Ο€, right = A[i+1] + Ο€;
        C[i] = binarySearch(a,right) - binarySearch(a,left);
        for(j=0...i)
            totalCount += C[j]

return totalCount;
+2

, Θ (n 3), , , - , .

n, .

. , , k < n/2 , Θ (k). ; , , Θ (n 2), ( t20) Θ (n 3) ( 3 , ).

0

Source: https://habr.com/ru/post/1612863/


All Articles