<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
# Breadth First Search - 廣度優先搜索
--------
#### 問題
在$$ n $$行$$ m $$列的的二維方格圖$$ s = n \times m $$中從$$ beg $$點移動到$$ end $$點。
#### 解法
廣度優先搜索是優先搜索二維方格圖$$ s $$中每個節點的相鄰節點,與之相對的深度優先搜索則會沿著節點的一個相鄰節點試圖走到最遠。
例如在下面這個$$ 5 \times 4 $$二維方格$$ s $$中從$$ beg = [1,0] $$移動到$$ end = [4,3] $$。初始時將起點$$ beg $$加入待搜索節點的隊列$$ queue $$中,之后每次從$$ queue $$中取出頭節點$$ e $$,訪問$$ e $$四周從未被訪問的鄰節點,并將鄰節點加入$$ queue $$中。將每個節點加入$$ queue $$之前將其染為紅色,避免重復訪問。

$$ (1) $$初始時將$$ beg = [1,0] $$染紅并加入$$ queue $$;
$$ (2) $$從$$ queue $$中取出頭節點$$ [1,0] $$,因$$ [1,0] \ne end $$,將其四周未被染紅的節點$$ [0,0], [1,1], [2,0] $$染紅并加入$$ queue $$,圖中$$ queue $$的左邊為頭部,右邊為尾部,新訪問的節點插入隊列尾部,每次從隊列中取出頭節點$$ e $$:

$$ (3) $$從$$ queue $$中取出頭節點$$ [0,0] $$,因$$ [0,0] \ne end $$,將其四周未被染紅的節點$$ [0,1] $$染紅并加入$$ queue $$;


$$
\cdots
$$

$$ (4) $$從$$ queue $$中取出頭節點$$ [1,3] $$,因$$ [1,3] \ne end $$,其四周的節點都已經被染紅,因此不加入任何新節點;

$$
\cdots
$$


$$ (5) $$從$$ queue $$中取出頭節點$$ [4,3] $$,因$$ [4,3] = end $$,算法結束;
本章的圖搜索中,一個節點通常只需要搜索一次,常用染色來標記一個節點是否被搜索過,染紅后就不會再放入待搜索隊列$$ queue $$中。用類似“父節點”指針來記錄搜索時遇到節點的上一個節點,搜索完成時通過“父節點”逆向的找到一條從$$ end $$回到$$ beg $$的路徑。
該算法的時間復雜度為$$ O(n \times m) $$。
--------
#### 源碼
[BreadthFirstSearch.h](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Search/BreadthFirstSearch.h)
[BreadthFirstSearch.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Search/BreadthFirstSearch.cpp)
#### 測試
[BreadthFirstSearchTest.cpp](https://github.com/linrongbin16/Way-to-Algorithm/blob/master/src/Search/BreadthFirstSearchTest.cpp)
- Content 目錄
- Preface 前言
- Chapter-1 Sort 第1章 排序
- InsertSort 插入排序
- BubbleSort 冒泡排序
- QuickSort 快速排序
- MergeSort 歸并排序
- Chapter-2 Search 第2章 搜索
- BinarySearch 二分查找法(折半查找法)
- BruteForce 暴力枚舉
- Recursion 遞歸
- BreadthFirstSearch 廣度優先搜索
- BidirectionalBreadthSearch 雙向廣度搜索
- AStarSearch A*搜索
- DancingLink 舞蹈鏈
- Chapter-3 DataStructure 第3章 數據結構
- DisjointSet 并查集
- PrefixTree(TrieTree) 前綴樹
- LeftistTree(LeftistHeap) 左偏樹(左偏堆)
- SegmentTree 線段樹
- FenwickTree(BinaryIndexedTree) 樹狀數組
- BinarySearchTree 二叉查找樹
- AVLTree AVL平衡樹
- RedBlackTree 紅黑樹
- Chapter-4 DynamicProgramming 第4章 動態規劃
- Chapter-5 GraphTheory 第5章 圖論
- Chapter-6 Calculation 第6章 計算
- LargeNumber 大數字
- Exponentiation 求冪運算
- Chapter-7 CombinatorialMathematics 第7章 組合數學
- FullPermutation 全排列
- UniqueFullPermutation 唯一的全排列
- Combination 組合
- DuplicableCombination (元素)可重復的組合
- Subset 子集
- UniqueSubset 唯一的子集
- Permutation 排列
- PermutationGroup 置換群
- Catalan 卡特蘭數
- Chapter-8 NumberTheory 第8章 數論
- Sieve 篩選算法
- Euclid 歐幾里得
- EuclidExtension 歐幾里得擴展
- ModularLinearEquation 模線性方程
- ChineseRemainerTheorem 中國剩余定理
- ModularExponentiation 模冪運算
- Chapter-9 LinearAlgebra 第9章 線性代數
- Chapter-10 AnalyticGeometry 第10章 解析幾何
- Chapter-11 TextMatch 第11章 文本匹配
- SimpleMatch 簡單匹配
- AhoCorasickAutomata AC自動機
- KnuthMorrisPratt KMP匹配算法
- RabinKarp RabinKarp算法
- BoyerMoore BoyerMoore算法
- Chapter-12 GameTheory 第12章 博弈論
- BashGame 巴什博弈
- WythoffGame 威佐夫博弈
- NimGame 尼姆博弈