KNOW: BFS & DFS

BFS

http://en.wikipedia.org/wiki/Breadth-first_search

Definition

In graph theory, breadth-first search (BFS) is astrategy for searching in a graph when search is limited to essentially two operations: (a) visit and inspect a node of a graph; (b) gain access to visit the nodes that neighbor the currently visited node. The BFS begins at a root node and inspects all the neighboring nodes. Then for each of those neighbor nodes in turn, it inspects their neighbor nodes which were unvisited, and so on.

Algorithm

  1. Enqueue the root node
  2. Dequeue a node and examine it
    • If the element sought is found in this node, quit the search and return a result.
    • Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered.
  3. If the queue is empty, every node on the graph has been examined – quit the search and return "not found".
  4. If the queue is not empty, repeat from Step 2.
Note: Using a stack instead of a queue would turn this algorithm into a depth-first search.

Code

void BFS(Node root)
{
    Queue queue = new Queue();
    root.visited = true;
    visit(root);
    queue.enqueue(root);      
    
    while(!queue.IsEmpty())
    {
        Node tmp = queue.dequeue();
        for(Node n in r.adjacent)
        {
            if(n.visited == false)
            {
                visit(n);
                n.visited = true;
                queue.enqueue(n);
            }
        }
    }
}

Application

  • Finding all nodes within one connected component
  • Finding the shortest path between two nodes u and v (with path length measured by number of edges)


DFS

http://en.wikipedia.org/wiki/Depth-first_search

Definition

Depth-first search (DFS) is an algorithm for traversing or searchingtree orgraph data structures. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

Code

void DFS(Node root)
{
    if (root == null) return;
    visit(root);
    root.visited = true;
    for(Node n in root.adjacent)
    {
        if(n.visited == false)
            DFS(n);
    }
}

Application

  • Finding connected components.
  • Topological sorting.

Difference Between BFS & DFS

http://stackoverflow.com/questions/2626198/graphs-data-structure-dfs-vs-bfs

http://www.programmerinterview.com/index.php/data-structures/dfs-vs-bfs/

Comparing BFS and DFS, the big advantage of DFS is that it has much lower memory requirements than BFS, because it’s not necessary to store all of the child pointers at each level. Depending on the data and what you are looking for, either DFS or BFS could be advantageous.

For example, given a family tree if one were looking for someone on the tree who’s still alive, then it would be safe to assume that person would be on the bottom of the tree. This means that a BFS would take a very long time to reach that last level. A DFS, however, would find the goal faster. But, if one were looking for a family member who died a very long time ago, then that person would be closer to the top of the tree. Then, a BFS would usually be faster than a DFS. So, the advantages of either vary depending on the data and what you’re looking for.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值