递归地将子图添加到图中的所有节点

2024-05-29 10:51:17 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我有一个图形的数据结构,如下所示:

graph = {'graphData': {'nodes': List of Nodes,
                       'edges': List of Edges,}
         }

除此之外,我还有一个子图列表,如下所示:

Subgraphs

在第一步中,我将具有一个节点的子图(注释3下的SOI[3]添加到一起,形成如下内容:

SOI Subgraphs

下面的代码可以处理它:

^{pr2}$

上面的代码是: 对于第1张图中的每个独立子图,我使用函数merge\u entity_in_graph()找到任何子图是否有SOI节点。如果子图中存在SOI,则返回子图,并将其添加到由variablesGraph=dict()定义的结构的原始图变量中。在

在下一步中,我希望递归地添加图1中的所有这些建议,以添加到当前打开的节点中。在我的例子中,根据图2,这是Ec,Ep,E1。在

我期望的是:

enter image description here

如果有更多的子图,其中一个节点为Ef,Eq,Er,它们也会被递归地添加。在

我的问题是,我如何做到这一点?我写了一个方法如下,但不确定天气方法是否正确:

def add_other_subgraphs(self, subgraph, node, all_nodes, graph, coreference_graphs):
    """

    Args:
        subgraph (dict):
        node (dict):
        all_nodes (list):
        graph (dict):

    Returns:

    """

    subgraph_edges = subgraph['graphData']['edges']
    subgraph_nodes = subgraph['graphData']['nodes']

    for node in subgraph_nodes:
        if node not in graph['graphData']['nodes']:
            graph['graphData']['nodes'].append(node)

    for edge in subgraph_edges:
        if edge not in graph['graphData']['edges']:
            graph['graphData']['edges'].append(edge)

    for node in graph['graphData']['nodes']:
        coreference_graph = self.fetch_coreference_graph(node, coreference_graphs)
        if coreference_graph:
            graph = self.add_other_subgraphs(coreference_graph, node, all_nodes, graph, coreference_graphs)
        else:
            return graph

    return graph

对于每个节点(即Ec、Ep、E1


Tags: inselfnodefor节点alldictgraph

热门问题