BFS与DFS简介与LC实例
作者:Bluemapleman(tomqianmaple@outlook.com)
麻烦不吝star和fork本博文对应的github上的技术博客项目吧!谢谢大家的支持!
知识无价,写作辛苦,欢迎转载,但请注明出处,谢谢!
文章目录
前言:BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或搜索算法,在树与图相关算法的考察中是非常常见的两种解题思路。
文章目录
Definition of DFS and BFS
DFS的wikipedia定义:
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.
BFS的wikipedia定义:
Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a ‘search key’[1]), and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.
It uses the opposite strategy as depth-first search, which instead explores the highest-depth nodes first before being forced to backtrack and expand shallower nodes.
So obviously, as their name suggest, DFS focuses on ‘depth’ when searching or traversing while BFS focuses on ‘breath’.
By the way, because of DFS’s feature, it’s easy to relate it with ‘Backtracking’ algorithm as the wiki definition mentions. The relationship between DFS and backtracking is well explained by Reed Copsey on StackOverflow:
Backtracking is a more general purpose algorithm.
Depth-First search is a specific form of backtracking related to searching tree structures. From Wikipedia:
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.
It uses backtracking as part of its means of working with a tree, but is limited to a tree structure.
Backtracking, though, can be used on any type of structure where portions of the domain can be eliminated - whether or not it is a logical tree. The Wiki example uses a chessboard and a specific problem - you can look at a specific move, and eliminate it, then backtrack to the next possible move, eliminate it, etc.
How to Implement DFS and BFS
DFS
In tree structure, DFS means we always start from a root node and try to reach the leaf node as direct as possible before we have to backtrack.
Order in which the nodes are visited
In graph, DFS means we start from a random assigned node in the graph, and explores as far as possible along the branch before we have to backtrack.
So the key points for DFS are:
- How to explore as far as possible?
- How to backtrack?
How to explore as far as possible
Normally, for tree node, it would have left child or right child, so we would continuously go on exploring current node’s child node until we encounter a null node, then we go back to last node. Repeat above procedures until all nodes have been visited.
for graph node, we do the similar exploration: explore as further as possible according to the representation of graph (adjacency list, adjacency matrix or incidence matrix) until we find no more node that hasn’t been visited and connected with current node, then we go back to last node. Repeat above procedures until all nodes have been visited.
How to backtrack/go back?
‘Go back’ generally can be realized using data structure ——stack—— or by recursion. And if we use stack, it means we would need to push each node we visited in the process of exploring each branch, and pop when we can’t explore fu