繁体   English   中英

将二部图转换为邻接矩阵python

[英]convert bipartite graph to adjacency matrix python

我正在尝试转换以下格式的边缘列表

data = [('a', 'developer'),
         ('b', 'tester'),
        ('b', 'developer'),
         ('c','developer'),
         ('c', 'architect')]

其中邻接矩阵的形式为

      developer     tester    architect
a        1            0          0
b        1            1          0
c        1            0          1

我想以以下格式存储矩阵

 1    0    0
 1    1    0
 1    0    1

任何帮助都受到高度赞赏

这可以通过networkx轻松完成

from operator import itemgetter

import networkx as nx
from networkx.algorithms.bipartite import biadjacency_matrix

def to_adjacency_matrix(data):
    g = nx.DiGraph()
    g.add_edges_from(data)
    partition_1 = set(map(itemgetter(0), data))
    return biadjacency_matrix(g, partition_1).toarray()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM