Questions tagged [motzkin-numbers]

Motzkin numbers. Number of rooted planar unlabeled unary-binary trees with N nodes. Use this tag when calculating Motzkin numbers, e.g. 1, 1, 2, 4, 9, 21, 51, 127, or with enumerating unary-binary trees, or binary expression trees, or related combinatorics . Note: Don't confuse binary expression trees with strictly binary trees used with expressions, binary expression trees have unary edges.

Motzkin numbers. Number of rooted planar unlabeled unary-binary trees with N nodes.

References

Related sequences

Motzkin numbers are for regular binary trees, e.g. all nodes have 0,1, or 2 branches, and count all nodes, while Catalan numbers are for full binary trees, e.g. all nodes have 0 or 2 branches, and count only leaves, while

6 questions
10
votes
2 answers

Generating strongly-connected, uniformly-distributed, random di-graphs

So I've been building a program that uses Monte Carlo simulations to find properties of evolutionary graph theory. One of the key functions of this is to be able to generate uniformly-distributed random graphs, so that we can determine the…
dl101
  • 147
  • 4
6
votes
2 answers

What is the fastest way to generate the n-th Motzkin number?

I want to generate all the Motzkin Number and store in an array. The formula is given as follows: My current implementation is just way too slow: void generate_slow() { mm[0] = 1; mm[1] = 1; mm[2] = 2; mm[3] = 4; mm[4] = 9; …
roxrook
  • 13,511
  • 40
  • 107
  • 156
5
votes
3 answers

Algorithm improvement for enumerating binary trees

Currently I can enumerate rooted planar unlabeled binary trees using the following brute-force Prolog code. e --> u | b | t. u --> ['[op(u),['], e, [']]']. b --> ['[op(b),['], e, [','], e, [']]']. t --> ['number(n)']. Note: See output listings…
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
3
votes
3 answers

How can I get rid of yield and use another function instead in my code

def mot (n) : if n==0 : yield n else : for m in mot(n-1) : yield [m] for k in range(0,n-1) : for l in mot(k) : for r in mot(n-2-k) : yield (l,r) def countFor(f,n) : for i in range(n) : …
3
votes
0 answers

R: fastest way to do high-precision calculations with very large numbers

I'm using dynamic programming (in R) to fill about 20,000 cells in an array. I need the exact integer in the final cell. The function only requires addition, multiplication, and logical expressions. If I run my function with double-precision…
3
votes
4 answers

Haskell: Code running too slow

I have a code which computes a Motzkin number as: module Main where -- Program execution begins here main :: IO () main = interact (unlines . (map show) . map wave . (map read) . words) -- Compute Motzkin number wave :: Integer…
Zubin Kadva
  • 593
  • 1
  • 4
  • 29