简体   繁体   English

首先尝试在Python中遍历图的广度

[英]Trying to iterate through a graph breadth first in Python

In my code, I am essentially trying to create an implementation of a data flow network. 在我的代码中,我实质上是在尝试创建数据流网络的实现。 Although the particularities of how I am going about doing this aren't particularly important, I need some help to make the program go through the graph in a breadth first fashion. 尽管我执行该操作的特殊性并不是特别重要,但是我需要一些帮助以使程序以广度优先的方式通过图表。 When I do this with my code: 当我用代码执行此操作时:

def traverse(self):
    source = self._nodelist[0]
    self.sand_pile(source)
    return

def sand_pile(self, start):
    for sink in start._sinks:
        //ALGORITHM FOR SENDING DATA HERE
    for s in start._sinks:
        self.sand_pile(s)
    return

The compiler correctly goes through the first node and its sinks breadth first, but then when it goes on to repeat the process for the sinks, it starts to go depth first. 编译器正确地通过了第一个节点,并且首先到达了接收器的宽度,但是随后继续对接收器重复该过程时,它首先开始进入深度。

Another way of explaining this is the following: if the source of the graph has two or three sinks, each of which have two or three of their own sinks, the compiler will print them all out and pass the value to them successfully, but then will proceed to go depth first for the rest of the nodes until it reaches the end. 另一种解释方式如下:如果图的源有两个或三个接收器,每个接收器都有两个或三个自己的接收器,则编译器将全部打印出来并将值成功传递给它们。将继续对其余节点进行深度操作,直到到达终点为止。 Where am I going wrong in my logic? 我的逻辑哪里出问题了?

PS If I am not explaining anything well, please leave a comment so I can clarify. 附注:如果我不能很好地解释任何内容,请发表评论以便澄清。

You are mixing BFS ( breadth first search ) and DFS ( depth first search ) in your approach. 您正在混合BFS( 广度优先搜索 )和DFS( 深度优先搜索 )。

When sending data in your sand_pile method, you iterate horizontally through all children of the current node, which probably is your approach of implementing a BFS. 在使用sand_pile方法发送数据时,您将在当前节点的所有子级中进行水平迭代,这可能是实现BFS的方法。 But as soon as the first level of children (or sinks in your case) is done, you perform a recursion step on each of the child nodes. 但是,一旦完成子级(或您的情况下的接收器 )的第一级,就对每个子节点执行递归步骤。 This essentially results in a schema like this: 这实质上导致了这样的模式:

  • Start sand_pile on node on level 0 (root node) 在级别0节点(根节点)上启动sand_pile
  • Distribute data to all its children on level 1 将数据分发给第1级的所有子级
  • Start sand_pile on first children of level 1 在第1级的第一个孩子上开始sand_pile
  • Distribute data to all of its children on level 2 将数据分发给其第2级的所有子级
  • ... keep on going DFS with out-of-line data distribution ...通过离线数据分发继续进行DFS

The flaw is, that you employ recursion for a BFS. 缺陷在于,您为BFS使用了递归。

You can use a recursive descent (ie stack-like iteration) for DFS, but for BFS, you should create a buffer that contains all nodes in the desired order (ie queue-like iteration) 您可以对DFS使用递归下降( 例如,类似堆栈的迭代),但是对于BFS,您应该创建一个缓冲区,该缓冲区包含所需顺序的所有节点( 例如,类似队列的迭代)

The steps can then be like the following 步骤如下

  1. Get maximum depth of your tree 获得树的最大深度
  2. For each depth level, put all nodes that reside on that level into a list 对于每个深度级别,将驻留在该级别上的所有节点放入列表中
  3. Concatenate all lists to obtain something like [lvl0_node0, lvl1_node0, lvl1_node1, lvl1_node2, lvl2_node_0, ..., lvlN_nodeM] 串联所有列表以获得类似[lvl0_node0, lvl1_node0, lvl1_node1, lvl1_node2, lvl2_node_0, ..., lvlN_nodeM]
  4. Iterate over that queue 遍历该队列

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

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