标签: depth-first-search

什么时候使用深度优先搜索(DFS)与广度优先搜索(BFS)是否可行?

我理解DFS和BFS之间的区别,但是我很想知道何时使用一个比另一个更实用?

任何人都可以举例说明DFS如何胜过BFS,反之亦然?

algorithm graph-theory breadth-first-search depth-first-search graph-algorithm

312
推荐指数
11
解决办法
22万
查看次数

广度优先与深度优先

遍历树/图时,广度优先和深度之间的区别首先是什么?任何编码或伪代码示例都会很棒.

algorithm breadth-first-search tree-traversal depth-first-search

166
推荐指数
3
解决办法
11万
查看次数

为什么DFS而不是BFS在图中查找周期

主要是DFS用于在图中找到循环而不是BFS.有什么原因?两者都可以在遍历树/图时查找是否已访问过节点.

algorithm tree graph breadth-first-search depth-first-search

73
推荐指数
5
解决办法
6万
查看次数

迭代DFS与递归DFS和不同元素顺序

我编写了一个递归DFS算法来遍历图:

void Graph<E, N>::DFS(Node n)
{
    std::cout << ReadNode(n) << " ";

    MarkVisited(n);

    NodeList adjnodes = Adjacent(n);

    NodeList::position pos = adjnodes.FirstPosition();

    while(!adjnodes.End(pos))
    {
        Node adj = adjnodes.ReadList(pos);

        if(!IsMarked(adj))
            DFS(adj);

        pos = adjnodes.NextPosition(pos);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我用堆栈编写了迭代DFS算法:

template <typename E, typename N>
void Graph<E, N>::IterativeDFS(Node n)
{
    Stack<Node> stack;

    stack.Push(n);

    while(!stack.IsEmpty())
    {
        Node u = stack.Read();

        stack.Pop();

        if(!IsMarked(u))
        {
            std::cout << ReadNode(u) << " ";

            MarkVisited(u);

            NodeList adjnodes = Adjacent(u);

            NodeList::position pos = adjnodes.FirstPosition();

            while(!adjnodes.End(pos))
            {
                stack.Push(adjnodes.ReadList(pos));

                pos = adjnodes.NextPosition(pos);
            }
        } …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm traversal graph depth-first-search

40
推荐指数
1
解决办法
4万
查看次数

BFS和DFS运行时的说明

为什么BFS和DFS O(V + E)的运行时间,特别是当有一个节点具有可以从顶点到达的节点的有向边时,就像在以下站点中的此示例中一样

http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/depthSearch.htm

graph breadth-first-search time-complexity depth-first-search data-structures

36
推荐指数
4
解决办法
8万
查看次数

使用DFS在图表中检测周期:2种不同的方法,区别在于什么

请注意,图表表示为邻接列表.

我听说有两种方法可以在图表中找到一个循环:

  1. 保留一个布尔值数组,以跟踪您之前是否访问过某个节点.如果你的新节点用完了(没有点击你已经存在的节点),那么只需回溯并尝试不同的分支.

  2. 来自Cormen的CLRS或Skiena的那个:对于无向图中的深度优先搜索,有两种类型的边,树和背.当且仅当存在后沿时,该图具有循环.

有人可以解释一下图的后边缘是什么,以及上述两种方法之间的差异是什么.

谢谢.

更新: 这是在两种情况下检测周期的代码.Graph是一个简单的类,为了简单起见,将所有图形节点表示为唯一编号,每个节点都有其相邻的相邻节点(g.getAdjacentNodes(int)):

public class Graph {

  private int[][] nodes; // all nodes; e.g. int[][] nodes = {{1,2,3}, {3,2,1,5,6}...};

  public int[] getAdjacentNodes(int v) {
    return nodes[v];
  }

  // number of vertices in a graph
  public int vSize() {
    return nodes.length;
  }

}
Run Code Online (Sandbox Code Playgroud)

用于检测无向图中的循环的Java代码:

    public class DFSCycle {

    private boolean marked[];
    private int s;
    private Graph g;
    private boolean hasCycle;

    // s - starting node
    public DFSCycle(Graph g, int s) {
        this.g = g;
        this.s = …
Run Code Online (Sandbox Code Playgroud)

graph cycle adjacency-list depth-first-search

36
推荐指数
2
解决办法
7万
查看次数

迭代深化与深度优先搜索

我一直在阅读有关迭代深化的内容,但我不明白它与深度优先搜索的区别.

我知道深度优先搜索越来越深入.

在迭代深化中,您建立一个级别的值,如果该级别没有解决方案,则递增该值,然后从头开始(根).

这不会与深度优先搜索相同吗?

我的意思是你会继续增加和增加,直到找到解决方案.我认为这是同样的事情!我会走同一个分支,因为如果我从头开始,我会像以前一样走下同一个分支.

algorithm search artificial-intelligence depth-first-search iterative-deepening

35
推荐指数
1
解决办法
4万
查看次数

如何使用非递归的方法实现深度优先搜索图形

好吧,我花了很多时间在这个问题上.但是,我只能找到树的递归方法的解决方案:树的非递归,或图的递归方法,图的递归.

许多教程(我不在这里提供这些链接)也没有提供方法.或者教程完全不正确.请帮我.

更新:

这很难描述:

如果我有一个无向图:

               1
             / |  \
            4  |   2
               3 /
Run Code Online (Sandbox Code Playgroud)

1-- 2-- 3 - 1是一个循环.

在这一步: push the neighbors of the popped vertex into the stack

WHAT'S THE ORDER OF THE VERTEXES SHOULD BE PUSHED?

如果推送的顺序是2 4 3,则堆栈中的顶点是:

| |
|3|
|4|
|2|    
 _
Run Code Online (Sandbox Code Playgroud)

弹出节点后,我们得到了结果:1 - > 3 - > 4 - > 2而不是1 - > 3 - > 2 - > 4.

这是不正确的.我应该添加什么条件来阻止这个场景?

algorithm graph depth-first-search non-recursive

31
推荐指数
4
解决办法
7万
查看次数

A*是最好的寻路算法吗?

通常认为A*是解决寻路问题的最佳算法.

当A*不是找到解决方案的最佳算法时,是否存在任何情况?

与BFS,DFS,UCS等相比,A*有多好?

a-star breadth-first-search path-finding depth-first-search

28
推荐指数
3
解决办法
3万
查看次数

我怎么能记住DFS和BFS使用哪些数据结构?

无论是使用堆栈还是DFS或BFS的队列,我总是混淆.有人可以提供一些关于如何记住哪种算法使用哪种数据结构的直觉?

queue stack breadth-first-search depth-first-search

26
推荐指数
7
解决办法
6万
查看次数