作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是创建“问题”的 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?
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(H, B)
# True
nx.readwrite.graphml.write_graphml(H, "my_graph.graphml")
关于python - 如何将 quotient_graph 结果写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60217286/
这是创建“问题”的 MWE >>> A = nx.complete_bipartite_graph(2, 3) >>> same_neighbors = lambda u, v: (u not in
我是一名优秀的程序员,十分优秀!