# 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.
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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) = &Sum; 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 ` `using` `namespace` `std; ` ` `  `// A dynamic programming based function to find nth ` `// Catalan number ` `unsigned ``long` `int` `catalanDP(unsigned ``int` `n) ` `{ ` `    ``// Table to store results of subproblems ` `    ``unsigned ``long` `int` `catalan[n+1]; ` ` `  `    ``// Initialize first two values in table ` `    ``catalan = catalan = 1; ` ` `  `    ``// Fill entries in catalan[] using recursive formula ` `    ``for` `(``int` `i=2; i<=n; i++) ` `    ``{ ` `        ``catalan[i] = 0; ` `        ``for` `(``int` `j=0; j

## 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. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// A dynamic programming  ` `// based function to find  ` `// nth Catalan number ` `static` `int` `catalanDP(``int` `n) ` `{ ` `    ``// Table to store  ` `    ``// results of subproblems ` `    ``int` `[]catalan = ``new` `int` `[n + ``1``]; ` ` `  `    ``// Initialize first ` `    ``// two values in table ` `    ``catalan[``0``] = catalan[``1``] = ``1``; ` ` `  `    ``// Fill entries in catalan[]  ` `    ``// using recursive formula ` `    ``for` `(``int` `i = ``2``; i <= n; i++) ` `    ``{ ` `        ``catalan[i] = ``0``; ` `        ``for` `(``int` `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. ` `static` `int` `countWays(``int` `n) ` `{ ` `    ``// Throw error if n is odd ` `    ``if` `(n < ``1``) ` `    ``{ ` `        ``System.out.println(``"Invalid"``); ` `        ``return` `0``; ` `    ``} ` ` `  `    ``// Else return n/2'th  ` `    ``// Catalan number ` `    ``return` `catalanDP(n / ``2``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``System.out.println(countWays(``6``) + ``" "``); ` `} ` `} ` ` `  `// This code is contributed  ` `// by akt_mit `

## 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 number ` `def` `catalanDP(n): ` `     `  `    ``# Table to store results ` `    ``# of subproblems  ` `    ``catalan ``=` `[``1` `for` `i ``in` `range``(n ``+` `1``)] ` `     `  `    ``# Fill entries in catalan[] ` `    ``# using recursive formula  ` `    ``for` `i ``in` `range``(``2``, n ``+` `1``): ` `        ``catalan[i] ``=` `0` `        ``for` `j ``in` `range``(i): ` `            ``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. ` `def` `countWays(n): ` `     `  `    ``# Throw error if n is odd  ` `    ``if` `(n & ``1``): ` `        ``print``(``"Invalid"``) ` `        ``return` `0` `         `  `    ``# Else return n/2'th Catalan number ` `    ``return` `catalanDP(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. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// A dynamic programming  ` `// based function to find  ` `// nth Catalan number ` `static` `int` `catalanDP(``int` `n) ` `{ ` `    ``// Table to store  ` `    ``// results of subproblems ` `    ``int` `[]catalan = ``new` `int` `[n + 1]; ` ` `  `    ``// Initialize first ` `    ``// two values in table ` `    ``catalan = catalan = 1; ` ` `  `    ``// Fill entries in catalan[]  ` `    ``// using recursive formula ` `    ``for` `(``int` `i = 2; i <= n; i++) ` `    ``{ ` `        ``catalan[i] = 0; ` `        ``for` `(``int` `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. ` `static` `int` `countWays(``int` `n) ` `{ ` `    ``// Throw error if n is odd ` `    ``if` `(n < 1) ` `    ``{ ` `        ``Console.WriteLine(``"Invalid"``); ` `        ``return` `0; ` `    ``} ` ` `  `    ``// Else return n/2'th  ` `    ``// Catalan number ` `    ``return` `catalanDP(n / 2); ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `    ``Console.WriteLine(countWays(6) + ``" "``); ` `} ` `} ` ` `  `// This code is contributed  ` `// by M_kit `

## PHP

 ` `

Output :

`5`

Time Complexity : O(n2)
Auxiliary Space : O(n)

This article is contributed by Shivam Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.