Tuesday, November 9, 2010

R: working with graphs

use igraph


library(igraph)
create empty graph: graph.empty()
create graph with edges (1,2); (2,3); (5,6): graph(c(1,2,2,3,5,6), directed=TRUE)
get nodes number:  vcount(graph)
get edges number: ecount(graph)
add/delete edges: add.edges; delete.edges

Shortest paths: http://igraph.sourceforge.net/doc/R/shortest.paths.html

shortest.paths(graph, v=V(graph), mode = c("all", "out", "in"),
      weights = NULL, algorithm = c("automatic", "unweighted",
                                    "dijkstra", "bellman-ford",
                                    "johnson"))
get.shortest.paths(graph, from, to=V(graph), mode = c("all", "out",
      "in"), weights = NULL)
get.all.shortest.paths(graph, from, to = V(graph), mode = c("all", "out", "in")) 
average.path.length(graph, directed=TRUE, unconnected=TRUE)
path.length.hist (graph, directed = TRUE, verbose = igraph.par("verbose")) 

No comments:

Post a Comment