简体   繁体   English

继承networkx图并使用nx.connected_component_subgraphs

[英]inheriting networkx Graph and using nx.connected_component_subgraphs

I am trying to subclass a networkx Graph object. 我试图子类化networkx Graph对象。 My __init__ has one variable passed to it. 我的__init__传递了一个变量。 However, this means that when I try to use the following method which calls connected_component_iter , 但是,这意味着当我尝试使用以下方法调用connected_component_iter

def connected_component_iter(self):
    """
    Yields connected components.
    """
    assert self.is_built is True
    for subgraph in nx.connected_component_subgraphs(self):
        yield subgraph

I get this error: 我收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "src/unitigGraph.py", line 163, in connected_component_iter
    def connected_component_iter(self):
  File "/Library/Python/2.7/site-packages/networkx/algorithms/components/connected.py", line 94, in connected_component_subgraphs
    yield G.subgraph(c).copy()
  File "/Library/Python/2.7/site-packages/networkx/classes/graph.py", line 1486, in subgraph
    H = self.__class__()
TypeError: __init__() takes exactly 2 arguments (1 given)

I would really prefer not to remove my initialization class variable. 我真的不希望删除我的初始化类变量。 Is there a way I can still use the connected_component_iter method from Graph ? 有没有办法我仍然可以使用Graphconnected_component_iter方法?

You could work around the problem by giving your new initialization variable, val , a default value: 您可以通过给您的新初始化变量val一个默认值来解决此问题:

class MyGraph(nx.Graph):
    def __init__(self, data=None, val=None, **attr):
        super(MyGraph, self).__init__()
        self.val = val

Above, the default value for val is None. 上面的val的默认值为None。 So 所以

H = self.__class__()

would initialize a new subgraph with val equal to None . 将用val等于None初始化一个新的子图。

However, it seems likely that you'd like the subgraph to inherit the same value of val as the parent MyGraph. 但是,您似乎希望子图继承与父MyGraph相同的val值。 In that case, we'd need to change 在这种情况下,我们需要进行更改

    H = self.__class__()

to

    H = self.__class__(val=self.val)

We can do this by overriding the subgraph method by defining our slightly altered version in MyGraph . 我们可以通过在MyGraph定义稍有改动的版本来覆盖subgraph方法来实现此目的。 For example, the code might look something like: 例如,代码可能类似于:

import networkx as nx
class MyGraph(nx.Graph):
    def __init__(self, data=None, val=None, **attr):
        super(MyGraph, self).__init__()
        self.val = val
        self.is_built = True

    def connected_component_iter(self):
        """
        Yields connected components.
        """
        assert self.is_built is True
        for subgraph in nx.connected_component_subgraphs(self):
            yield subgraph

    def subgraph(self, nbunch):
        bunch =self.nbunch_iter(nbunch)
        # create new graph and copy subgraph into it
        H = self.__class__(val=self.val)
        # copy node and attribute dictionaries
        for n in bunch:
            H.node[n]=self.node[n]
        # namespace shortcuts for speed
        H_adj=H.adj
        self_adj=self.adj
        # add nodes and edges (undirected method)
        for n in H.node:
            Hnbrs={}
            H_adj[n]=Hnbrs
            for nbr,d in self_adj[n].items():
                if nbr in H_adj:
                    # add both representations of edge: n-nbr and nbr-n
                    Hnbrs[nbr]=d
                    H_adj[nbr][n]=d
        H.graph=self.graph
        return H

G = MyGraph(val='val')
G.add_edges_from([(0, 1), (1, 2), (1, 3), (3, 5), (3, 6), (3, 7), (4, 8), (4, 9)])

for subgraph in G.connected_component_iter():
    print(subgraph.nodes(), subgraph.val)

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

相关问题 networkx 库更新 nx.connected_component_subgraphs(G) 后,此错误生成 - after networkx library update nx.connected_component_subgraphs(G), this error genarated max(nx.connected_component_subgraphs(),) 的错误没有属性“connected_component_subgraphs” - Error for max(nx.connected_component_subgraphs(),) no attribute 'connected_component_subgraphs' 从 NetworkX 图中查找连接组件内的子图 - Find Subgraphs inside a Connected Component from a NetworkX Graph 如何在NetworkX中仅绘制前50-100个连接的组件子图; 一次绘制多个子图 - How to graph only top 50-100 connected component subgraphs in NetworkX; drawing multiple subgraphs at once 完全连接来自networkx中较大图形的子图 - completely connected subgraphs from a larger graph in networkx AttributeError:模块“networkx”没有属性“connected_component_subgraphs” - AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs' 如何使用.networkx 查找强连通分量的子图 - How to find subgraphs of strongly connected components using networkx Networkx-使用节点属性的子图 - Networkx - Subgraphs using node attributes Networkx:提取包含给定节点的连通组件(有向图) - Networkx: extract the connected component containing a given node (directed graph) 如何在networkx中创建连接图 - How to create a connected graph in networkx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM