user3767071
user3767071

Reputation: 109

plot bipartite graph created with Networkx using igraph

I have trouble reading my bipartite matrix in igraph. I created bipartite graphs using Networkx and exported them as a biadjacency matrix:

   bipartite.biadjacency_matrix(Graph[i],row_order=bipartite.sets(stockGraph[i])[0], column_order=bipartite.sets(stockGraph[i])[1],format='csr') 

Then I import the 10x10 matrix in R using igraph :

  data <-readMat("graphToMatlab1.mat")
  data1 <- data$graphToMatlab[[1]][[1]]
  graph <- graph_from_adjacency_matrix(data1, mode="undirected")

Here is the sparse matrix:

data1 10 x 10 sparse Matrix of class "dgCMatrix"

 [1,] 1 . . . . . . . . .
 [2,] . . . . 1 . . . . .
 [3,] . . 1 . . 1 . . . .
 [4,] . . . . 1 . 1 1 . .
 [5,] . . . . . . . . . 1
 [6,] . . 1 1 . . 1 . 1 .
 [7,] . . 1 1 1 2 . . . .
 [8,] . . 1 . . 1 . . . .
 [9,] . . 1 1 . . . 1 . .
 [10,] . 2 . . . . . . . .

graph

IGRAPH U--- 10 21 -- 
+ edges:
[1] 1-- 1 2-- 5 2--10 2--10 3-- 3 3-- 6 3-- 7 3-- 8 3-- 9 4-- 5 4-- 6  4-- 7 4-- 8 4-- 9 5-- 7 5--10 6-- 7 6-- 7 6-- 8 6-- 9
[21] 8-- 9

So this is wrong because it does not take into account that there are two types of vertices (missing the attributes section). So I think this is because of the way I exported the graph (using bipartite.biadjacency matrix), but is there a way to bypass this problem? Either in the way igraph reads matrices or how I export in Networkx ?

Upvotes: 0

Views: 605

Answers (1)

Aric
Aric

Reputation: 25309

You probably just want the adjacency matrix representation of your bipartite graph

In [1]: import networkx as nx

In [2]: G = nx.complete_bipartite_graph(5,3)

In [3]: nx.adjacency_matrix(G,nodelist=range(8)).todense()
Out[3]: 
matrix([[0, 0, 0, 0, 0, 1, 1, 1],
        [0, 0, 0, 0, 0, 1, 1, 1],
        [0, 0, 0, 0, 0, 1, 1, 1],
        [0, 0, 0, 0, 0, 1, 1, 1],
        [0, 0, 0, 0, 0, 1, 1, 1],
        [1, 1, 1, 1, 1, 0, 0, 0],
        [1, 1, 1, 1, 1, 0, 0, 0],
        [1, 1, 1, 1, 1, 0, 0, 0]], dtype=int64)

The biadjacency format only has one section of the adjacency matrix

In [4]: from networkx.algorithms.bipartite import biadjacency_matrix

In [5]: biadjacency_matrix(G,range(5)).todense()
Out[5]: 
matrix([[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]], dtype=int64)

Upvotes: 1

Related Questions