<p>It seems to me that it is enough to consider tangent lines from the '-1' points onto the convex hull of the '+1' points as the candidates for the sides of $T$ (let's say that '+1' points will be inner to $T$).</p>

<p>Too bad, I can't publish images here. But picture this: $t$ is the tangent line to the convex hull that goes through some '-1' point. $A$ is the tangency point. $B$ is the extreme (see below) point on $t$, and $BC$ is the tangent line from the point $B$ ($C$ is the tangency point).</p>

<p>So, the algorithm is the following. For every line $t$ of the tangents lines we can try to build a triangle based on it:</p>

<ol>
<li>Compute the intersection points of $t$ with all the other lines;</li>
<li>Find an extreme (farthest from $A$) point $B$ and the corresponding line $t'$ to the right (or left) from $A$, such that the psuedotriangle $ABC$ (=$AB$, $BC$ and the part of the convex hull between $A$ and $C$) doesn't contain '-1' points (i.e. doesn't contain any points).</li>
<li>Do the same with the line $t'$ and see if we can 'close' the triangle.</li>
</ol>

<p>It looks like this would be a $O(m^2)$ running time. Maybe this can be improved by using some data structures?</p>

CC BY-SA 3.0
2012-05-31T20:44:55.367Z
2012-05-31T20:44:55.367Z
shalabuda
<p>As @TsuyoshiIto suggests, there is an $O(n\log n)$-time algorithm for this problem, due to Edelsbrunner and Preparata.  In fact, their algorithm finds a convex polygon with the minimum possible number of edges that separates the two point sets.  They also prove an $\Omega(n\log n)$ lower bound for the more general problem in the algebraic decision tree model; however, it's not clear whether this lower bound applies to the triangle case.</p>

<p>A full description of the algorithm is too long to post here, but here's the basic idea.  Let $C$ be the convex hull of the positive points.  For each negative point $q$, consider the lines through $q$ that are tangent to $C$.  These lines split the plane into four wedges, one of which contains $C$; let $W(q)$ be the wedge <em>opposite</em> the one that contains $C$.  Finally, let $F$ (the "forbidden region") be the union of all wedges $W(q)$.  Any separating triangle must separate $C$ from $F$.  Both $C$ and $F$ can be constructed in $O(n\log n)$ time.</p>

<p><img src="https://i.sstatic.net/n39yz.png" alt="example of $C$ and $F$"></p>

<p>Label the edges of $F$ alternately clockwise and counterclockwise.  Edelsbrunner and Preparata further prove that if a separating triangle exists, then there is a separating triangle whose edges are collinear with clockwise edges of $F$.  In $O(n)$ additional time, we can find the (necessarily clockwise) edge of $F$ first hit by a ray from each clockwise edge $e$; call this edge the "successor" of $e$.  The successor pointers partition the clockwise edges into cycles; if there is a separating triangle, one of these successor cycles has length 3 (and none have length more than 4).</p>

<p>See the original paper for more details:</p>

<ul>
<li>Herbert Edelsbrunner and Franco P. Preparata.  <a href="http://prophet.cs.duke.edu/~edels/Papers/1988-J-01-PolygonalSeparation.pdf" rel="nofollow noreferrer">Minimum polygonal separation</a>.  <em>Information and Computation</em> 77(3):218–232, 1988.</li>
</ul>

CC BY-SA 3.0
2012-06-01T07:24:44.717Z
2012-06-01T07:24:44.717Z
Jeffε
<p>I assigned my students the problem of finding a triangle consistent with a collection of $m$ points in $\mathbb{R}^2$, labeled with $\pm1$. (A triangle $T$ is <em>consistent</em> with the labeled sample if $T$ contains all of the positive and none of the negative points; by assumption, the sample admits at least 1 consistent triangle).</p>

<p>The best they (or I) could do is an algorithm that runs in time $O(m^6)$, where $m$ is sample size. Can anyone do better?</p>

CC BY-SA 3.0
2012-05-31T16:21:06.670Z
Just to be clear: the vertices of the triangle do not need to be points of the collection, right?  And it's acceptable to have negative points on the boundary?
ex0du5
CC BY-SA 3.0
2012-05-31T17:47:54.830Z
(1) I had voted to close the question because I had misunderstood the problem.  The system does not allow me to cancel my vote, but I virtually cancel it.  (2) I think that there is an O(m log m)-time algorithm, but do not have time to verify it right now.  The idea is to compute the convex hull of the positive examples and to sweep around this convex hull to find three lines forming the desired triangle.
Tsuyoshi Ito
CC BY-SA 3.0
2012-05-31T18:59:18.400Z
@ex0du5 -- indeed, the vertices of the triangle need not consist of the sample points. As for boundary issues, these can be ignored here as they are inessential. [If the boundary counts as part of the triangle, then you won't have negative points on the boundary.]
Aryeh
CC BY-SA 3.0
2012-05-31T19:17:30.423Z
@TsuyoshiIto: I was thinking similarly, but there are cases where you cannot have the triangle edges be collinear to the edges of the convex hull, but a triangle still exists.  The triangle still obviously contains the convex hull, but it's not just extending the lines of the hull and finding the triangle.  You may need lines that are rotated around some of the vertices to avoid the negative points.  That was why I asked about negatives on the boundary, to allow a search algorithm that chose lines from the vertices of the hull to negatives to keep it a discrete search.
ex0du5
CC BY-SA 3.0
2012-05-31T21:24:48.237Z
@ex0du5: Well, I did not assume that the edges of the triangle are parallel to some edges of the convex hull of the positive examples.
Tsuyoshi Ito
<cg.comp-geom><machine-learning><lg.learning><convex-geometry>
Learning triangles in the plane
Aryeh
# Learning triangles in the plane

(Asked by: username_0 on 2012-05-31)

I assigned my students the problem of finding a triangle consistent with a collection of $m$ points in $\mathbb{R}^2$, labeled with $\pm1$. (A triangle $T$ is *consistent* with the labeled sample if $T$ contains all of the positive and none of the negative points; by assumption, the sample admits at least 1 consistent triangle).

The best they (or I) could do is an algorithm that runs in time $O(m^6)$, where $m$ is sample size. Can anyone do better?

**Tags:** cg.comp-geom, machine-learning, lg.learning, convex-geometry

> **Comments:**
> **username_1**: Just to be clear: the vertices of the triangle do not need to be points of the collection, right?  And it's acceptable to have negative points on the boundary?  
> **username_2**: (1) I had voted to close the question because I had misunderstood the problem.  The system does not allow me to cancel my vote, but I virtually cancel it.  (2) I think that there is an O(m log m)-time algorithm, but do not have time to verify it right now.  The idea is to compute the convex hull of the positive examples and to sweep around this convex hull to find three lines forming the desired triangle.  
> **username_0**: @username_1 -- indeed, the vertices of the triangle need not consist of the sample points. As for boundary issues, these can be ignored here as they are inessential. [If the boundary counts as part of the triangle, then you won't have negative points on the boundary.]  
> **username_1**: @TsuyoshiIto: I was thinking similarly, but there are cases where you cannot have the triangle edges be collinear to the edges of the convex hull, but a triangle still exists.  The triangle still obviously contains the convex hull, but it's not just extending the lines of the hull and finding the triangle.  You may need lines that are rotated around some of the vertices to avoid the negative points.  That was why I asked about negatives on the boundary, to allow a search algorithm that chose lines from the vertices of the hull to negatives to keep it a discrete search.  
> **username_2**: @username_1: Well, I did not assume that the edges of the triangle are parallel to some edges of the convex hull of the positive examples.  

---

## 2 answers

### Answer by username_4 on 2012-06-01. Score: 14 | Accepted answer

As @TsuyoshiIto suggests, there is an $O(n\log n)$-time algorithm for this problem, due to Edelsbrunner and Preparata. In fact, their algorithm finds a convex polygon with the minimum possible number of edges that separates the two point sets. They also prove an $\Omega(n\log n)$ lower bound for the more general problem in the algebraic decision tree model; however, it's not clear whether this lower bound applies to the triangle case.

A full description of the algorithm is too long to post here, but here's the basic idea. Let $C$ be the convex hull of the positive points. For each negative point $q$, consider the lines through $q$ that are tangent to $C$. These lines split the plane into four wedges, one of which contains $C$; let $W(q)$ be the wedge *opposite* the one that contains $C$. Finally, let $F$ (the "forbidden region") be the union of all wedges $W(q)$. Any separating triangle must separate $C$ from $F$. Both $C$ and $F$ can be constructed in $O(n\log n)$ time.

![example of $C$ and $F$](https://i.sstatic.net/n39yz.png)

Label the edges of $F$ alternately clockwise and counterclockwise. Edelsbrunner and Preparata further prove that if a separating triangle exists, then there is a separating triangle whose edges are collinear with clockwise edges of $F$. In $O(n)$ additional time, we can find the (necessarily clockwise) edge of $F$ first hit by a ray from each clockwise edge $e$; call this edge the "successor" of $e$. The successor pointers partition the clockwise edges into cycles; if there is a separating triangle, one of these successor cycles has length 3 (and none have length more than 4).

See the original paper for more details:

* Herbert Edelsbrunner and Franco P. Preparata. [Minimum polygonal separation](http://prophet.cs.duke.edu/~edels/Papers/1988-J-01-PolygonalSeparation.pdf). *Information and Computation* 77(3):218–232, 1988.


---

### Answer by username_3 on 2012-05-31. Score: 3

It seems to me that it is enough to consider tangent lines from the '-1' points onto the convex hull of the '+1' points as the candidates for the sides of $T$ (let's say that '+1' points will be inner to $T$).

Too bad, I can't publish images here. But picture this: $t$ is the tangent line to the convex hull that goes through some '-1' point. $A$ is the tangency point. $B$ is the extreme (see below) point on $t$, and $BC$ is the tangent line from the point $B$ ($C$ is the tangency point).

So, the algorithm is the following. For every line $t$ of the tangents lines we can try to build a triangle based on it:

1. Compute the intersection points of $t$ with all the other lines;
2. Find an extreme (farthest from $A$) point $B$ and the corresponding line $t'$ to the right (or left) from $A$, such that the psuedotriangle $ABC$ (=$AB$, $BC$ and the part of the convex hull between $A$ and $C$) doesn't contain '-1' points (i.e. doesn't contain any points).
3. Do the same with the line $t'$ and see if we can 'close' the triangle.

It looks like this would be a $O(m^2)$ running time. Maybe this can be improved by using some data structures?


---

