igraph: How to convert a graph to a bipartite graph?

Viewed 184

I have a simple graph in igraph as follows:

> myGraph
IGRAPH 9689c03 DN-- 11 8 -- 
+ attr: name (v/n), types (v/n), col (v/n), degree (v/n)
+ edges from 9689c03 (vertex names):
[1] 2-> 7 2-> 8 2-> 9 2->10 3->11 3->12 3->13 3->14

Although it is a simple graph, its topology is like a bipartite graph. This means that nodes' types are 0 and 1 and the nodes in each group are not connected together:

> V(myGraph)$types
[1] 1 1 0 0 0 0 0 0 0 0 0

I am wondering if there is a method/function/approach to convert this simple graph to a bipartite one to use some available functions such as: bipartite.projection()?

1 Answers

Bipartite graph depends on type attribute of graph object myGraph, instead of types. In this case, you can try the following code to make myGraph a bipartite one:

> myGraph <- myGraph %>%
+   set_vertex_attr(name = "type", value = as.logical(V(.)$types))

> is_bipartite(myGraph)
[1] TRUE

where you can see myGraph is a bipartite graph now, and bipartite.projection() gives you

> bipartite.projection(myGraph)
$proj1
IGRAPH 8f910c3 UNW- 8 12 --
+ attr: name (v/c), types (v/l), weight (e/n)
+ edges from 8f910c3 (vertex names):
 [1] 7 --8  7 --9  7 --10 8 --9  8 --10 9 --10 11--12 11--13 11--14 12--13
[11] 12--14 13--14

$proj2
IGRAPH 8f910c3 UN-- 2 0 --
+ attr: name (v/c), types (v/l)
+ edges from 8f910c3 (vertex names):

dummy data

df <- data.frame(
  from = c(2, 2, 2, 2, 3, 3, 3, 3),
  to = c(7, 8, 9, 10, 11, 12, 13, 14)
)

myGraph <- graph_from_data_frame(df) %>%
  set_vertex_attr(name = "types", value = names(V(.)) %in% df$from)
Related