Assignment: unrooted trees

We often don’t root phylogenetic trees. This reduces the number of possible trees and is described by the equation:

Text: (2n-5)!/[2n-3*(n-3)!]

Rendered:

\(\frac{(2*n-5)!}{2^{n-3} * (n-3)!}\)

Assignment part 1

Modify the function used in the “number of phylogenetic trees” tutorial to work for unrooted trees.

Compare your results to http://carrot.mcb.uconn.edu/mcb396_41/tree_number.html

You can use the simplest form of the function which doesn’t have any additional argument, eg

The function is here; make the necessary changes

tree_count <- function(n){
  if(n < 3){
    warning("This function is only valid for 3 or more taxa.")
    break
  }
  numerator   <- factorial(2*n-5)
  denominator <- 2^(n-3)*factorial(n-3)
  trees  <- numerator / denominator 
  print(trees)
  
}
tree_count(11)
## [1] 34459425

Assignment part 2

Create a function that will work for rooted OR unrooted trees. Do this by adding an additional argument like

type = “rooted”

and conditional additional statements like

if(type == "rooted){ #do this }

if(type == "unrooted){ #do something else }

Again, you can use the simplest form of the argument.

tree_count_any <- function(n, type)
{
  if(n < 2){
    warning("This function is only valid for 3 or more taxa.")
    break
  }
  if(type == "unrooted"){
    numerator   <- factorial(2*n-5)
    denominator <- 2^(n-3)*factorial(n-3)
    trees  <- numerator / denominator 
    print(trees)
  }
  else if(type == "rooted"){
    numerator   <- factorial(2*n-3)
    denominator <- 2^(n-2)*factorial(n-2)
    trees  <- numerator / denominator 
    print(trees)
  }
  else
  {
    warning("Invalid tree type: Select rooted or unrooted tree.")
    break
  }
  
}
print("Number of Rooted Trees from 3 to 11:")
## [1] "Number of Rooted Trees from 3 to 11:"
for(i in 3:11)
{
  tree_count_any(i,"rooted")
}
## [1] 3
## [1] 15
## [1] 105
## [1] 945
## [1] 10395
## [1] 135135
## [1] 2027025
## [1] 34459425
## [1] 654729075
print("______________________")
## [1] "______________________"
print("Number of Unrooted Trees from 3 to 11:")
## [1] "Number of Unrooted Trees from 3 to 11:"
for(i in 3:11)
{
  tree_count_any(i,"unrooted")
}
## [1] 1
## [1] 3
## [1] 15
## [1] 105
## [1] 945
## [1] 10395
## [1] 135135
## [1] 2027025
## [1] 34459425
print("______________________")
## [1] "______________________"

For this assignment, render to RRubps, submit the link, and submit a screen grab that contains