繁体   English   中英

networkx子图作为节点

[英]networkx subgraph as node

我想替换一个子图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])

XGraph类具有一种将边添加到图形并如上所述构建子图的方法。 然后,当我遍历图和子图的节点时,一切似乎都是正确的。 然后,当我将子图添加为节点,并通过get_item-method访问它时,它似乎已经变成一个空字典,而不是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)]

我错误地访问了该节点。 这样做是可行的:

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, {})

我有一个图G,首先将其划分为子图。然后创建一个新图A,并为其添加节点和边。 所以在这里我有多个子图,我想用一个节点替换这些子图,并在它们之间添加边。 在下面,分区是一个字典,它将图G的节点映射到该节点所属的社区。 首先,我将位于同一社区中的节点放入列表中,并从该节点创建一个子图。之后,找到列表的邻居并将边缘添加到新图中。
此代码可用于有向图或无向图。

A = nx.Graph()
commlist = []

for com in set(partition.values()) :

    list_nodes = [nodes for nodes in partition.keys()if partition[nodes] == com]
    commlist.append(list_nodes)
    subgraph = G.subgraph(list_nodes)

    A.add_node(com)
    for node in list_nodes:
        nbrs = set(G.neighbors(node))
        for i in nbrs - set(list_nodes):
            A.add_edge(com, G.node[i]['community'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM