Catalan numbers for triangulations gambler’s ruin binary…

July 27, 2015
By

[This article was first published on isomorphismes, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Catalan numbers for

  • triangulations
  • gambler’s ruin
  • binary trees
  • trees

…from the Flajolet/Sedgewick coursera analysis of algorithms

Noncrossing partitions 5.svg

in R:

catalan <- function(n) {
    if(n<=1) 1
    else lapply(1:n,
        FUN=function(k) catalan(k-1) * catalan(n-k) ) %>% unlist %>% sum
    }

(This is slow above catalan(10) though, so here’s a memoised version. Then if you want to run above catalan(250) you’ll need to adjust options(expressions=5e3) to something higher; otherwise the nesting of calls goes too deep.)

To leave a comment for the author, please follow the link and comment on their blog: isomorphismes.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)