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
- Enqueue the root node
- 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.
- If the queue is empty, every node on the graph has been examined – quit the search and return "not found".
- If the queue is not empty, repeat from Step 2.
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.
- KNOW: BFS & DFS
- BFS-DFS
- BFS DFS
- DFS+BFS
- BFS DFS
- DFS BFS
- DFS & BFS
- BFS,DFS
- dfs bfs
- BFS&DFS
- DFS BFS
- BFS && DFS
- BFS DFS
- DFS & BFS
- DFS&BFS
- BFS&DFS
- dfs && bfs
- dfs bfs
- 总结一下ERP .NET程序员必须掌握的.NET技术,掌握了这些技术工作起来才得心应手
- 设计模式原则
- 面向对象设计的设计原则
- PLM与ERP整合逐渐成重要趋势
- PLM潜力解密 传统ERP厂商主导PLM PLM潜力解密 传统ERP厂商主导PLM
- KNOW: BFS & DFS
- git使用流程
- UITableView 基本使用方法总结
- Edit Distance
- 图像的各向异性扩散——启发性方法
- matlab双边滤波
- VPC 装 windows server2003 并配置WRK-v1.2出现的错误记录
- 【iphone应用开发】iphone应用开发之六:UITableView的详细讲解(一)
- 2013年9月5日、|思维方法