【问题标题】:Implementing Depth First Search into C# using List and Stack使用 List 和 Stack 在 C# 中实现深度优先搜索
【发布时间】:2011-08-13 20:58:32
【问题描述】:

我想创建一个深度优先搜索,我已经取得了一定的成功。

到目前为止,这是我的代码(除了我的构造函数,请注意 Vertex 和 Edge 类只包含属性,这里没有重要的发布):

private Stack<Vertex> workerStack = new Stack<Vertex>();
private List<Vertex> vertices = new List<Vertex>();
private List<Edge> edges = new List<Edge>();

private int numberOfVertices;
private int numberOfClosedVertices;
private int visitNumber = 1;

private void StartSearch()
{
    // Make sure to visit all vertices
    while (numberOfClosedVertices < numberOfVertices && workerStack.Count > 0)
    {
        // Get top element in stack and mark it as visited
        Vertex workingVertex = workerStack.Pop();
        workingVertex.State = State.Visited;

        workingVertex.VisitNumber = visitNumber;
        visitNumber++;

        numberOfClosedVertices++;

        // Get all edges connected to the working vertex
        foreach (Vertex vertex in GetConnectedVertices(workingVertex))
        {
            vertex.Parent = workingVertex;
            workerStack.Push(vertex);
        }
    }
}

private List<Vertex> GetConnectedVertices(Vertex vertex)
{
    List<Vertex> vertices = new List<Vertex>();

    // Get all vertices connected to vertex and is unvisited, then add them to the vertices list
    edges.FindAll(edge => edge.VertexSource == vertex && edge.VertexTarget.State == State.Unvisited).ForEach(edge => vertices.Add(edge.VertexTarget));

    return vertices;
}

它的工作方式是访问所有顶点,但顺序不正确。

以下是我的访问方式与维基百科的比较:

我的好像转了个弯,从右到左开始了。

你知道是什么原因造成的吗? (也非常感谢任何关于我的实施的建议)

编辑:我得到了答案,但仍想显示 GetConnectedVertices 方法的最终结果:

private List<Vertex> GetConnectedVertices(Vertex vertex)
{
    List<Vertex> connectingVertices = new List<Vertex>();

    (from edge in edges
     where edge.VertexSource == vertex && edge.VertexTarget.State == State.Unvisited
     select edge).
     Reverse().
     ToList().
     ForEach(edge => connectingVertices.Add(edge.VertexTarget));

    return connectingVertices;
}

【问题讨论】:

    标签: c# search vertex depth edges


    【解决方案1】:

    问题在于您搜索元素的顺序。 StartSearch 中的 for each 不保证元素顺序。 FindAllGetConnectedVertices 方法中也没有。让我们看看这一行:

    edges.FindAll(edge => edge.VertexSource == vertex && edge.VertexTarget.State == State.Unvisited).ForEach(edge => vertices.Add(edge.VertexTarget));
    

    您应该添加OrderBy() 以确保所需的顺序。

    【讨论】:

    • 颠倒列表的顺序(使用 Reverse())似乎可以解决问题。谢谢你的回答,让我找到了正确的方向。
    • @Dumpen:你可以比使用 Reverse 更有效地做到这一点。建立另一个堆栈!这样它会自动“向后”枚举。
    • 哦,那是切肉刀。你的意思是这样吗? ` edge.FindAll(edge => edge.VertexSource == vertex && edge.VertexTarget.State == State.Unvisited)。 ForEach(edge => connectedVertices.Push(edge.VertexTarget)); `(对不起,我似乎无法进行代码格式化)
    【解决方案2】:

    项目将按照与它们被推入堆栈的相反顺序从堆栈中弹出:

    stach.push() 结果:1 2 3 4 5

    stack.pop() 结果:5 4 3 2 1(所以:从右到左)

    【讨论】:

      【解决方案3】:

      你可能会喜欢这个:

              public static bool DepthFirstSearch<T>(this IEnumerable<T> vertices, T rootVertex, T targetVertex, Func<T, IEnumerable<T>> getConnectedVertices, Func<T, T, bool> matchFunction = null)
          {
              if (getConnectedVertices == null)
              {
                  throw new ArgumentNullException("getConnectedVertices");
              }
              if (matchFunction == null)
              {
                  matchFunction = (t, u) => object.Equals(t, u);
              }
              var directlyConnectedVertices = getConnectedVertices(rootVertex);
              foreach (var vertex in directlyConnectedVertices)
              {
                  if (matchFunction(vertex, targetVertex))
                  {
                      return true;
                  }
                  else if (vertices.DepthFirstSearch(vertex, targetVertex, getConnectedVertices, matchFunction))
                  {
                      return true;
                  }
              }
              return false;
          }
      

      【讨论】:

      • 谢谢。我当然会对此进行研究,但是目前我的脑海中似乎对当前脚本有足够的了解:)
      • 递归并不总是一个好主意,我会小心使用这个解决方案。
      【解决方案4】:

      我的好像是转过来的,从右到左开始。你知道是什么原因造成的吗?

      正如其他人所指出的,您正在按从左到右的顺序在堆栈上推送节点到访问下一个。这意味着它们从右到左弹出,因为堆栈颠倒了顺序。堆栈是后进先出的。

      您可以通过让 GetConnectedVertices 构建堆栈而不是列表来解决此问题。这样,连接的顶点被反转两次,一次是在返回的堆栈上,一次是在真正的堆栈上。

      也非常感谢任何关于我的实施的建议

      我想这个实现是可行的,但它有很多基本问题。如果向我展示了该代码以供审查,我会说:

      首先,假设您想同时对该数据结构进行两次深度优先搜索。要么是因为您在多个线程上执行此操作,要么是因为您有一个嵌套循环,其中内循环为与外循环不同的元素执行 DFS。发生什么了?它们相互干扰,因为两者都试图改变“State”和“VisitNumber”字段。像搜索这样应该是“干净”的操作实际上会使您的数据结构“脏”,这是一个非常糟糕的主意。

      这样做还使您无法使用持久不可变数据来表示图表的冗余部分。

      另外,我注意到您省略了清理代码。 “状态”什么时候恢复到原来的值?如果您进行 DFS 会怎样?它会立即失败,因为根已经被访问过。

      出于所有这些原因,更好的选择是将“已访问”状态保留在其自己的对象中,而不是在每个顶点中。

      接下来,为什么一个类的所有状态对象都是私有变量?这是一个简单的算法;没有必要为它构建一个完整的类。深度优先搜索算法应该将要搜索的图作为形式参数,而不是对象状态,并且应该在局部变量而不是字段中根据需要维护自己的局部状态。

      接下来,图的抽象是……好吧,它不是抽象。这是两个列表,一个顶点和一个边。我们怎么知道这两个列表是一致的?假设有一些顶点不在顶点列表中但在边列表中。你如何防止这种情况发生?你想要的是一个图形抽象。让图抽象实现来操心如何表示边并找到邻居。

      接下来,您对 ForEach 的使用既合法又常见,但这让我很头疼。使用所有 lambdas 很难阅读您的代码并对其进行推理。我们有一个非常好的“foreach”声明。使用它。

      接下来,您正在对“父”属性进行变异,但完全不清楚该属性的用途或在遍历期间发生变异的原因。任意图中的顶点没有“父节点”,除非图是树,如果图是树,则无需跟踪“已访问”状态;树中没有循环。这里发生了什么?这段代码很奇怪,没有必要执行DFS。

      接下来,您的名为 GetConnectedVertices 的辅助方法是一个谎言。它没有得到连接的顶点,它得到连接的尚未访问的顶点。名称谎言的方法非常混乱。

      最后,这声称是深度优先搜索,但 它什么都不搜索! 正在搜索的东西在哪里?返回的结果在哪里?这根本不是搜索,而是遍历。

      重新开始。你想要什么? 给定起始顶点的图的深度优先遍历。然后实施。首先定义您要遍历的内容。一个图表。您需要从图表中获得什么服务?一种获取相邻顶点集合的方法:

      interface IGraph
      {
          IEnumerable<Vertex> GetNeighbours(Vertex v);
      }
      

      你的方法返回什么?深度优先顺序的顶点序列。需要做些什么?一个起始顶点。好的:

      static class Extensions
      {
          public static IEnumerable<Vertex> DepthFirstTraversal(
              this IGraph graph, 
              Vertex start) 
          { ... }
      }
      

      我们现在有了深度优先搜索的简单实现;您现在可以使用 Where 子句:

      IGraph myGraph = whatever;
      Vertex start = whatever;
      Vertex result = myGraph.DepthFirstTraversal(start)
                             .Where(v=>something)
                             .FirstOrDefault();
      

      好的,那么我们将如何实现该方法,以便在不破坏图形状态的情况下进行遍历?维护自己的外部状态:

      public static IEnumerable<Vertex> DepthFirstTraversal(
          this IGraph graph, 
          Vertex start) 
      {
          var visited = new HashSet<Vertex>();
          var stack = new Stack<Vertex>();
      
          stack.Push(start);
      
          while(stack.Count != 0)
          {
              var current = stack.Pop();
      
              if(!visited.Add(current))
                  continue;
      
              yield return current;
      
              var neighbours = graph.GetNeighbours(current)
                                    .Where(n=>!visited.Contains(n));
      
              // If you don't care about the left-to-right order, remove the Reverse
              foreach(var neighbour in neighbours.Reverse()) 
                  stack.Push(neighbour);
          }
      }
      

      看看这样更简洁更短了吗?没有状态突变。不要乱用边缘列表。没有名字不好的辅助函数。代码实际上做了它所说的:遍历一个图。

      我们还获得了迭代器块的好处;即,如果有人将其用于 DF 搜索,则在满足搜索条件时放弃迭代。如果我们早点找到结果,我们就不必进行完整的遍历。

      【讨论】:

      • @Dumpen:这取决于图表的性质。假设 V 和 E 是顶点和边的数量。如果“分支因子”E/V(即每个顶点的平均边数)很低,那么您的建议是有效的。如果它很高——因为每个顶点都连接到几乎每个顶点——那么存储每个顶点的所有信息是低效的;而是制作一个布尔值的二维“边表”并让图形保持它。
      • @Dumpen:编译器不需要我的帮助;它知道数据类型是什么。在这种情况下是否使用“var”的问题是 人类 是否需要帮助来解决这个问题。你是人;您真的需要查看“Stack stack = new Stack()”才能确定堆栈是一个顶点堆栈吗?我对此表示怀疑。冗余使代码更难阅读,而不是更容易,所以消除它。有关此主题的更多想法,请参阅blogs.msdn.com/b/ericlippert/archive/2011/04/20/…
      • @Atlas2k:你能问一个更具体的问题吗?这行代码的意思是“获取我还没有访问过的邻居”。我们通过一组访问节点来表示这一点,并且我们正在获取尚未在访问节点集中的所有邻居。你是在问为什么我们不想访问我们已经访问过的节点?因为问题是“一阶深度访问每个节点”。
      • @Atlas2k:您要搜索的关键字是“lambda 表达式”和“LINQ”。
      • @ziezi 你的问题很容易回答你自己。创建一个树形图并运行算法。它以什么顺序遍历节点?
      【解决方案5】:

      我为任何 T 概括了 @Eric 的 DFS 遍历代码,以使任何有孩子的类型都能正常工作 - 我想我会分享:

      public static IEnumerable<T> DepthFirstTraversal<T>(
          T start,
          Func<T, IEnumerable<T>> getNeighbours)
      {
          var visited = new HashSet<T>();
          var stack = new Stack<T>();
          stack.Push(start);
      
          while (stack.Count != 0)
          {
              var current = stack.Pop();
      
              if (!visited.Add(current))
                  continue;
      
              yield return current;
      
              var neighbours = getNeighbours(current).Where(node => !visited.Contains(node));
      
              // If you don't care about the left-to-right order, remove the Reverse
              foreach(var neighbour in neighbours.Reverse())
              {
                  stack.Push(neighbour);
              }
          }
      }
      

      示例用法:

      var nodes = DepthFirstTraversal(myNode, n => n.Neighbours);
      

      【讨论】:

      • 我认为这可能包含一个错误,正如我在 @EricLippert 答案中的 cmets 中提到的那样。
      【解决方案6】:

      这是我的实现,一个堆栈就足够了。在 foreach 循环之前进行反向操作。

          /// <summary>
          /// Depth first search implementation in c#
          /// </summary>
          /// <typeparam name="T">Type of tree structure item</typeparam>
          /// <typeparam name="TChilds">Type of childs collection</typeparam>
          /// <param name="node">Starting node to search</param>
          /// <param name="ChildsProperty">Property to return child node</param>
          /// <param name="Match">Predicate for matching</param>
          /// <returns>The instance of matched result, null if not found</returns>
          public static T DepthFirstSearch<T, TChilds>(this T node, Func<T, TChilds> ChildsProperty, Predicate<T> Match) 
              where T:class
          {
              if (!(ChildsProperty(node) is IEnumerable<T>))
                  throw new ArgumentException("ChildsProperty must be IEnumerable<T>");
      
              Stack<T> stack = new Stack<T>();
              stack.Push(node);
              while (stack.Count > 0) {
                  T thisNode = stack.Pop();
                  #if DEBUG
                  System.Diagnostics.Debug.WriteLine(thisNode.ToString());
                  #endif
                  if (Match(thisNode))
                      return thisNode;
                  if (ChildsProperty(thisNode) != null) {
                      foreach (T child in (ChildsProperty(thisNode) as IEnumerable<T>).Reverse()) 
                          stack.Push(child);
                  }
              }
              return null;
          }
      

      【讨论】:

        猜你喜欢
        • 2014-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 2011-01-31
        • 1970-01-01
        相关资源
        最近更新 更多