Bidirectional Search
This page briefly introduces two bidirectional search algorithms: "Bidirectional Simultaneous Search" and "Meet in the Middle".
Bidirectional Simultaneous Search
Definition
The basic idea of bidirectional simultaneous search is to perform BFS or DFS simultaneously from both the start and end points in the state graph.
If the two search frontiers meet, a feasible solution is considered to have been found.
Process
Steps for bidirectional BFS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Example Problem
Example Eight Digits Puzzle
On a \(3\times 3\) board, there are eight pieces, each marked with a number from \(1\) to \(8\). There is one empty space on the board, represented by \(0\). Pieces adjacent to the empty space can be moved into it. The problem is: given an initial layout (initial state) and a target layout (for simplicity, let the target state be \(123804765\)), find a method to move from the initial layout to the target layout with the minimum number of steps.
Solution Idea
It is easy to think of a brute-force BFS. Using brute-force BFS for this problem will not time out. However, it is used here as an example of bidirectional simultaneous search. We can use two BFS: one starting from the initial state and searching forward, and the other starting from the target state and searching backward. Alternating between the two BFS significantly reduces the size of the search tree. When one BFS finds a state already discovered by the other BFS, the answer is obtained.
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
Meet in the Middle
Warning
This section does not introduce Binary Search (another translation for binary search is "half-interval search").
Introduction
The Meet in the Middle algorithm has no formal translation; common translations include "half-interval search", "bidirectional search", or "meet in the middle".
It is suitable when the input data is relatively small but not small enough to allow direct brute-force search.
Process
The main idea of the Meet in the Middle algorithm is to split the entire search process into two halves, search each half separately, and finally merge the results from both halves.
Properties
The complexity of brute-force search is often exponential. By using the meet in the middle algorithm, the exponent can be halved, reducing the complexity from \(O(a^b)\) to \(O(a^{b/2})\).
Example Problem
Example 「USACO09NOV」Lights
There are \(n\) lights, each connected to several other lights. Each light has a switch. Pressing the switch on a light will toggle the state of that light and all lights connected to it. Initially, all lights are off. You need to turn on all lights. Find the minimum number of switch presses required.
\(1\le n\le 35\).
Solution Idea
If we use brute-force DFS to search for the switch states, the time complexity would be \(O(2^{n})\), which is clearly too slow. However, if we use meet in the middle, the time complexity can be optimized to \(O(n2^{n/2})\). Meet in the middle involves first finding the states achievable using only the switches numbered from \(1\) to \(\mathrm{mid}\), and then finding the states achievable using only the other half of the switches. If the lights turned on in the first half and the second half are complementary, merging these two halves gives a solution to turn on all lights. In implementation, we can store the states from the first half and the minimum number of switch presses to reach each state in a map. When searching the second half, for each state found, we merge it with the complementary state from the first half to update the answer.
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
External Links
- What is meet in the middle algorithm w.r.t. competitive programming? - Quora
- Meet in the Middle Algorithm - YouTube
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用