Graph 图:节点依赖关系

Graph 图:节点依赖关系,graph,dependencies,Graph,Dependencies,我有一个这样的图表: 例如,我想知道节点8的依赖关系,它们是:1,2,3,5。有人能给我一些代码或伪代码来解决这个问题吗 谢谢依赖关系结构是一个很好的解决方案。我用2种方法(python)介绍了类似的情况: nodes\u to\u update(to\u proc)参数是一组要开始的节点(例如set([8])。返回两组节点:给定节点所依赖的所有节点集和叶节点集。其思想是递归地访问依赖于被访问节点的所有节点 sequence\u to\u update(to\u proc)参数如上所示。返回(

我有一个这样的图表:

例如,我想知道节点8的依赖关系,它们是:1,2,3,5。有人能给我一些代码或伪代码来解决这个问题吗

谢谢

依赖关系结构是一个很好的解决方案。我用2种方法(python)介绍了类似的情况:

  • nodes\u to\u update(to\u proc)
    参数是一组要开始的节点(例如set([8])。返回两组节点:给定节点所依赖的所有节点集和叶节点集。其思想是递归地访问依赖于被访问节点的所有节点
  • sequence\u to\u update(to\u proc)
    参数如上所示。返回(有序的)节点列表,以便列表中的节点仅依赖于列表中之前的节点。通过将叶节点添加到有序列表,并将节点集更新为已处理(所有和叶节点),可以完成此操作
依赖节点通过方法
向下节点(o\u id)
获取,依赖节点通过
向上节点(o\u id)
获取

def nodes_to_update(self, to_proc):
    all_to_update, first_to_update = set(), set()
    while to_proc:
        n = to_proc.pop()
        all_to_update.add(n)
        ds = self.down_nodes(n)  # nodes on which n depends
        if not ds:
            first_to_update.add(n)
        else:
            to_proc.update(ds)
    return all_to_update, first_to_update

def sequence_to_update(self, to_proc):
    all_to_update, first_to_update = self.nodes_to_update(to_proc)
    update_in_order = []
    while first_to_update:
        n_id = first_to_update.pop()
        all_to_update.discard(n_id)
        update_in_order.append(n_id)
        # nodes which depend on n_id (intersection of upper nodes and all nodes to update)
        for up_id in (self.up_nodes(n_id) & all_to_update):
            if all(d not in all_to_update for d in self.down_nodes(up_id)):
                first_to_update.add(up_id)
    return update_in_order