Non-crossing lines to connect points in a circle

Consider a circle with n points on circumference of it where n is even. Count number of ways we can connect these points such that no two connecting lines to cross each other and every point is connected with exactly one other point. Any point can be connected with any other point.
 
Consider a circle with 4 points.
    1
2        3
    4
In above diagram, there are two 
non-crossing ways to connect
{{1, 2}, {3, 4}} and {{1, 3}, {2, 4}}.
Note that {{2, 3}, {1, 4}} is invalid
as it would cause a cross
Examples : 
 
Input : n = 2 Output : 1 Input : n = 4 Output : 2 Input : n = 6 Output : 5 Input : n = 3 Output : Invalid n must be even.
We need to draw n/2 lines to connect n points. When we draw a line, we divide the points in two sets that need to connected. Each set needs to be connected within itself. Below is recurrence relation for the same.
 
Let m = n/2
// For each line we draw, we divide points
// into two sets such that one set is going
// to be connected with i lines and other
// with m-i-1 lines.
Count(m) = ∑ Count(i) * Count(m-i-1) 
           where 0 <= i < m
Count(0) = 1
Total number of ways with n points 
               = Count(m) = Count(n/2)
If we take a closer look at above recurrence, it is actually recurrence of Catalan Numbers. So the task reduces to finding n/2’th Catalan number.
Below is implementation based on above idea.
 
C++
| // C++ program to count number of ways to connect n (where n// is even) points on a circle such that no two connecting// lines cross each other and every point is connected with// one other point.#include <iostream>usingnamespacestd;// A dynamic programming based function to find nth// Catalan numberunsigned longintcatalanDP(unsigned intn){    // Table to store results of subproblems    unsigned longintcatalan[n + 1];    // Initialize first two values in table    catalan[0] = catalan[1] = 1;    // Fill entries in catalan[] using recursive formula    for(inti = 2; i <= n; i++) {        catalan[i] = 0;        for(intj = 0; j < i; j++)            catalan[i] += catalan[j] * catalan[i - j - 1];    }    // Return last entry    returncatalan[n];}// Returns count of ways to connect n points on a circle// such that no two connecting lines cross each other and// every point is connected with one other point.unsigned longintcountWays(unsigned longintn){    // Throw error if n is odd    if(n & 1) {        cout << "Invalid";        return0;    }    // Else return n/2'th Catalan number    returncatalanDP(n / 2);}// Driver program to test above functionintmain(){    cout << countWays(6) << " ";    return0;}// This code is contributed by Aditya Kumar (adityakumar129) | 
C
| // C program to count number of ways to connect n (where n// is even) points on a circle such that no two connecting// lines cross each other and every point is connected with// one other point.#include <stdio.h>// A dynamic programming based function to find nth// Catalan numberunsigned longintcatalanDP(unsigned intn){    // Table to store results of subproblems    unsigned longintcatalan[n + 1];    // Initialize first two values in table    catalan[0] = catalan[1] = 1;    // Fill entries in catalan[] using recursive formula    for(inti = 2; i <= n; i++) {        catalan[i] = 0;        for(intj = 0; j < i; j++)            catalan[i] += catalan[j] * catalan[i - j - 1];    }    // Return last entry    returncatalan[n];}// Returns count of ways to connect n points on a circle// such that no two connecting lines cross each other and// every point is connected with one other point.unsigned longintcountWays(unsigned longintn){    // Throw error if n is odd    if(n & 1) {        printf("Invalid");        return0;    }    // Else return n/2'th Catalan number    returncatalanDP(n / 2);}// Driver program to test above functionintmain(){    printf("%ld", countWays(6));    return0;}// This code is contributed by Aditya Kumar (adityakumar129) | 
Java
| // Java program to count number of ways to connect n (where// n is even) points on a circle such that no two connecting// lines cross each other and every point is connected with// one other point.importjava.io.*;classGFG {    // A dynamic programming based function to find nth    // Catalan number    staticintcatalanDP(intn)    {        // Table to store results of subproblems        int[] catalan = newint[n + 1];        // Initialize first two values in table        catalan[0] = catalan[1] = 1;        // Fill entries in catalan[] using recursive formula        for(inti = 2; i <= n; i++) {            catalan[i] = 0;            for(intj = 0; j < i; j++)                catalan[i]                    += catalan[j] * catalan[i - j - 1];        }        // Return last entry        returncatalan[n];    }    // Returns count of ways to connect n points on a circle    // such that no two connecting lines cross each other    // and every point is connected with one other point.    staticintcountWays(intn)    {        // Throw error if n is odd        if(n < 1) {            System.out.println("Invalid");            return0;        }        // Else return n/2'th Catalan number        returncatalanDP(n / 2);    }    // Driver Code    publicstaticvoidmain(String[] args)    {        System.out.println(countWays(6) + " ");    }}// This code is contributed by Aditya Kumar (adityakumar129) | 
Python3
| # Python3 program to count number # of ways to connect n (where n # is even) points on a circle such # that no two connecting lines# cross each other and every point # is connected with one other point. # A dynamic programming based # function to find nth Catalan numberdefcatalanDP(n):        # Table to store results    # of subproblems     catalan =[1fori inrange(n +1)]        # Fill entries in catalan[]    # using recursive formula     fori inrange(2, n +1):        catalan[i] =0        forj inrange(i):            catalan[i] +=(catalan[j] *                           catalan[i -j -1])    # Return last entry     returncatalan[n]# Returns count of ways to connect# n points on a circle such that # no two connecting lines cross # each other and every point is# connected with one other point.defcountWays(n):        # Throw error if n is odd     if(n & 1):        print("Invalid")        return0            # Else return n/2'th Catalan number    returncatalanDP(n //2)# Driver Code print(countWays(6))# This code is contributed # by sahilshelangia | 
C#
| // C# program to count number // of ways to connect n (where // n is even) points on a circle// such that no two connecting// lines cross each other and // every point is connected with// one other point.usingSystem;classGFG{    // A dynamic programming // based function to find // nth Catalan numberstaticintcatalanDP(intn){    // Table to store     // results of subproblems    int[]catalan = newint[n + 1];    // Initialize first    // two values in table    catalan[0] = catalan[1] = 1;    // Fill entries in catalan[]     // using recursive formula    for(inti = 2; i <= n; i++)    {        catalan[i] = 0;        for(intj = 0; j < i; j++)            catalan[i] += catalan[j] *                           catalan[i - j - 1];    }    // Return last entry    returncatalan[n];}// Returns count of ways to // connect n points on a circle// such that no two connecting// lines cross each other and// every point is connected // with one other point.staticintcountWays(intn){    // Throw error if n is odd    if(n < 1)    {        Console.WriteLine("Invalid");        return0;    }    // Else return n/2'th     // Catalan number    returncatalanDP(n / 2);}// Driver CodestaticpublicvoidMain (){    Console.WriteLine(countWays(6) + " ");}}// This code is contributed // by M_kit | 
PHP
| <?php// PHP program to count number of// ways to connect n (where n is // even) points on a circle such // that no two connecting lines // cross each other and every // point is connected with one // other point.// A dynamic programming based // function to find nth Catalan numberfunctioncatalanDP($n){    // Table to store results     // of subproblems Initialize     // first two values in table    $catalan[0] = $catalan[1] = 1;    // Fill entries in catalan[]     // using recursive formula    for($i= 2; $i<= $n; $i++)    {        $catalan[$i] = 0;        for($j= 0; $j< $i; $j++)            $catalan[$i] += $catalan[$j] *                             $catalan[$i- $j- 1];    }    // Return last entry    return$catalan[$n];}// Returns count of ways to connect // n points on a circle such that // no two connecting lines cross // each other and every point is // connected with one other point.functioncountWays($n){// Throw error if n is oddif($n& 1){    echo"Invalid";    return0;}// Else return n/2'th// Catalan numberreturncatalanDP($n/ 2);}// Driver CodeechocountWays(6) , " ";// This code is contributed by aj_36 ?> | 
Javascript
| <script>// javascript program to count number of// ways to connect n (where n is // even) points on a circle such // that no two connecting lines // cross each other and every // point is connected with one // other point.// A dynamic programming based // function to find nth Catalan numberfunctioncatalanDP(n){    // Table to store results     // of subproblems Initialize     // first two values in table    let catalan = []    catalan[0] = catalan[1] = 1;    // Fill entries in catalan[]     // using recursive formula    for(let i = 2; i <= n; i++)    {        catalan[i] = 0;        for(let j = 0; j < i; j++)            catalan[i] += catalan[j] *                             catalan[i - j - 1];    }    // Return last entry    returncatalan[n];}// Returns count of ways to connect // n points on a circle such that // no two connecting lines cross // each other and every point is // connected with one other point.functioncountWays(n){// Throw error if n is oddif(n & 1){    document.write("Invalid");    return0;}// Else return n/2'th// Catalan numberreturncatalanDP(n / 2);}// Driver Codedocument.write(countWays(6) + " ");// This code is contributed by _saurabh_jaiswal</script> | 
Output :
5
Time Complexity : O(n2) 
Auxiliary Space : O(n)
This article is contributed by Shivam Agrawal. If you like zambiatek and would like to contribute, you can also write an article and mail your article to review-team@zambiatek.com. See your article appearing on the zambiatek main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
 
				 
					


