1

I have a graph, which i would like to split by a node. Which is a presented below.

I am using R tidygraph. It has morph functionality with to_split and to_components, but i couldnot find proper documentation to implement it. Any leads will be helpful.

enter image description here

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
naveen chandra
  • 149
  • 1
  • 7

1 Answers1

2

A possible option with igraph

library(igraph)
g <- graph_from_data_frame(df, directed = FALSE)
v <- "3"
v.neighbor <- neighbors(g, v)
v.new <- paste0(v, "_", seq_along(v.neighbor))
gout <- g %>%
    delete.vertices(v) %>%
    add.vertices(nv = length(v.neighbor), name = v.new) %>%
    add.edges(c(rbind(v.new, names(v.neighbor))))

and plot(gout) shows

enter image description here

data

df <- data.frame(
    from = c(1, 2, 3, 3, 4, 7, 5),
    to = c(2, 3, 4, 5, 7, 8, 10)
)

enter image description here

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Thanks @ThomasIsCoding. I have a follow-up question on using `igraph` functionalities on tidygraph object. Is there anything to keep in mind when using `igraph` functionalities on tidygraph. I have come across the following link where it says "passing this onto igraph functions works only when the column is called name within the node data. This might be an issue to report to tidygraph". Are there any such rules to be aware of. https://stackoverflow.com/questions/65446626/tidygraph-obtain-sequence-of-nodes-along-shortest-path – naveen chandra Jun 22 '21 at 14:22
  • @naveenchandra Sorry that I didn't use `tidygraph` before, so I may not be able to help you – ThomasIsCoding Jun 22 '21 at 19:34