# Minimum Cost Polygon Triangulation

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, i) is cost(i, j, k) = dist(i, j) + dist(j, k) + dist(k, i)

Following is implementation of above naive recursive formula.

## C++

`// Recursive implementation for minimum cost convex polygon triangulation ` `#include <iostream> ` `#include <cmath> ` `#define MAX 1000000.0 ` `using` `namespace` `std; ` ` ` `// Structure of a point in 2D plane ` `struct` `Point ` `{ ` ` ` `int` `x, y; ` `}; ` ` ` `// Utility function to find minimum of two double values ` `double` `min(` `double` `x, ` `double` `y) ` `{ ` ` ` `return` `(x <= y)? x : y; ` `} ` ` ` `// A utility function to find distance between two points in a plane ` `double` `dist(Point p1, Point p2) ` `{ ` ` ` `return` `sqrt` `((p1.x - p2.x)*(p1.x - p2.x) + ` ` ` `(p1.y - p2.y)*(p1.y - p2.y)); ` `} ` ` ` `// A utility function to find cost of a triangle. The cost is considered ` `// as perimeter (sum of lengths of all edges) of the triangle ` `double` `cost(Point points[], ` `int` `i, ` `int` `j, ` `int` `k) ` `{ ` ` ` `Point p1 = points[i], p2 = points[j], p3 = points[k]; ` ` ` `return` `dist(p1, p2) + dist(p2, p3) + dist(p3, p1); ` `} ` ` ` `// A recursive function to find minimum cost of polygon triangulation ` `// The polygon is represented by points[i..j]. ` `double` `mTC(Point points[], ` `int` `i, ` `int` `j) ` `{ ` ` ` `// There must be at least three points between i and j ` ` ` `// (including i and j) ` ` ` `if` `(j < i+2) ` ` ` `return` `0; ` ` ` ` ` `// Initialize result as infinite ` ` ` `double` `res = MAX; ` ` ` ` ` `// Find minimum triangulation by considering all ` ` ` `for` `(` `int` `k=i+1; k<j; k++) ` ` ` `res = min(res, (mTC(points, i, k) + mTC(points, k, j) + ` ` ` `cost(points, i, k, j))); ` ` ` `return` `res; ` `} ` ` ` `// Driver program to test above functions ` `int` `main() ` `{ ` ` ` `Point points[] = {{0, 0}, {1, 0}, {2, 1}, {1, 2}, {0, 2}}; ` ` ` `int` `n = ` `sizeof` `(points)/` `sizeof` `(points[0]); ` ` ` `cout << mTC(points, 0, n-1); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Recursive implementation for minimum ` `# cost convex polygon triangulation ` `from` `math ` `import` `sqrt ` `MAX` `=` `1000000.0` ` ` `# A utility function to find distance ` `# between two points in a plane ` `def` `dist(p1, p2): ` ` ` `return` `sqrt((p1[` `0` `] ` `-` `p2[` `0` `])` `*` `(p1[` `0` `] ` `-` `p2[` `0` `]) ` `+` `\ ` ` ` `(p1[` `1` `] ` `-` `p2[` `1` `])` `*` `(p1[` `1` `] ` `-` `p2[` `1` `])) ` ` ` `# A utility function to find cost of ` `# a triangle. The cost is considered ` `# as perimeter (sum of lengths of all edges) ` `# of the triangle ` `def` `cost(points, i, j, k): ` ` ` `p1 ` `=` `points[i] ` ` ` `p2 ` `=` `points[j] ` ` ` `p3 ` `=` `points[k] ` ` ` `return` `dist(p1, p2) ` `+` `dist(p2, p3) ` `+` `dist(p3, p1) ` ` ` ` ` `# A recursive function to find minimum ` `# cost of polygon triangulation ` `# The polygon is represented by points[i..j]. ` `def` `mTC(points, i, j): ` ` ` ` ` `# There must be at least three points between i and j ` ` ` `# (including i and j) ` ` ` `if` `(j < i ` `+` `2` `): ` ` ` `return` `0` ` ` ` ` `# Initialize result as infinite ` ` ` `res ` `=` `MAX` ` ` ` ` `# Find minimum triangulation by considering all ` ` ` `for` `k ` `in` `range` `(i ` `+` `1` `, j): ` ` ` `res ` `=` `min` `(res, (mTC(points, i, k) ` `+` `\ ` ` ` `mTC(points, k, j) ` `+` `\ ` ` ` `cost(points, i, k, j))) ` ` ` ` ` `return` `round` `(res, ` `4` `) ` ` ` ` ` `# Driver code ` `points ` `=` `[[` `0` `, ` `0` `], [` `1` `, ` `0` `], [` `2` `, ` `1` `], [` `1` `, ` `2` `], [` `0` `, ` `2` `]] ` `n ` `=` `len` `(points) ` `print` `(mTC(points, ` `0` `, n` `-` `1` `)) ` ` ` `# This code is contributed by SHUBHAMSINGH10 ` |

*chevron_right*

*filter_none*

**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.

Following is C++ implementation of dynamic programming solution.

`// A Dynamic Programming based program to find minimum cost of convex ` `// polygon triangulation ` `#include <iostream> ` `#include <cmath> ` `#define MAX 1000000.0 ` `using` `namespace` `std; ` ` ` `// Structure of a point in 2D plane ` `struct` `Point ` `{ ` ` ` `int` `x, y; ` `}; ` ` ` `// Utility function to find minimum of two double values ` `double` `min(` `double` `x, ` `double` `y) ` `{ ` ` ` `return` `(x <= y)? x : y; ` `} ` ` ` `// A utility function to find distance between two points in a plane ` `double` `dist(Point p1, Point p2) ` `{ ` ` ` `return` `sqrt` `((p1.x - p2.x)*(p1.x - p2.x) + ` ` ` `(p1.y - p2.y)*(p1.y - p2.y)); ` `} ` ` ` `// A utility function to find cost of a triangle. The cost is considered ` `// as perimeter (sum of lengths of all edges) of the triangle ` `double` `cost(Point points[], ` `int` `i, ` `int` `j, ` `int` `k) ` `{ ` ` ` `Point p1 = points[i], p2 = points[j], p3 = points[k]; ` ` ` `return` `dist(p1, p2) + dist(p2, p3) + dist(p3, p1); ` `} ` ` ` `// A Dynamic programming based function to find minimum cost for convex ` `// polygon triangulation. ` `double` `mTCDP(Point points[], ` `int` `n) ` `{ ` ` ` `// There must be at least 3 points to form a triangle ` ` ` `if` `(n < 3) ` ` ` `return` `0; ` ` ` ` ` `// table to store results of subproblems. table[i][j] stores cost of ` ` ` `// triangulation of points from i to j. The entry table[0][n-1] stores ` ` ` `// the final result. ` ` ` `double` `table[n][n]; ` ` ` ` ` `// Fill table using above recursive formula. Note that the table ` ` ` `// is filled in diagonal fashion i.e., from diagonal elements to ` ` ` `// table[0][n-1] which is the result. ` ` ` `for` `(` `int` `gap = 0; gap < n; gap++) ` ` ` `{ ` ` ` `for` `(` `int` `i = 0, j = gap; j < n; i++, j++) ` ` ` `{ ` ` ` `if` `(j < i+2) ` ` ` `table[i][j] = 0.0; ` ` ` `else` ` ` `{ ` ` ` `table[i][j] = MAX; ` ` ` `for` `(` `int` `k = i+1; k < j; k++) ` ` ` `{ ` ` ` `double` `val = table[i][k] + table[k][j] + cost(points,i,j,k); ` ` ` `if` `(table[i][j] > val) ` ` ` `table[i][j] = val; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `table[0][n-1]; ` `} ` ` ` `// Driver program to test above functions ` `int` `main() ` `{ ` ` ` `Point points[] = {{0, 0}, {1, 0}, {2, 1}, {1, 2}, {0, 2}}; ` ` ` `int` `n = ` `sizeof` `(points)/` `sizeof` `(points[0]); ` ` ` `cout << mTCDP(points, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

15.3006

Time complexity of the above dynamic programming solution is O(n^{3}).

Please note that the above implementations assume that the points of covnvex polygon are given in order (either clockwise or anticlockwise)

**Exercise:**

Extend the above solution to print triangulation also. For the above example, the optimal triangulation is 0 3 4, 0 1 3, and 1 2 3.

**Sources:**

http://www.cs.utexas.edu/users/djimenez/utsa/cs3343/lecture12.html

http://www.cs.utoronto.ca/~heap/Courses/270F02/A4/chains/node2.html

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum area of a Polygon with three points given
- Minimum side of square embedded in Regular polygon with N sides
- Minimum cost to fill given weight in a bag
- Minimum odd cost path in a matrix
- Minimum cost to buy N kilograms of sweet for M persons
- Minimum cost of choosing the array element
- Minimum cost to build N blocks from one block
- Minimum cost to partition the given binary string
- Find minimum adjustment cost of an array
- Minimum cost to merge all elements of List
- Minimum Cost To Make Two Strings Identical
- Minimum cost path in a Matrix by moving only on value difference of X
- Minimum Cost to make two Numeric Strings Identical
- Minimum cost to reach the top of the floor by climbing stairs
- Minimum cost to select K strictly increasing elements
- Minimum cost to make a string free of a subsequence
- Find the minimum cost to reach destination using a train
- Minimum cost to reach a point N from 0 with two different operations allowed
- Minimum cost to form a number X by adding up powers of 2
- Minimum cost to sort strings using reversal operations of different costs