A triangulation of a convex polygon is formed by drawing diagonals between non-adjacent vertices (corners) such that the diagonals never intersect. The problem is to find the cost of triangulation with the minimum cost. The cost of a triangulation is sum of the weights of its component triangles. Weight of each triangle is its perimeter (sum of lengths of all sides)
See following example taken from this source.
Two triangulations of the same convex pentagon. The triangulation on the left has a cost of 8 + 2√2 + 2√5 (approximately 15.30), the one on the right has a cost of 4 + 2√2 + 4√5 (approximately 15.77).
This problem has recursive substructure. The idea is to divide the polygon into three parts: a single triangle, the sub-polygon to the left, and the sub-polygon to the right. We try all possible divisions like this and find the one that minimizes the cost of the triangle plus the cost of the triangulation of the two sub-polygons.
Let Minimum Cost of triangulation of vertices from i to j be minCost(i, j) If j <= i + 2 Then minCost(i, j) = 0 Else minCost(i, j) = Min { minCost(i, k) + minCost(k, j) + cost(i, k, j) } Here k varies from 'i+1' to 'j-1' Cost of a triangle formed by edges (i, j), (j, k) and (k, j) is cost(i, j, k) = dist(i, j) + dist(j, k) + dist(k, j)[ad type=”banner”]
Following is C++ implementation of above naive recursive formula.
Output:
15.3006
The above problem is similar to Matrix Chain Multiplication. The following is recursion tree for mTC(points[], 0, 4).
It can be easily seen in the above recursion tree that the problem has many overlapping subproblems. Since the problem has both properties: Optimal Substructure and Overlapping Subproblems, it can be efficiently solved using dynamic programming.
[ad type=”banner”]Following is C++ implementation of dynamic programming solution.
Output:
15.3006
Time complexity of the above dynamic programming solution is O(n3).
[ad type=”banner”]