2

I would like generate a random bipartite graph with a fixed degree sequence in igraph or other r package. I am familiar with how to generate a random graph with a fixed degree sequence and how to generate random bipartite graph in igraph--but I cannot sort out how to generate a random bipartite graph with a fixed degree sequence (as described by Newman, Watts and Strogatz (2002)).

avgoustisw
  • 213
  • 1
  • 7

2 Answers2

2

We can use sample_degseq by defining out.degree and in.degree (borrowing data from the answer by @Szabolcs)

sample_degseq(c(deg1, 0 * deg2), c(0 * deg1, deg2)) %>%
  set_vertex_attr(name = "type", value = degree(., mode = "out") > 0) %>%
  plot(layout = layout_as_bipartite)

which produces a graph like below

enter image description here

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Thank you @ThomasIsCoding--can you unpack what the value argument in the set_vertex_attr() function is doing? I understand that the $type attribute is logical, but I can't sort out what this bit of your solution is doing. – avgoustisw Feb 01 '22 at 16:28
  • @avgoustisw `set_vertex_attr` adds an attribute named "type" to the vertices. since we need two types of nodes for plotting `bipartite` graph. "type" doesn't have to be logic values, i.e., `TRUE` or `FALSE`. Any vector consists of two categories is always fine. – ThomasIsCoding Feb 01 '22 at 17:35
1

Use a bipartite version of the configuration model:

  • create vertex IDs for each of the two partitions
  • replicate each vertex ID as many times as its degree
  • shuffle the lists if IDs and match them up, creating a graph
deg1 <- c(3,2,1,1,1)
deg2 <- c(2,2,2,2)

edgelist <- cbind(sample(rep(1:length(deg1), deg1)), sample(rep(length(deg1) + 1:length(deg2), deg2)))

graph <- graph_from_edgelist(edgelist)

You can now check degree(graph).

Note that this may create multigraphs (graphs with parallel edges), and that it does not sample uniformly over the set of bipartite multigraphs. However, it can be shown that if you reject all non-simple outcomes, the sampling becomes uniform.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174