【问题标题】:plot bipartite graph created with Networkx using igraph使用 igraph 绘制使用 Networkx 创建的二分图
【发布时间】:2016-07-06 10:07:23
【问题描述】:

我无法在 igraph 中读取我的二分矩阵。我使用 Networkx 创建了二分图并将它们导出为一个二元邻接矩阵:

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

然后我使用 igraph 在 R 中导入 10x10 矩阵:

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

这里是稀疏矩阵:

数据1 “dgCMatrix”类的 10 x 10 稀疏矩阵

 [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 . . . . . . . .

图表

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

所以这是错误的,因为它没有考虑到有两种类型的顶点(缺少属性部分)。所以我认为这是因为我导出图形的方式(使用 bipartite.biadjacency 矩阵),但是有没有办法绕过这个问题?无论是 igraph 读取矩阵的方式还是我在 Networkx 中导出的方式?

【问题讨论】:

    标签: r matrix networkx


    【解决方案1】:

    您可能只想要二部图的邻接矩阵表示

    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)
    

    biadjacency 格式只有邻接矩阵的一部分

    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)
    

    【讨论】:

    • 谢谢,成功了!我在循环中导出了矩阵,这导致列类型的丢失。我只做了g = bipartite.biadjacency_matrix(G,row_order=bipartite.sets(stockGraph[i])[0], column_order=bipartite.sets(stockGraph[i])[1]).todense()(所以我从bipartite.adjacency 矩阵中删除了format='csr'),它起作用了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多