MultiGraph.adj#

属性 MultiGraph.adj#

图的邻接对象,保存每个节点的邻居信息。

此对象是一个只读的类似字典的结构,其中键是节点,值是邻居字典。邻居字典的键是邻居节点,值是边键-数据字典。因此,G.adj[3][2][0]['color'] = 'blue' 将边 (3, 2, 0) 的颜色设置为 "blue"

遍历 G.adj 的行为类似于字典。有用的用法包括 for nbr, edgesdict in G.adj[n].items():

邻居信息也可以通过索引图来获取。

示例

>>> e = [(1, 2), (1, 2), (1, 3), (3, 4)]  # list of edges
>>> G = nx.MultiGraph(e)
>>> G.edges[1, 2, 0]["weight"] = 3
>>> result = set()
>>> for edgekey, data in G[1][2].items():
...     result.add(data.get("weight", 1))
>>> result
{1, 3}

对于有向图,G.adj 包含出边(后继)信息。