{"id":27200,"date":"2018-01-03T22:18:57","date_gmt":"2018-01-03T16:48:57","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27200"},"modified":"2018-01-03T22:18:57","modified_gmt":"2018-01-03T16:48:57","slug":"minimum-cost-polygon-triangulation","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/minimum-cost-polygon-triangulation\/","title":{"rendered":"Minimum Cost Polygon Triangulation"},"content":{"rendered":"<p>A triangulation of a convex polygon is formed by drawing diagonals between non-adjacent vertices (corners) such that the diagonals never intersect. <span id=\"more-131581\"><\/span>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)<\/p>\n<p>See following example taken from this source.<img fetchpriority=\"high\" decoding=\"async\" class=\"alignright wp-image-27203 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Polynomial-Triangular.png\" alt=\"Polynomial Triangular\" width=\"378\" height=\"161\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Polynomial-Triangular.png 378w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Polynomial-Triangular-300x128.png 300w\" sizes=\"(max-width: 378px) 100vw, 378px\" \/><\/p>\n<p><em>Two triangulations of the same convex pentagon. The triangulation on the left has a cost of 8 + 2\u221a2 + 2\u221a5 (approximately 15.30), the one on the right has a cost of 4 + 2\u221a2 + 4\u221a5 (approximately 15.77).<\/em><\/p>\n<p>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.<\/p>\n<pre>Let Minimum Cost of triangulation of vertices from i to j be minCost(i, j)\r\nIf j &lt;= i + 2 Then\r\n  minCost(i, j) = 0\r\nElse\r\n  minCost(i, j) = Min { minCost(i, k) + minCost(k, j) + cost(i, k, j) }\r\n                  Here k varies from 'i+1' to 'j-1'\r\n\r\nCost of a triangle formed by edges (i, j), (j, k) and (k, j) is \r\n  cost(i, j, k)  = dist(i, j) + dist(j, k) + dist(k, j)<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C++ implementation of above naive recursive formula.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ Recursive implementation for minimum cost convex polygon triangulation<br\/>#include &lt;iostream&gt;<br\/>#include &lt;cmath&gt;<br\/>#define MAX 1000000.0<br\/>using namespace std;<br\/> <br\/>\/\/ Structure of a point in 2D plane<br\/>struct Point<br\/>{<br\/>    int x, y;<br\/>};<br\/> <br\/>\/\/ Utility function to find minimum of two double values<br\/>double min(double x, double y)<br\/>{<br\/>    return (x &lt;= y)? x : y;<br\/>}<br\/> <br\/>\/\/ A utility function to find distance between two points in a plane<br\/>double dist(Point p1, Point p2)<br\/>{<br\/>    return sqrt((p1.x - p2.x)*(p1.x - p2.x) +<br\/>                (p1.y - p2.y)*(p1.y - p2.y));<br\/>}<br\/> <br\/>\/\/ A utility function to find cost of a triangle. The cost is considered<br\/>\/\/ as perimeter (sum of lengths of all edges) of the triangle<br\/>double cost(Point points[], int i, int j, int k)<br\/>{<br\/>    Point p1 = points[i], p2 = points[j], p3 = points[k];<br\/>    return dist(p1, p2) + dist(p2, p3) + dist(p3, p1);<br\/>}<br\/> <br\/>\/\/ A recursive function to find minimum cost of polygon triangulation<br\/>\/\/ The polygon is represented by points[i..j].<br\/>double mTC(Point points[], int i, int j)<br\/>{<br\/>   \/\/ There must be at least three points between i and j<br\/>   \/\/ (including i and j)<br\/>   if (j &lt; i+2)<br\/>      return 0;<br\/> <br\/>   \/\/ Initialize result as infinite<br\/>   double res = MAX;<br\/> <br\/>   \/\/ Find minimum triangulation by considering all<br\/>   for (int k=i+1; k&lt;j; k++)<br\/>        res = min(res, (mTC(points, i, k) + mTC(points, k, j) +<br\/>                        cost(points, i, k, j)));<br\/>   return  res;<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    Point points[] = {{0, 0}, {1, 0}, {2, 1}, {1, 2}, {0, 2}};<br\/>    int n = sizeof(points)\/sizeof(points[0]);<br\/>    cout &lt;&lt; mTC(points, 0, n-1);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>15.3006<\/pre>\n<p>The above problem is similar to Matrix Chain Multiplication. The following is recursion tree for mTC(points[], 0, 4).<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-27204 aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Polygonal-Triangular-Solution.png\" alt=\"Polygonal Triangular Solution\" width=\"533\" height=\"341\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Polygonal-Triangular-Solution.png 533w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Polygonal-Triangular-Solution-300x192.png 300w\" sizes=\"(max-width: 533px) 100vw, 533px\" \/><\/p>\n<p>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.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C++ implementation of dynamic programming solution.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A Dynamic Programming based program to find minimum cost of convex<br\/>\/\/ polygon triangulation<br\/>#include &lt;iostream&gt;<br\/>#include &lt;cmath&gt;<br\/>#define MAX 1000000.0<br\/>using namespace std;<br\/> <br\/>\/\/ Structure of a point in 2D plane<br\/>struct Point<br\/>{<br\/>    int x, y;<br\/>};<br\/> <br\/>\/\/ Utility function to find minimum of two double values<br\/>double min(double x, double y)<br\/>{<br\/>    return (x &lt;= y)? x : y;<br\/>}<br\/> <br\/>\/\/ A utility function to find distance between two points in a plane<br\/>double dist(Point p1, Point p2)<br\/>{<br\/>    return sqrt((p1.x - p2.x)*(p1.x - p2.x) +<br\/>                (p1.y - p2.y)*(p1.y - p2.y));<br\/>}<br\/> <br\/>\/\/ A utility function to find cost of a triangle. The cost is considered<br\/>\/\/ as perimeter (sum of lengths of all edges) of the triangle<br\/>double cost(Point points[], int i, int j, int k)<br\/>{<br\/>    Point p1 = points[i], p2 = points[j], p3 = points[k];<br\/>    return dist(p1, p2) + dist(p2, p3) + dist(p3, p1);<br\/>}<br\/> <br\/>\/\/ A Dynamic programming based function to find minimum cost for convex<br\/>\/\/ polygon triangulation.<br\/>double mTCDP(Point points[], int n)<br\/>{<br\/>   \/\/ There must be at least 3 points to form a triangle<br\/>   if (n &lt; 3)<br\/>      return 0;<br\/> <br\/>   \/\/ table to store results of subproblems.  table[i][j] stores cost of<br\/>   \/\/ triangulation of points from i to j.  The entry table[0][n-1] stores<br\/>   \/\/ the final result.<br\/>   double table[n][n];<br\/> <br\/>   \/\/ Fill table using above recursive formula. Note that the table<br\/>   \/\/ is filled in diagonal fashion i.e., from diagonal elements to<br\/>   \/\/ table[0][n-1] which is the result.<br\/>   for (int gap = 0; gap &lt; n; gap++)<br\/>   {<br\/>      for (int i = 0, j = gap; j &lt; n; i++, j++)<br\/>      {<br\/>          if (j &lt; i+2)<br\/>             table[i][j] = 0.0;<br\/>          else<br\/>          {<br\/>              table[i][j] = MAX;<br\/>              for (int k = i+1; k &lt; j; k++)<br\/>              {<br\/>                double val = table[i][k] + table[k][j] + cost(points,i,j,k);<br\/>                if (table[i][j] &gt; val)<br\/>                     table[i][j] = val;<br\/>              }<br\/>          }<br\/>      }<br\/>   }<br\/>   return  table[0][n-1];<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    Point points[] = {{0, 0}, {1, 0}, {2, 1}, {1, 2}, {0, 2}};<br\/>    int n = sizeof(points)\/sizeof(points[0]);<br\/>    cout &lt;&lt; mTCDP(points, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>15.3006<\/pre>\n<p>Time complexity of the above dynamic programming solution is O(n<sup>3<\/sup>).<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Minimum Cost Polygon Triangulation &#8211; Dynamic Programming A triangulation of a convex polygon is formed by drawing diagonals between non-adjacent vertices<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70145,83848],"tags":[69992,72848,81063,81060,81070,81062,81072,81066,81067,81069,81071,72852,81065],"class_list":["post-27200","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-polygon-triangulation","tag-analysis-of-algorithms","tag-dynamic-programming-code-generation-algorithm","tag-minimum-weight-triangulation","tag-minimum-weight-triangulation-dynamic-programming","tag-minimum-weight-triangulation-of-convex-polygon","tag-optimal-polygon-triangulation","tag-optimal-polygon-triangulation-complexity","tag-optimal-polygon-triangulation-example","tag-optimal-triangulation-algorithm","tag-optimal-triangulation-of-a-convex-polygon","tag-polygon-triangulation-algorithm","tag-problems-on-dynamic-programming","tag-proving-correctness-of-the-algorithm-for-convex-polygon-minimum-cost"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27200"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27200\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}