简体   繁体   中英

Cleaning a graph with R package “igraph”

I need to "clean" a graph in R. By cleaning, I mean that i need to delete all nodes which are not linked with a specific one. For instance, if in my graph there are 4 nodes, with these edges :

  • 1 to 3

  • 1 to 2

  • 4 to 2

I want to keep only the nodes linked with the edges 1 plus the edges 1 itself, so to say I need to delete the edges 4.

Is there any way with igraph to build an algorithm which can do that for very very very big graph (like more than 1000 nodes and 1 000 000 edges) ?

Use subcomponent and induced.subgraph :

edges_df <- data.frame(from = c(1, 1, 4), to = c(3, 2, 2))
g1 <- graph.data.frame(edges_df, directed = TRUE)

g2 <- induced.subgraph(g1, subcomponent(g1, "1", mode = "out"))

As for the "big" graphs: 1000 is not so big. On my laptop:

system.time({
  g3 <- graph.full(n = 1000, directed = TRUE)
  g4 <- induced.subgraph(g3, subcomponent(g3, "1", mode = "out"))
})
# user  system elapsed 
# 0.47    0.10    0.57 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM