rigraphtidygraph

Generate a unique set of complete undirected graphs from a tibble of nodes and sets


I have a tibble like this:

Nodes <- tibble(
  Set = c("A","A","A","B","B","C"),
  name = c(1,2,3,4,6,5)
)

I want to generate a graph that has Nodes as nodes set and

Edges <- tibble(
from = c(1,1,4),
to = c(2,3,6)
)

as edges. Of course I want to generalize this process for any Nodes. I think that I can code this with purrr, but I expect igraph to be faster and less tricky.

Theoretically, it would be nothing really different than:

Nodes %>% group_as(Set) %>% complete_graph(by = "name")

but I am not sure that a command like complete_graph() exists. It exists play_islands(), but it works differently and it assumes that n(Set) would be fixed.


Solution

  • Try make_full_graph from igraph

    with(
      Nodes,
      do.call(
        rbind,
        Map(
          function(v) {
            get.data.frame(
              set_vertex_attr(
                make_full_graph(length(v)),
                name = "name", value = v
              )
            )
          },
          split(name, Set)
        )
      )
    )
    

    which gives

        from to
    A.1    1  2
    A.2    1  3
    A.3    2  3
    B      4  6