2016-01-31 6 views
1

Я хочу заменить подграф S сетевого графика G на один узел N, который снова содержит весь подграф S. Мне нужно сделать это, потому что мне нужен край от N к другим узлам моего графика.networkx subgraph as node

Поскольку я не получил метод подграфа сети 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, он, кажется, стал пустым словарем, а не MultiDiGraph, как это было до добавления его в качестве узла.

Выход сценария заключается в следующем:

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

Почему мой подграф стал словарь по добавляемого в качестве узла и куда идут все его данные?

EDIT

Я неправильно обратился к узлу. Делая это, как это работает:

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

ответ

1

Я не совсем понимаю, почему ваш код не работает. Вот небольшой пример, который может помочь

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)] 
+0

спасибо. Я понял, что я просто ошибся при доступе к узлу. –

0

Я неправильно обратился к узлу. Делая это, как это работает:

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, {}) 
Смежные вопросы