KNOW: BFS & DFS

来源:互联网 发布:姚明nba数据统计 编辑:程序博客网 时间:2024/05/23 14:37

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.


原创粉丝点击