我目前有以下代码将所有最短路径导出到CSV。它工作正常,但是对于大型网络来说它很慢。有没有更有效的出口方式?
library(igraph)
g.links.data<-structure(list(OriginID = structure(c(2L, 3L, 1L, 3L, 1L, 2L), .Label = c("IAD",
"JFK", "MIA"), class = "factor"), DestinationID = structure(c(1L,
1L, 2L, 2L, 3L, 3L), .Label = c("IAD", "JFK", "MIA"), class = "factor"),
Weight = c(1.504321, 2.454545, 1.454348, 3.022917, 2.666667,
3.32619)), .Names = c("OriginID", "DestinationID", "Weight"
), class = "data.frame", row.names = c(NA, -6L))
g.nodes.data<-structure(list(Airport_Index = 1:3, Airport_ID = structure(1:3, .Label = c("IAD",
"JFK", "MIA"), class = "factor")), .Names = c("Airport_Index",
"Airport_ID"), class = "data.frame", row.names = c(NA, -3L))
g.graph<-graph.edgelist(as.matrix(g.links.data[,1:2]))
E(g.graph)$weight=as.numeric(g.links.data[,3])
g.graph.adjmatrix<-get.adjacency(g.graph,sparse=F)
file.remove("shortest_paths.csv")
for (v in 1:length(V(g.graph))) {
g.shortest_paths<-get.all.shortest.paths(g.graph, v, to = V(g.graph))
nb_of_sp <- length(g.shortest_paths[1][[1]]);
for (i in 1:nb_of_sp) {
SP <- as.matrix(t(g.shortest_paths[1][[1]][[i]]))
write.table(SP, file = "shortest_paths.csv", append = TRUE, sep = ",", col.names = FALSE, row.names = FALSE)
}
}
这给了我以下输出:
1
1,2
1,3
2,1
2
2,3
3,1
3,2
3