Schröder–Hipparchus number
Last Updated :
14 Feb, 2023
The Schröder–Hipparchus numbers form an integer sequence that can be used to count the number of plane trees with a given set of leaves, the number of ways of inserting parentheses into a sequence, and the number of ways of dissecting a convex polygon into smaller polygons by inserting diagonals.
Schröder–Hipparchus number can be define by recurrence relation:
Schröder–Hipparchus number can be used to count several closely related combinatorial objects:
- The nth number in the sequence counts the number of different ways of subdividing of a polygon with n + 1 sides into smaller polygons by adding diagonals of the original polygon. Refer this image for details.
Image source: Wikipedia - The nth number counts the number of different plane trees with n leaves and with all internal vertices having two or more children.
- The nth number counts the number of different ways of inserting parentheses into a sequence of n symbols, with each pair of parentheses surrounding two or more symbols or parenthesized groups, and without any parentheses surrounding the entire sequence.
- The nth number counts the number of faces of all dimensions of an associahedron Kn + 1 of dimension n ? 1, including the associahedron itself as a face, but not including the empty set. For instance, the two-dimensional associahedron K4 is a pentagon; it has five vertices, five faces, and one whole associahedron, for a total of 11 faces.
- count the number of double permutations (sequences of the numbers from 1 to n, each number appearing twice, with the first occurrences of each number in sorted order) that avoid the permutation patterns 12312 and 121323.
Examples:
Input : n = 5
Output : 45
Input : n = 6
Output : 197
A simple solution is simply to implement a recursive formula for the numbers.
C++
#include <bits/stdc++.h>
using namespace std;
int nthSHN( int n)
{
if (n == 1 || n == 2)
return 1;
return ((6 * n - 9) * nthSHN(n - 1) -
(n - 3) * nthSHN(n - 2)) / n;
}
int main()
{
int n = 6;
cout << nthSHN(n) << endl;
return 0;
}
|
Java
class GFG
{
static int nthSHN( int n)
{
if (n == 1 || n == 2 )
return 1 ;
return (( 6 * n - 9 ) * nthSHN(n - 1 ) -
(n - 3 ) * nthSHN(n - 2 )) / n;
}
public static void main (String[] args)
{
int n = 6 ;
System.out.println(nthSHN(n));
}
}
|
Python3
def nthSHN(n):
if (n = = 1 or n = = 2 ):
return 1
else :
return (( 6 * n - 9 ) * nthSHN(n - 1 ) -
((n - 3 ) * nthSHN(n - 2 ))) / n
n = 6
print (nthSHN(n))
|
C#
using System;
class GFG
{
static int nthSHN( int n)
{
if (n == 1 || n == 2)
return 1;
return ((6 * n - 9) * nthSHN(n - 1) -
(n - 3) * nthSHN(n - 2)) / n;
}
public static void Main ()
{
int n = 6;
Console.WriteLine(nthSHN(n));
}
}
|
PHP
<?php
function nthSHN( $n )
{
if ( $n == 1 || $n == 2)
return 1;
return ((6 * $n - 9) * nthSHN( $n - 1) -
( $n - 3) * nthSHN( $n - 2)) / $n ;
}
$n = 6;
echo nthSHN( $n );
?>
|
Javascript
<script>
function nthSHN(n)
{
if (n == 1 || n == 2)
return 1;
return ((6 * n - 9) * nthSHN(n - 1) -
(n - 3) * nthSHN(n - 2)) / n;
}
let n = 6;
document.write(nthSHN(n));
</script>
|
Output:
197
Time Complexity: O(2^n)
Auxiliary Space: O(n) since the maximum number of function calls is n.
Below is Dynamic Programming solution of finding nth Schröder–Hipparchus number:
C++
#include <bits/stdc++.h>
#define MAX 500
using namespace std;
int nthSHN( int n, int dp[])
{
if (n == 1 || n == 2)
return dp[n] = 1;
if (dp[n] != -1)
return dp[n];
return dp[n] = ((6 * n - 9) * nthSHN(n - 1, dp) -
(n - 3) * nthSHN(n - 2, dp)) / n;
}
int main()
{
int n = 6;
int dp[MAX];
memset (dp, -1, sizeof dp);
cout << nthSHN(n, dp) << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static final int MAX= 500 ;
static int nthSHN( int n, int dp[])
{
if (n == 1 || n == 2 )
return dp[n] = 1 ;
if (dp[n] != - 1 )
return dp[n];
return dp[n] = (( 6 * n - 9 ) * nthSHN(n - 1 , dp) -
(n - 3 ) * nthSHN(n - 2 , dp)) / n;
}
public static void main (String[] args)
{
int n = 6 ;
int dp[] = new int [MAX];
Arrays.fill(dp, - 1 );
System.out.println(nthSHN(n, dp));
}
}
|
Python3
def nthSHN(n, dp):
if (n = = 1 or n = = 2 ):
dp[n] = 1
return dp[n]
if (dp[n] ! = - 1 ):
return dp[n]
dp[n] = (( 6 * n - 9 ) * nthSHN(n - 1 , dp) -
(n - 3 ) * nthSHN(n - 2 , dp)) / n
return dp[n]
n = 6 ;
dp = [ - 1 for i in range ( 500 )]
print (nthSHN(n, dp))
|
C#
using System;
class GFG
{
static int MAX = 500;
static int nthSHN( int n, int [] dp)
{
if (n == 1 || n == 2)
return dp[n] = 1;
if (dp[n] != -1)
return dp[n];
return dp[n] = ((6 * n - 9) *
nthSHN(n - 1, dp) -
(n - 3) *
nthSHN(n - 2, dp)) / n;
}
public static void Main ()
{
int n = 6;
int [] dp = new int [MAX];
for ( int i = 0; i < dp.Length; i++)
dp[i] = -1;
Console.Write(nthSHN(n, dp));
}
}
|
PHP
<?php
$MAX = 500;
function nthSHN( $n , $dp )
{
if ( $n == 1 || $n == 2)
return $dp [ $n ] = 1;
if ( $dp [ $n ] != -1)
return $dp [ $n ];
return $dp [ $n ] = ((6 * $n - 9) *
nthSHN( $n - 1, $dp ) -
( $n - 3) *
nthSHN( $n - 2, $dp )) / $n ;
}
$n = 6;
$dp = array_fill (0, $MAX , true);
echo nthSHN( $n , $dp ), "\n" ;
?>
|
Javascript
<script>
let MAX = 500;
function nthSHN(n, dp)
{
if (n == 1 || n == 2)
return dp[n] = 1;
if (dp[n] != -1)
return dp[n];
return dp[n] = ((6 * n - 9) * nthSHN(n - 1, dp) - (n - 3) * nthSHN(n - 2, dp)) / n;
}
let n = 6;
let dp = new Array(MAX);
for (let i = 0; i < dp.length; i++)
dp[i] = -1;
document.write(nthSHN(n, dp));
</script>
|
Output:
197
Time Complexity: O(n)
Auxiliary Space: O(n)
Source:
https://en.wikipedia.org/wiki/Schr%C3%B6der%E2%80%93Hipparchus_number
Share your thoughts in the comments
Please Login to comment...