11
1
The Problem: Count the number of holes in a connected polygon. Connectivity of the polygon is guaranteed by the condition that every triangle in the input triangulation shares at least 1 side with another triangle and that there is only one such connected set of triangles.
Input is a list L
of n
points in the plane and a list T
of 3-tuples with entries from 0...n-1
. For each item in T
the tuple (t_1,t_2,t_3)
represents the three vertices (from the list L
) of a triangle in the triangulation. Note that this is a triangulation in the sense of 'polygon triangulation', because of this there will never be two triangles in T
that overlap. An additional stipulation is that you will not have to sanitize the input, L
and T
do not contain any repeats.
Example 1: If L = {{0,0},{1,0},{0,1},{1,2}}
and T = {{0,1,2},{1,2,3}}
then the polygon specified has a hole count of 0.
Example 2: If L = {{0,0},{1,0},{2,0},{2,1},{2,2},{1,2},{0,2},{0,1},{.5,.5},{1.5,.5},{1.5,1.5},{.5,1.5}}
and T = {{5,6,11},{5,10,11},{4,5,10},{3,8,10},{2,3,9},{2,8,9},{1,2,8},{0,1,8},{0,8,11},{0,7,11},{6,7,11},{3,4,10}}
then the polygon input should result in an output of 2.
The task is to write the shortest program (or function) that takes L
and T
as input and returns the number of holes. The 'winner' will be recognized as the entry with the least character count (tentative end date 1st of June).
Sample input formatting (note the 0 indexing):
0,0
1,0
0,1
1,2
0,1,2
1,2,3
1"Connectivity of the polygon is guaranteed by the condition that every triangle in the input triangulation shares at least 1 side with another triangle." -- no. That's not a sufficient condition. Take, for example,
T=1,2,3/1,2,4/5,6,7/5,6,8
. Every triangle shares an edge with another triangle, but the triangulation is disconnected – John Dvorak – 2013-05-19T06:40:09.150May we assume the input represents a valid partial triangulation (no two triangles overlap and no triangle is present twice) and the triangulation is connected? – John Dvorak – 2013-05-19T06:43:21.070
Note that the number of holes is not the genus of the shape. Every subset of plane has a genus of 0. "The genus of a connected, orientable surface is an integer representing the maximum number of cuttings along non-intersecting closed simple curves without rendering the resultant manifold disconnected"
– John Dvorak – 2013-05-19T06:47:29.070May we also assume the input is edge-connected in the sense it is not possible to remove a finite set of points to render the shape disconnected? (ex:
T=1,2,3/1,4,5
is connected but not edge-connected) – John Dvorak – 2013-05-19T06:55:22.880Thank you for the clarification questions Jan. As a summary: (1) Yes, there will be neither overlap nor repetition (2) Yes, you may assume input is edge-connected in the sense prescribed (3) 'genus' is not strictly applicable I agree: should I strike usage of it in favor of "number of holes"? – Kaya – 2013-05-19T07:19:50.490
Alternatively I could claim that the exterior of the polygon (out to infinity in all directions) is also considered a face in the complex and then 'genus' would be valid, no? – Kaya – 2013-05-19T07:25:29.763
Genus would still be invalid. Any closed curve separates the region in two or more regions - the genus is zero. Filling a circular (or polygonal) hole by a disk (a circular - or polygonal - region of the plane) doesn't change the genus in any way. Only if the triangles didn't lie in a plane, the genus could be non-zero. – John Dvorak – 2013-05-19T11:39:51.443
May I assume the points are not duplicated (in L) either? :-) – John Dvorak – 2013-05-19T11:41:55.690
Affirmative, this is the case. – Kaya – 2013-05-19T14:58:34.217
There's nothing wrong with self-answers in general on stackExchange, and there's nothing wrong with self-answers here (except it's kinda discouraging if the optimal solution appears right away with the question. But then it's about not posting the answer right away - but you can still declare its length and see if someone else finds it or beats it). Moreover, the comment formatting has broken your indentation. – John Dvorak – 2013-05-20T03:49:57.310
? "The 'winner' will be recognized as the entry with the least character count (tentative end date 1st of April)" – DavidC – 2013-05-20T15:01:15.140
+1 The more I look into this question, the greater my appreciation for it. I now realize that it can be solved regardless of the size of the holes. – DavidC – 2013-05-20T15:19:24.083
Can the vertex of one triangle appear mid-edge on another? – Keith Randall – 2013-05-20T15:26:55.927
@KeithRandall Technically if the input is supposed to be a valid triangulation then the presence of a vertex 'mid-edge' on would create a 4 sided face. The presence of such a face in the triangulation should not interfere with topological or visual techniques for counting holes, but if it is relevant to your solution you may assume that this will not occur in the input. – Kaya – 2013-05-20T17:16:05.267
@DavidCarraher Is this not a satisfactory 'win-condition' for code-golf? I wondered when writing this question if 'char-count' as the goal might not be unfair. – Kaya – 2013-05-20T17:19:32.517
The condition is fine. It's the date I was referring to. (April 1) – DavidC – 2013-05-20T17:45:23.733
I think my brain preformed a -- when I wanted a ++. Thanks. – Kaya – 2013-05-20T17:48:32.077
2
I'm not sure why this business about end dates has started cropping up recently. You're allowed to change the accepted answer, so there's no need to set an end date. It's reasonable to have a mental idea that you'll wait a week before selecting an answer in order not to scare people into thinking that the first answer is unbeatable, but as long as you're active on the site you can change the selected answer if someone posts a better one. Relevant meta discussions include http://meta.codegolf.stackexchange.com/q/542/194 and http://meta.codegolf.stackexchange.com/q/193/194
– Peter Taylor – 2013-05-21T09:20:26.763Can we assume that each list in
T
is sorted? – Volatility – 2013-05-22T10:24:10.180@Volatility I'm going to say no to that. While when I constructed the examples I numbered my triangles from lowest index vertex to highest, I think it would be more likely for the 'author' of a triangulation to maintain constant orientation within the triangles instead of switching to accomodate the names of the vertices. – Kaya – 2013-05-22T15:13:10.343