gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:35:06 25 4
gpt4 key购买 nike

这是创建“问题”的 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属性实际上是在原始图的基础上指定子图。它在 quotient 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 ,使用上述工作代码。

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

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com