【问题标题】:Alphabetical ordering in BFSBFS 中的字母顺序
【发布时间】:2017-12-31 08:53:00
【问题描述】:

我很难区分带有字母顺序的 BFS 和没有它的 BFS。

例如,在此图中查找生成树(从 E 开始)。

Starting G

添加 {E,B} 和 {E,C} 后

T after added EB and EC

我不确定是继续添加 {B,F} 还是 {C,F}。 非常感谢你。

【问题讨论】:

    标签: breadth-first-search alphabetical


    【解决方案1】:

    我不确定是继续添加 {B,F} 还是 {C,F}。非常感谢 很多。

    嗯,答案取决于您在BFS 算法的队列中添加顶点BC 的顺序。如果你看一下算法:

    BFS (G, s)      //Where G is the graph and s is the Source Node
    
      let Q be queue.
      Q.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked.
    
      mark s as visited.
      while ( Q is not empty)
           //Removing that vertex from queue,whose neighbour will be visited now
           v  =  Q.dequeue( )
    
          //processing all the neighbours of v  
          for all neighbours w of v in Graph G
               if w is not visited 
                    Q.enqueue( w ) //Stores w in Q to further visit its neighbours                       
    
          mark w as visited.
    

    很明显,它没有指定应该是什么顺序将顶点的邻居排入队列。

    所以如果你按顺序访问 E 的邻居: B , C ,那么显然是由于 FIFO 队列数据结构的属性,节点 B 将在 C 之前出队(从队列中取出),您将拥有边 B--F时间>。如果顺序是 CB,那么边缘将是 C--F 出于类似的原因。

    一旦你理解了伪代码,你就会非常清楚地理解它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-29
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      相关资源
      最近更新 更多