[R] Calculating shortest path length among multiple graphs

张伦 zhanglun1525 at gmail.com
Wed Dec 24 03:14:52 CET 2014


Hi,
I have an edgelist network data (undirected) with multiple graphs like
this. For each graph (i.e., denoted by doc.id), I want to calculate the
shortest path length (SPL) between the nodes. By using the code attached
below, I can only calculate the SPL for only 1 graph at a time. How can I
handle the multiple networks in R?
Thanks for the help.
Lun
graph = data.frame(V1=c("A","A","B","B","C","D"),
V2=c("C","D","F","A","A","Z"),
doc.id=c("1","2","2","3","3","3"))
el=as.matrix(graph)
el[,1]=as.character(el[,1])
el[,2]=as.character(el[,2])
graph=graph.edgelist(el[,1:2],directed=FALSE)
sp <- shortest.paths(graph, v=V(graph), to=V(graph),
  mode = c("all"),
  weights = NULL, algorithm = c("dijkstra"))
sp <-graph.adjacency(sp, mode=c( "undirected"), weighted=T)
sp <- cbind( get.edgelist(sp) , round( E(sp)$weight, 3 ))

	[[alternative HTML version deleted]]



More information about the R-help mailing list