gpt4 book ai didi

python - networkx 子图作为节点

转载 作者:太空宇宙 更新时间:2023-11-03 16:59:31 32 4
gpt4 key购买 nike

我想替换子图S Networkx 图的 G通过单个节点N再次包含整个子图 S 。我需要这样做,因为我需要 N 的优势到我的图表的其他节点。

因为我没有让网络x的子图方法起作用,所以我编写了自己的代码来做到这一点。但我对结果感到困惑。

这是一个小示例脚本:

import networkx as nx
from copy import deepcopy
from collections import deque


class XGraph(nx.MultiDiGraph):

def dographthings(self, graph_edges, graph_nodes, subgraph_nodes):

self.add_edges_from(graph_edges)

subgraph = deepcopy(self)

# remove all nodes and their transitive children from subgraph,that are
# not in subgraph_nodes
remove_subtree(deque((set(graph_nodes) - set(subgraph_nodes))), subgraph)

# remove all nodes from self that are now in subgraph
self.remove_nodes_from(subgraph)


print "subgraph:"
print type(subgraph)
for node in subgraph.nodes_iter():
print node

print "self:"
print type(self)
for node in self.nodes_iter():
print node

self.add_node(subgraph)

print self.node[subgraph]




def remove_subtree(nodes, graph):
"""
Removes all nodes that are successors of the nodes in ``nodes``.
Is robust for cyclic graphs.

Parameters
----------
graph : referance to networkx.graph
graph to remove nodes from
nodes : deque of nodes-ids
the nodes the successors of which to remove from graph
"""
to_remove = set()
to_add = list()
for node in nodes:
to_remove.add(node)
if node in graph:
to_add.extend(graph.successors(node))
graph.remove_node(node)
for node in to_remove:
nodes.remove(node)
for node in to_add:
nodes.append(node)
if len(nodes) > 0:
graph = remove_subtree(nodes, graph)



g = XGraph()
g.dographthings([(1,2),(2,3),(2,4),(1,5)], [1,2,3,4,5], [3,2,1])

类(class)XGraph有一种方法可以向图中添加边,并如上所述构建子图。然后,当我迭代图和子图的节点时,一切似乎都是正确的。然后当我添加子图作为一个节点,并通过 get_item 方法访问它,它似乎变成了一个空字典而不是一个MultiDiGraph,就像将其添加为节点之前一样。

脚本的输出是这样的:

subgraph:
<class '__main__.XGraph'>
1
2
3
self:
<class '__main__.XGraph'>
4
5
{}

为什么我的子图在添加为节点后会变成字典,它的所有数据去了哪里?

编辑

我错误地访问了节点。这样做是有效的:

for node in self.nodes_iter(data=True):
if isinstance(node[0], nx.MultiDiGraph):
print "this is the subgraph-node:"
print node
print "these are its internal nodes:"
for x in node[0].nodes_iter():
print x
else:
print "this is an atomic node:"
print node

输出:

this is the subgraph-node:
(<__main__.XGraph object at 0xb5ec21ac>, {})
these are its internal nodes:
1
2
3
this is an atomic node:
(4, {})
this is an atomic node:
(5, {})

最佳答案

我不太明白为什么你的代码不起作用。这是一个可能有帮助的小例子

import networkx as nx

G = nx.Graph()
G.add_path([1,2,3,4])
S = G.subgraph([2,3]) # S is the graph 2-3
# add the subgraph as a node in the original graph
G.add_node(S)
# connect S to the neighbors of 2 and 3 and remove 2,3
for n in S:
nbrs = set(G.neighbors(n))
for nbr in nbrs - set([S]):
G.add_edge(S,nbr)
G.remove_node(n)
print(G.nodes()) # 1,4, <graph id>
print(G.edges()) # [(1, <graph id>), (<graph id>,4)]

关于python - networkx 子图作为节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108949/

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