Wedderburn–Etherington number
Last Updated :
04 Jun, 2022
The Nth term in the Wedderburn–Etherington number sequence (starting with the number 0 for n = 0) counts the number of unordered rooted trees with n leaves in which all nodes including the root have either zero or exactly two children.
For a given N. The task is to find first N terms of the sequence.
Sequence:
0, 1, 1, 1, 2, 3, 6, 11, 23, 46, 98, 207, 451, 983, 2179, 4850, 10905, 24631, 56011, ….
Trees with 0 or 2 childs:
Examples:
Input : N = 10
Output : 0, 1, 1, 1, 2, 3, 6, 11, 23, 46,
Input : N = 20
Output : 0, 1, 1, 1, 2, 3, 6, 11, 23, 46, 98, 207, 451, 983, 2179, 4850, 10905, 24631, 56011, 127912
Approach:
The Recurrence relation to find Nth number is:
- a(2x-1) = a(1) * a(2x-2) + a(2) * a(2x-3) + … + a(x-1) * a(x)
- a(2x) = a(1) * a(2x-1) + a(2) * a(2x-2) + … + a(x-1) * a(x+1) + a(x) * (a(x)+1)/2
Using the above relation we can find the ith term of the series. We will start from the 0th term and then store the answer in a map and then use the values in the map to find the i+1 th term of the series. we will also use base cases for the 0th, 1st and 2nd element which are 0, 1, 1 respectively.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
map< int , int > store;
int Wedderburn( int n)
{
if (n <= 2)
return store[n];
else if (n % 2 == 0)
{
int x = n / 2, ans = 0;
for ( int i = 1; i < x; i++) {
ans += store[i] * store[n - i];
}
ans += (store[x] * (store[x] + 1)) / 2;
store[n] = ans;
return ans;
}
else
{
int x = (n + 1) / 2, ans = 0;
for ( int i = 1; i < x; i++) {
ans += store[i] * store[n - i];
}
store[n] = ans;
return ans;
}
}
void Wedderburn_Etherington( int n)
{
store[0] = 0;
store[1] = 1;
store[2] = 1;
for ( int i = 0; i < n; i++)
{
cout << Wedderburn(i);
if (i!=n-1)
cout << ", " ;
}
}
int main()
{
int n = 10;
Wedderburn_Etherington(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static HashMap<Integer,
Integer> store = new HashMap<Integer,
Integer>();
static int Wedderburn( int n)
{
if (n <= 2 )
return store.get(n);
else if (n % 2 == 0 )
{
int x = n / 2 , ans = 0 ;
for ( int i = 1 ; i < x; i++)
{
ans += store.get(i) * store.get(n - i);
}
ans += (store.get(x) * (store.get(x) + 1 )) / 2 ;
store. put(n, ans);
return ans;
}
else
{
int x = (n + 1 ) / 2 , ans = 0 ;
for ( int i = 1 ; i < x; i++)
{
ans += store.get(i) * store.get(n - i);
}
store. put(n, ans);
return ans;
}
}
static void Wedderburn_Etherington( int n)
{
store. put( 0 , 0 );
store. put( 1 , 1 );
store. put( 2 , 1 );
for ( int i = 0 ; i < n; i++)
{
System.out.print(Wedderburn(i));
if (i != n - 1 )
System.out.print( " " );
}
}
public static void main(String[] args)
{
int n = 10 ;
Wedderburn_Etherington(n);
}
}
|
Python3
store = dict ()
def Wedderburn(n):
if (n < = 2 ):
return store[n]
elif (n % 2 = = 0 ):
x = n / / 2
ans = 0
for i in range ( 1 , x):
ans + = store[i] * store[n - i]
ans + = (store[x] * (store[x] + 1 )) / / 2
store[n] = ans
return ans
else :
x = (n + 1 ) / / 2
ans = 0
for i in range ( 1 , x):
ans + = store[i] * store[n - i]
store[n] = ans
return ans
def Wedderburn_Etherington(n):
store[ 0 ] = 0
store[ 1 ] = 1
store[ 2 ] = 1
for i in range (n):
print (Wedderburn(i), end = "")
if (i ! = n - 1 ):
print (end = ", " )
n = 10
Wedderburn_Etherington(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static Dictionary< int ,
int > store = new Dictionary< int ,
int >();
static int Wedderburn( int n)
{
if (n <= 2)
return store[n];
else if (n % 2 == 0)
{
int x = n / 2, ans = 0;
for ( int i = 1; i < x; i++)
{
ans += store[i] * store[n - i];
}
ans += (store[x] * (store[x] + 1)) / 2;
if (store.ContainsKey(n))
{
store.Remove(n);
store.Add(n, ans);
}
else
store.Add(n, ans);
return ans;
}
else
{
int x = (n + 1) / 2, ans = 0;
for ( int i = 1; i < x; i++)
{
ans += store[i] * store[n - i];
}
if (store.ContainsKey(n))
{
store.Remove(n);
store.Add(n, ans);
}
else
store.Add(n, ans);
return ans;
}
}
static void Wedderburn_Etherington( int n)
{
store.Add(0, 0);
store.Add(1, 1);
store.Add(2, 1);
for ( int i = 0; i < n; i++)
{
Console.Write(Wedderburn(i));
if (i != n - 1)
Console.Write( " " );
}
}
public static void Main(String[] args)
{
int n = 10;
Wedderburn_Etherington(n);
}
}
|
Javascript
<script>
var store = new Map();
function Wedderburn(n)
{
if (n <= 2)
return store[n];
else if (n % 2 == 0)
{
var x = parseInt(n / 2), ans = 0;
for ( var i = 1; i < x; i++) {
ans += store[i] * store[n - i];
}
ans += (store[x] * (store[x] + 1)) / 2;
store[n] = ans;
return ans;
}
else
{
var x = (n + 1) / 2, ans = 0;
for ( var i = 1; i < x; i++) {
ans += store[i] * store[n - i];
}
store[n] = ans;
return ans;
}
}
function Wedderburn_Etherington(n)
{
store[0] = 0;
store[1] = 1;
store[2] = 1;
for ( var i = 0; i < n; i++)
{
document.write( Wedderburn(i));
if (i != n - 1)
document.write( ", " );
}
}
var n = 10;
Wedderburn_Etherington(n);
</script>
|
Output:
0, 1, 1, 1, 2, 3, 6, 11, 23, 46
Time Complexity: O(n2logn)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...