引子

  • NetworkX github: https://github.com/networkx/n...
  • 本文对 NetworkX 2.5.1 实用

Tips

  • 导入包

    import networkx as nx               # 载入networkx包import matplotlib.pyplot as plt     # 用于画图G = nx.Graph()                      # 无向图
  • 连通重量

    edges = [('A', 'B'), ('C', 'E'), ('D', 'E'),       ('F', 'G'), ('F', 'H'), ('G', 'I'),       ('G', 'J'), ('H', 'J'), ('H', 'L'),       ('H', 'M'), ('I', 'K'), ('J', 'K'),       ('L', 'K'), ('L', 'H'), ('L', 'M')]G.add_edges_from(edges)nx.draw_networkx(G)plt.show()

  • 度最大的节点

    >>> max(G.degree, key=lambda x: x[1])('H', 4)
  • 节点按度降序

    for tup in sorted(G.degree, key=lambda x: x[1], reverse=True):  print(tup)
  • 连通重量的个数

    >>> nx.number_connected_components(G)3
  • 打印各连通重量的节点汇合

    for iset in nx.connected_components(G):  print(iset){'B', 'A'}{'E', 'C', 'D'}{'L', 'I', 'M', 'J', 'G', 'H', 'K', 'F'}
本文出自 qbit snap