python - 如何将 quotient_graph 结果写入文件

标签 python networkx

这是一个创建“问题”的 MWE

>>> A = nx.complete_bipartite_graph(2, 3)
>>> same_neighbors = lambda u, v: (u not in A[v] and v not in A[u] and A[u] == A[v])
>>> B = nx.quotient_graph(A, same_neighbors)
>>> B.nodes()
NodeView((frozenset({0, 1}), frozenset({2, 3, 4})))
>>> B[frozenset({0, 1})]
AtlasView({frozenset({2, 3, 4}): {'weight': 6}})
>>> B.nodes[frozenset({0, 1})]
{'graph': <networkx.classes.graph.Graph object at 0x12b066e80>, 'nnodes': 2, 'nedges': 0, 'density': 0}

我相信节点上的这个graph属性指定了该节点来自原始图中的子图,但我不确定。如果有人可以验证那就太好了。

无论如何,这个graph属性都会阻止我使用nx.write_graphml函数,因为子图不能用作数据格式。特别是它提出了

networkx.exception.NetworkXError: GraphML writer does not support <class 'networkx.classes.graph.Graph'> as data values.

现在我实际上不需要 graphml 文件中的子图,因此删除该数据可能是将图形写入文件的最佳方法。最好的方法是什么?

最佳答案

I believe this graph attribute on the node is specifying the subgraph which this node comes from in the original graph, but I'm not sure. If someone could verify that would be nice.

是的,您是对的,假设 graph 属性实际上是基于原始图指定子图。商图的文档中提到了它。查看node_data的描述,你会发现:

node_data (function) – This function takes one argument, B, a set of
nodes in G, and must return a dictionary representing the node data
attributes to set on the node representing B in the quotient graph. If
None, the following node attributes will be set:

- graph, the subgraph of the graph G that this block represents,
- nnodes, the number of nodes in this block,
- nedges, the number of edges within this block,
- density, the density of the subgraph of G that this block represents.

您还可以通过键入

自行查看子图
for node in B.nodes():
    print("Information for subgraph ", node)
    print(B.nodes[node]['graph'].nodes())

# Output:
# Information for subgraph  frozenset({0, 1})
# Nodes are  [0, 1]
# Information for subgraph  frozenset({2, 3, 4})
# Nodes are  [2, 3, 4]

Now I don't actually need the subgraph in the graphml file, so just dropping that data is probably the best way for me to get the graph written to a file. What is the best way to do this?

您可以简单地创建一个新图并仅添加节点和边,并丢弃任何其他数据。 (同样,您正确地假设此信息与以 GraphML 格式写入数据无关,因为您只需要节点和边)。

在进一步讨论之前,让我们检查一下商图的 EdgeView:

B.edges(data=True)
# EdgeDataView([(frozenset({0, 1}), frozenset({2, 3, 4}), {'weight': 6})])

注意,边的data是如何以weight为键的字典。这个信息。将在后续代码中有用。

现在,创建一个新图表,并直接从商图表添加边。

H = nx.Graph()

# Here B is the quotient graph
for u,v,d in B.edges(data=True):
    # Notice how the weight is assigned to the new graph
    H.add_edge(u, v, weight=d['weight'])

如果你想验证新图是否具有相同的结构,可以使用 nx.is_isomorphic 进行检查。

nx.is_isomorphic(H, B)
# True

现在您可以简单地将图形写入 GraphML 格式

nx.readwrite.graphml.write_graphml(H, "my_graph.graphml")

更多信息,您可以查看this Google Colab Notebook ,使用上面的工作代码。

引用文献:

关于python - 如何将 quotient_graph 结果写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60217286/

相关文章:

python - 在 Python 3.x 中,为什么磁盘上没有 itertools 共享对象?

python - 在 Python 中以分别显示所有边的方式绘制有向图

python - 如何在 pandas dataframe python 中将名称的第一个字符转换为大写?

python - Matplotlib x 轴过度拥挤的标签渲染

python - 在 python matplotlib.dates 中绘制毫秒图

python - 带有 networkX 的子树

python - Networkx 有向图 Python

python networkx 在某些条件下删除节点和边

python - 在 networkX python 中,是否可以添加具有相同 ID 的相同内容?

Python:使用返回值引用另一个变量?