Open In App

Number of ways of Triangulation for a Polygon

Last Updated : 06 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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. 

4

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));

Output
14

Time Complexity: O(n), where n is nth catalan number.
Auxiliary Space: O(1)


Next Article

Similar Reads