Number of ways of Triangulation for a Polygon
Last Updated : 06 Nov, 2024
Given a convex polygon with n sides. The task is to calculate the number of ways in which triangles can be formed by connecting vertices with non-crossing line segments.
Examples:
Input: n = 3
Output: 1
It is already a triangle so it can only be formed in 1 way.
Input: n = 4
Output: 2
It can be cut into 2 triangles by using either pair of opposite vertices.
Input: n = 6
Output: 14 (Below, There are all 14 polygon).
It can be cut into 2 triangles by using either pair of opposite vertices.
The above problem is an application of a catalan numbers. The task is to only find the (n-2)’th Catalan Number. First few catalan numbers for n = 0, 1, 2, 3, 4, 5 are 1 1 2 5 14 42, … (considered from 0th number)
C++ // C++ code of finding Number of ways a convex polygon of n+2 // sides can split into triangles by connecting vertices #include <iostream> using namespace std; // Function to calculate the binomial coefficient C(n, k) int binomialCoeff(int n, int k) { // C(n, k) is the same as C(n, n-k) if (k > n - k) { k = n - k; } int res = 1; // Calculate the value of n! / (k! * (n-k)!) for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to find the nth Catalan number int countWays(int n) { n = n - 2; // Calculate C(2n, n) int c = binomialCoeff(2 * n, n); // Return the nth Catalan number return c / (n + 1); } int main() { int n = 6; cout << countWays(n) << endl; return 0; }
C // C code of finding Number of ways a convex polygon of n+2 // sides can split into triangles by connecting vertices #include <stdio.h> // Function to calculate the binomial coefficient C(n, k) int binomialCoeff(int n, int k) { // C(n, k) is the same as C(n, n-k) if (k > n - k) { k = n - k; } int res = 1; // Calculate the value of n! / (k! * (n-k)!) for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to find the nth Catalan number int countWays(int n) { n = n - 2; // Calculate C(2n, n) int c = binomialCoeff(2 * n, n); // Return the nth Catalan number return c / (n + 1); } int main() { int n = 6; printf("%d\n", countWays(n)); return 0; }
Java // Java code of finding Number of ways a convex polygon of n+2 // sides can split into triangles by connecting vertices public class PolygonTriangulation { // Function to calculate the binomial coefficient C(n, k) static int binomialCoeff(int n, int k) { // C(n, k) is the same as C(n, n-k) if (k > n - k) { k = n - k; } int res = 1; // Calculate the value of n! / (k! * (n-k)!) for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to find the nth Catalan number static int countWays(int n) { n = n - 2; // Calculate C(2n, n) int c = binomialCoeff(2 * n, n); // Return the nth Catalan number return c / (n + 1); } public static void main(String[] args) { int n = 6; System.out.println(countWays(n)); } }
Python # Python code of finding Number of ways a convex polygon of n+2 # sides can split into triangles by connecting vertices def binomialCoeff(n, k): # C(n, k) is the same as C(n, n-k) if k > n - k: k = n - k res = 1 # Calculate the value of n! / (k! * (n-k)!) for i in range(k): res *= (n - i) res //= (i + 1) return res # Function to find the nth Catalan number def countWays(n): n = n - 2 # Calculate C(2n, n) c = binomialCoeff(2 * n, n) # Return the nth Catalan number return c // (n + 1) n = 6 print(countWays(n))
JavaScript // JavaScript code of finding Number of ways a convex polygon of n+2 // sides can split into triangles by connecting vertices function binomialCoeff(n, k) { // C(n, k) is the same as C(n, n-k) if (k > n - k) { k = n - k; } let res = 1; // Calculate the value of n! / (k! * (n-k)!) for (let i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Function to find the nth Catalan number function countWays(n) { n = n - 2; // Calculate C(2n, n) let c = binomialCoeff(2 * n, n); // Return the nth Catalan number return c / (n + 1); } let n = 6; console.log(countWays(n));
Time Complexity: O(n), where n is nth catalan number.
Auxiliary Space: O(1)
Similar Reads
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. We
15+ min read
Count of ways to split N into Triplets forming a Triangle Given an integer N, the task is to find the number of ways to split N into ordered triplets which can together form a triangle. Examples: Input: N = 15 Output: Total number of triangles possible are 28 Input: N = 9 Output: Total number of triangles possible is 10 Approach: The following observation
4 min read
Number of triangles formed from a set of points on three lines Given three integers m, n, and k that store the number of points on lines l1, l2, and l3 respectively that do not intersect. The task is to find the number of triangles that can possibly be formed from these set of points. Examples: Input: m = 3, n = 4, k = 5 Output: 205Input: m = 2, n = 2, k = 1 Ou
5 min read
Number of triangles after N moves Find the number of triangles in Nth step, Rules: Draw an equilateral triangle at the start. In the i-th move, take the uncolored triangles, divides each of them in 4 parts of equal areas and color the central part. Keep a count of triangles till the Nth step. Examples: Input : 1 Output : 5 Explanati
6 min read
Number of triangles that can be formed with given N points Given X and Y coordinates of N points on a Cartesian plane. The task is to find the number of possible triangles with the non-zero area that can be formed by joining each point to every other point. Examples: Input: P[] = {{0, 0}, {2, 0}, {1, 1}, {2, 2}} Output: 3 Possible triangles can be [(0, 0},
9 min read