Tuesday, 8 March 2016

Making some simple graphs in igraph

library(igraph)
library(Matrix)

####################################################################
Couldn't find a function to make paths of size n in igraph, therefore:
make_path <- function(n = NULL){
  # assumed to be undirected
  require(igraph)
  stopifnot(n >= 1)
  if(n == 1){
    return(make_empty_graph(n, directed = FALSE))
    }
  #vertices <- (1:n)
  #edges <- rep(vertices, each = 2)[2:(2*n-1)]
  #make_graph(edges, directed = FALSE)
  # Alternative was found using igraph constructors
  # nb, this doesn't work for n = 1
  make_tree(n = n, children = 1, mode = 'undirected')
  }

##################################################### # cycle graphs

##################################################### # star graphs
make_star_ud <- function(n) make_star(n, mode = 'undirected')

#######################################################

# normalised betweenness:
# Betweenness values normalised by the sum of betweenness values for all vertices in the graph
# ? Should these values really be normalised by the number of possible shortest paths in a graph (ie, by choose(n,2) since betweenness is calculated in an undirected)
norm_bet <- function(graph){
  require(igraph)
  bet <- betweenness(graph)
  s.bet <- sum(bet)
  if(s.bet > 0){
    return(bet / s.bet)
    } else {
    return(bet)
    }
  }

########################################################

# eigenvector centrality

# eigen_centrality(graph)

########################################################

No comments:

Post a Comment