I wrote this R code that randomly generates 25 graphs (each graph is made with a random number of vertices):
library(igraph)
plot_random_trees <- function(num_trees) {
par(mfrow = c(5, 5), mar = c(1, 1, 2, 1))
for (i in 1:num_trees) {
num_vertices <- sample(5:15, 1)
random_tree <- sample_tree(num_vertices)
plot(random_tree, main = paste0("Tree #", i), margin = 0.1)
grid()
box()
}
par(mfrow = c(1, 1), mar = c(5.1, 4.1, 4.1, 2.1))
}
plot_random_trees(25)
Based on these graphs that have been generated, I want to answer the following question: Which of these graphs belong to TREE(3)?
That is, is it possible to write a function in R which takes an igraph object as an input and sees if a given graph satisfies the properties to be considered part of the TREE(3) subset? E.g. no cycles, no self referential edges, maximum degree of 3, etc.
Here is my attempt to manually write functions to check some of these conditions (in a randomly generated graph) using the igraph library:
num_vertices <- sample(5:15, 1)
random_tree <- sample_tree(num_vertices)
#################################
# should be equal to the tree(n), e.g. if n=3, function should return 3
get_max_degree <- function(graph) {
max(degree(graph))
}
has_cycles <- function(graph) {
girth_info <- girth(graph)
return(girth_info$girth > 0)
}
#########################################
get_max_degree(random_tree)
has_cycles(random_tree)
Can someone please show me to check if a randomly generated graph belongs to TREE(n=m)?
Thanks!