BFS and deformation

[BFS]

Queue means to achieve a breadth-first search, first, the queue contains only the starting state. In the process of breadth-first search, we continue to remove the head of state from the team, for all the branches of the state facing the next state along each branch of arrival (If you have not visited or solution can be updated to better) insert team tail. The above process is repeatedly executed until the queue is empty.

Queue corresponding to the case where:

1.Flood Fill

Flood Fill algorithm filling algorithm is a computer graphics and digital image processing. Just like at the start of pouring, to see how much of an area to cover. In fact, from the point to start looking around four points filling traversal, principles and BFS is very similar, of course, be the same as the DFS traversal.

2. Minimum number of steps

Typical such as "walking map" type of problem, that is shaped like "map given a rectangle, a control object request to move the map by seeking a minimum number of steps" problem.

BFS is very good at solving this problem. The overall shape of the map is fixed, changing only a few individuals or characteristics occur with each step. We just need to extract these changes as part of the state, the initial state to join the queue, using the breadth-first search teams continue to remove the head of state, along the branch expansion into the team can be.

Breadth-first search layer by layer traversal of the search tree algorithm, all states have levels monotonic (that is, the number of steps monotonic) in accordance with the order into the team. If every step corresponds exactly to the extension, then when a state is first accessed (enqueued), to give a minimum number of steps to reach this state from the initial state.

3. The state diagram search

BFS search process can only be a number of objects can also be a state shown in FIG.

Such as the typical eight digital, then we must consider:

  1. State how representation (storage)?
  2. Sentenced to state how heavy?
  3. How transfer between states?

[Distortion] wide search

1. deque BFS

The above analysis, in the most basic of breadth-first search, extended along each branch are referred to as "step", we search through layer by layer, to solve the problem of the number of request from the initial state to each state a minimum step. In fact, this is equivalent to a piece of the edge weights are 1 to perform a breadth-first traversal of the graph, find the shortest distance to each point relative to the starting point (level). The number of layers in the queue state satisfies monotonicity and two , so that we can know, in each state when the team is incorporated is first accessed , the number of steps calculated is also desired.

If, however, on the right side of Figure 1 is not all of it?

Let us first discuss the diagram on the right side of either a 1 or a 0.

In the edge weights are either 0 or 1 is not on the map, we can deque wide search is calculated. Overall framework broad general algorithm similar search, only slightly changed direction in the branch extension on each node. If this branch is the edge weights 0 side, put the new node along the branches reach into the team from the team head ; if this branch is the edge weights of 1 side, as a general wide search as from the end of the team team . As a result, we can still guarantee from the value of the node corresponding queues at any time have a broad search, "two" and "monotonic", each node is first accessed (the team) when, on can be obtained from the upper left to the shortest distance from the node.

2. priority queue BFS

In the case of having more universal, i.e., each has a different spreading the cost when obtaining the initial state to the minimum cost of each state, the equivalent calculated on a weighted graph from the beginning to the shortest path for each node. At this point, we have two solutions:

Method One: still using a general wide search, a generic queue.

  At this point we can no longer guarantee the first time each state will be able to get into the team when the minimum cost, we can only allow a state to be updated several times, many times out of the queue. We continue to execute the search until the queue is empty.

  Wide search algorithm to search the entire tree traversal and update repeated until the "convergence" to the optimal solution, in fact, is "iteration" thinking. The worst case, the time complexity of the algorithm will search from the broad general THE ( N ) O (N) growth to THE ( N 2 ) O (N ^ 2) . In the shortest path problem in correspondence isSPFA algorithm.

Method two: Switch priority queue extensive search.

  Here the "priority queue" the equivalent of a binary heap. We can from each queue remove the current minimum cost expanded state (this state must already be optimal because the cost of the current queue is not less than its other state, so it is impossible to update after it), along the pieces of the new branch added to the priority queue state has been reached. Continue to perform the search, until the queue is empty.

  In BFS priority queue in each state will be updated several times, queue times out, a status may exist in different price in the queue. However, when each state when it is first removed from the queue , to obtain the minimum cost from the start state to state. After being taken out if more, it can simply be ignored, does not extend. So, BFS priority queue in each state only once extended, the time complexity of maintaining more than just the price of a binary heap. If complexity is generally wide search THE ( N ) O (N) , the priority queue is the complexity of BFS THE ( N l The g N ) O (N lie) . In the corresponding shortest path problem in thatpile of Djjkstra optimization algorithm.

3. Bidirectional BFS

Because the BFS algorithm itself is a search layer by layer, so that two-way BFS more natural and simple.

BFS ordinary bidirectional minimum required number of steps, for example, we only need the initial state, the target state starts, respectively, on both sides alternately, each said respective spread a whole layer. When both sides have each
repeating a state occurs in an array of records, the search process will be described both met, it can be combined to obtain a minimum number of steps starting end.

【example】

  • AcWing 844. Maze (entry): Click here

  • AcWing 1097. pond count (Flood Fill): Click here

  • AcWing 1098. Castle problem (Flood Fill): Click here

  • AcWing 1106. peaks and valleys (Flood Fill): Click here

  • AcWing 173. distance matrix (multi-source BFS): Click here

  • AcWing 1076. maze (the minimum number of steps, the output path): Click here

  • AcWing 188. warrior demeanor of cattle (the minimum number of steps): Click here

  • POJ 3278 Catch That Cow (minimum number of steps): Click here

  • AcWing 845. eight digital (state graph search, BFS): Click here

  • AcWing 1107. Magic plate (FIG search state, the output of the lexicographically smallest path): Click here

  • AcWing 175. Service circuits (deque BFS): Click here

Published 844 original articles · won praise 135 · Views 150,000 +

Guess you like

Origin blog.csdn.net/qq_42815188/article/details/105094648