Defined:
The breadth-first algorithm (Breadth-first-search), or BFS, is a graph search arithmetic method. To put it simply,BFS is the node that traverses the tree from the root node, along the width of the tree, and if the target is found, the calculation terminates .
Algorithm Analysis:
BFS is a blind search method that is designed to systematically unfold and examine all the nodes in the diagram to find results. In other words, it does not take into account the possible location of the results and thoroughly searches the entire graph until the results are found.
Time complexity: The adjacency table is O (| v| + | e|). Among them | v| is the number of nodes, | e| is the number of edges in the graph.
--The above content mainly from the degree Niang
Applicable conditions:
For all sides of the same length, such as the map model, the first time BFS encountered the target point, it must be from the root node to the shortest path to the target node (because each time a bit is outward expansion of a step, you first encountered, then you must be the shortest). BFS first found must be the shortest . However, if it is a weighted edge, then there will be a problem, BFS is returned by the minimum number of edges of the solution, but because of the weighted, the solution to the root node is not necessarily the shortest distance. For example, 1000+1000 is only two paragraphs, 1+1+1+1 has 4 segments, but the BFS returned by the least number of edges of the solution, where the total length of 2000, obviously not the shortest path. At this point we should use the Dijkstra shortest path algorithm to solve the shortest route of weighted paths.
Application:
1. Find the shortest path of any two points in the non-weighted graph (or all sides with the same weights) .
2. Look for all nodes in one of the connected branches. (diffuse)
3, BFs staining method to determine whether it is a two-minute chart.
Some thoughts:
1. Why use a queue to save a point instead of a stack?
Let us cite an example where we currently have a starting node in our queue. After the first round of expansion, there are four points on the top, right, bottom, and left of the starting point in the queue, and now we need to take these four points for a round of expansion. That is, the node in the next queue should be the "top" of the four direction, "right" of the four direction, "down" of the four direction, "left" of the point of four directions (the book is of course not added). If we use a stack, on the ' up ' point, we blow it off, and then the stack's nature throws its four-direction point to the head of the queue. It is supposed that after we take the "up" point extension, we should take the remaining right, bottom, and left points to continue to complete the expansion of this queue, but stack put the "top" node four extension points to the right, down, left three nodes in front of the (according to our BFS every time to take the first point of the team) so the next one is the "up" node of the four extension points, which is not right. Instead, the queue throws the extension point to the back each time, and there is no effect on the next pick-up of the team's first point of expansion.
2, BFS (our default general BFS with book) do not have to worry about the queue has been a bit, empty, because all the points slowly expand will run to the outside of the map, in the boundary conditions there will be brushed off. So in addition to the kind of non-book need to consider the point in the queue to repeat the cycle of death, other times there is no need to worry about this issue.
3, BFS can record the path it.
Of course, we can make a note of the parent node of each point we go to, and then go back to it.
Memory:
1, as long as a see to find the shortest path: BFS or the shortest way. reserves the right to use the BFS, the shortest circuit.
2, if the second time to go back to the point where there is a state parameter changes, then can not book.