广度优先搜索算法 http://blog.csdn.net/ywjun0919/article/details/8838491
2016-03-01 11:09
555 查看
广度优先搜索算法
2013-04-23 10:30 4875人阅读 评论(0) 收藏 举报分类:
算法(19)
目录(?)[+]
广度优先搜索算法(Breadth-First-Search),又译作宽度优先搜索,或横向优先搜索,简称BFS,是一种图形搜索算法。简单的说,BFS是从根节点开始,沿着树的宽度遍历树的节点。如果所有节点均被访问,则算法中止。广度优先搜索的实现一般采用open-closed表。
[隐藏] |
[编辑]作法
BFS是一种盲目搜寻法,目的是系统地展开并检查图中的所有节点,以找寻结果。换句话说,它并不考虑结果的可能位址,彻底地搜索整张图,直到找到结果为止。BFS并不使用经验法则算法。从算法的观点,所有因为展开节点而得到的子节点都会被加进一个先进先出的伫列中。一般的实作里,其邻居节点尚未被检验过的节点会被放置在一个被称为 open 的容器中(例如伫列或是链表),而被检验过的节点则被放置在被称为 closed 的容器中。(open-closed表)
以德国城市为范例的地图。城市间有数条道路相连接。 | 从法兰克福开始执行广度优先搜索算法,所产生的广度优先搜索算法树。 | 广度优先搜索算法的动画范例 |
实作方法
首先将根节点放入伫列中。从伫列中取出第一个节点,并检验它是否为目标。
如果找到目标,则结束搜寻并回传结果。
否则将它所有尚未检验过的直接子节点加入伫列中。
若伫列为空,表示整张图都检查过了——亦即图中没有欲搜寻的目标。结束搜寻并回传“找不到目标”。
重复步骤2。
s为初始点 while 从Q中选一点 /* 选最先插入进Q的点,则为广度遍历,可以说先进先出。*/ /* 选最后插入进Q的点,则为深度遍历,可以说后进先出。 */ if then /* N(v):v的邻接点 */ else return H=(R,T)
C 的实作
广度优先搜索算法:<span class="kw4" style="color: rgb(153, 51, 51);">void</span> BFS<span class="br0" style="color: rgb(0, 153, 0);">(</span>VLink G<span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="br0" style="color: rgb(0, 153, 0);">]</span><span class="sy0" style="color: rgb(51, 153, 51);">,</span> <span class="kw4" style="color: rgb(153, 51, 51);">int</span> v<span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span> <span class="kw4" style="color: rgb(153, 51, 51);">int</span> w<span class="sy0" style="color: rgb(51, 153, 51);">;</span> VISIT<span class="br0" style="color: rgb(0, 153, 0);">(</span>v<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="coMULTI" style="color: rgb(128, 128, 128); font-style: italic;">/*訪問頂點v*/</span> visited<span class="br0" style="color: rgb(0, 153, 0);">[</span>v<span class="br0" style="color: rgb(0, 153, 0);">]</span> <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="nu0" style="color: rgb(0, 0, 221);">1</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="coMULTI" style="color: rgb(128, 128, 128); font-style: italic;">/*頂點v對應的訪問標記置為1*/</span> ADDQ<span class="br0" style="color: rgb(0, 153, 0);">(</span>Q<span class="sy0" style="color: rgb(51, 153, 51);">,</span>v<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="kw1" style="color: rgb(177, 177, 0);">while</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="sy0" style="color: rgb(51, 153, 51);">!</span>EMPTYQ<span class="br0" style="color: rgb(0, 153, 0);">(</span>Q<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span> v <span class="sy0" style="color: rgb(51, 153, 51);">=</span> DELQ<span class="br0" style="color: rgb(0, 153, 0);">(</span>Q<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="coMULTI" style="color: rgb(128, 128, 128); font-style: italic;">/*退出隊頭元素v*/</span> w <span class="sy0" style="color: rgb(51, 153, 51);">=</span> FIRSTADJ<span class="br0" style="color: rgb(0, 153, 0);">(</span>G<span class="sy0" style="color: rgb(51, 153, 51);">,</span>v<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="coMULTI" style="color: rgb(128, 128, 128); font-style: italic;">/*求v的第1個鄰接點。無鄰接點則返回-1*/</span> <span class="kw1" style="color: rgb(177, 177, 0);">while</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>w <span class="sy0" style="color: rgb(51, 153, 51);">!=</span> <span class="sy0" style="color: rgb(51, 153, 51);">-</span><span class="nu0" style="color: rgb(0, 0, 221);">1</span><span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span> <span class="kw1" style="color: rgb(177, 177, 0);">if</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>visited<span class="br0" style="color: rgb(0, 153, 0);">[</span>w<span class="br0" style="color: rgb(0, 153, 0);">]</span> <span class="sy0" style="color: rgb(51, 153, 51);">==</span> <span class="nu0" style="color: rgb(0, 0, 221);">0</span><span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span> VISIT<span class="br0" style="color: rgb(0, 153, 0);">(</span>w<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="coMULTI" style="color: rgb(128, 128, 128); font-style: italic;">/*訪問頂點v*/</span> ADDQ<span class="br0" style="color: rgb(0, 153, 0);">(</span>Q<span class="sy0" style="color: rgb(51, 153, 51);">,</span>w<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="coMULTI" style="color: rgb(128, 128, 128); font-style: italic;">/*當前被訪問的頂點w進隊*/</span> visited<span class="br0" style="color: rgb(0, 153, 0);">[</span>w<span class="br0" style="color: rgb(0, 153, 0);">]</span> <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="nu0" style="color: rgb(0, 0, 221);">1</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="coMULTI" style="color: rgb(128, 128, 128); font-style: italic;">/*頂點w對應的訪問標記置為1*/</span> <span class="br0" style="color: rgb(0, 153, 0);">}</span> w <span class="sy0" style="color: rgb(51, 153, 51);">=</span> NEXTADJ<span class="br0" style="color: rgb(0, 153, 0);">(</span>G<span class="sy0" style="color: rgb(51, 153, 51);">,</span>v<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="coMULTI" style="color: rgb(128, 128, 128); font-style: italic;">/*求v的下一個鄰接點。若無鄰接點則返回-1*/</span> <span class="br0" style="color: rgb(0, 153, 0);">}</span> <span class="br0" style="color: rgb(0, 153, 0);">}</span> <span class="br0" style="color: rgb(0, 153, 0);">}</span>
对图G=(V,E)进行广度优先搜索的主算法如下。
<span class="kw4" style="color: rgb(153, 51, 51);">void</span> TRAVEL_BFS<span class="br0" style="color: rgb(0, 153, 0);">(</span>VLink G<span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="br0" style="color: rgb(0, 153, 0);">]</span><span class="sy0" style="color: rgb(51, 153, 51);">,</span> <span class="kw4" style="color: rgb(153, 51, 51);">int</span> visited<span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="br0" style="color: rgb(0, 153, 0);">]</span><span class="sy0" style="color: rgb(51, 153, 51);">,</span> <span class="kw4" style="color: rgb(153, 51, 51);">int</span> n<span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span> <span class="kw4" style="color: rgb(153, 51, 51);">int</span> i<span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="kw1" style="color: rgb(177, 177, 0);">for</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>i <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="nu0" style="color: rgb(0, 0, 221);">0</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> i <span class="sy0" style="color: rgb(51, 153, 51);"><</span> n<span class="sy0" style="color: rgb(51, 153, 51);">;</span> i <span class="sy0" style="color: rgb(51, 153, 51);">++</span><span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span> visited<span class="br0" style="color: rgb(0, 153, 0);">[</span>i<span class="br0" style="color: rgb(0, 153, 0);">]</span> <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="nu0" style="color: rgb(0, 0, 221);">0</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="coMULTI" style="color: rgb(128, 128, 128); font-style: italic;">/* 標記數組賦初值(清零)*/</span> <span class="br0" style="color: rgb(0, 153, 0);">}</span> <span class="kw1" style="color: rgb(177, 177, 0);">for</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>i <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="nu0" style="color: rgb(0, 0, 221);">0</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> i <span class="sy0" style="color: rgb(51, 153, 51);"><</span> n<span class="sy0" style="color: rgb(51, 153, 51);">;</span> i <span class="sy0" style="color: rgb(51, 153, 51);">++</span><span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="kw1" style="color: rgb(177, 177, 0);">if</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>visited<span class="br0" style="color: rgb(0, 153, 0);">[</span>i<span class="br0" style="color: rgb(0, 153, 0);">]</span> <span class="sy0" style="color: rgb(51, 153, 51);">==</span> <span class="nu0" style="color: rgb(0, 0, 221);">0</span><span class="br0" style="color: rgb(0, 153, 0);">)</span> BFS<span class="br0" style="color: rgb(0, 153, 0);">(</span>G<span class="sy0" style="color: rgb(51, 153, 51);">,</span>i<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="br0" style="color: rgb(0, 153, 0);">}</span>
C++ 的实作
定义一个结构体来表达一个节点的结构:<span class="kw4" style="color: rgb(153, 51, 51);">struct</span> node <span class="br0" style="color: rgb(0, 153, 0);">{</span> <span class="kw4" style="color: rgb(153, 51, 51);">int</span> self<span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="co1" style="color: rgb(102, 102, 102); font-style: italic;">//数据</span> node <span class="sy0" style="color: rgb(51, 153, 51);">*</span>left<span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="co1" style="color: rgb(102, 102, 102); font-style: italic;">//左节点</span> node <span class="sy0" style="color: rgb(51, 153, 51);">*</span>right<span class="sy0" style="color: rgb(51, 153, 51);">;</span> <span class="co1" style="color: rgb(102, 102, 102); font-style: italic;">//右节点</span> <span class="br0" style="color: rgb(0, 153, 0);">}</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
那么,我们在搜索一个树的时候,从一个节点开始,能首先获取的是它的两个子节点。例如:
A B C
A是第一个访问的,然后顺序是B和C;然后再是B的子节点,C的子节点。那么我们怎么来保证这个顺序呢?
这里就应该用queue数据结构,因为queue采用先进先出( first-in-first-out )的顺序。
使用C++的STL函式库,下面的程序能帮助理解:
std<span class="sy4" style="color: rgb(0, 128, 128);">::</span><span class="me2" style="color: rgb(0, 119, 136);">queue</span><span class="sy1" style="color: rgb(0, 0, 128);"><</span>node<span class="sy2" style="color: rgb(0, 0, 64);">*</span><span class="sy1" style="color: rgb(0, 0, 128);">></span> visited, unvisited<span class="sy4" style="color: rgb(0, 128, 128);">;</span> node nodes<span class="br0" style="color: rgb(0, 128, 0);">[</span><span class="nu0" style="color: rgb(0, 0, 221);">9</span><span class="br0" style="color: rgb(0, 128, 0);">]</span><span class="sy4" style="color: rgb(0, 128, 128);">;</span> node<span class="sy2" style="color: rgb(0, 0, 64);">*</span> current<span class="sy4" style="color: rgb(0, 128, 128);">;</span> unvisited.<span class="me1" style="color: rgb(0, 119, 136);">push</span><span class="br0" style="color: rgb(0, 128, 0);">(</span><span class="sy3" style="color: rgb(0, 0, 64);">&</span>nodes<span class="br0" style="color: rgb(0, 128, 0);">[</span><span class="nu0" style="color: rgb(0, 0, 221);">0</span><span class="br0" style="color: rgb(0, 128, 0);">]</span><span class="br0" style="color: rgb(0, 128, 0);">)</span><span class="sy4" style="color: rgb(0, 128, 128);">;</span> <span class="co1" style="color: rgb(102, 102, 102);">//先把root放入unvisited queue</span> <span class="kw1" style="color: rgb(0, 0, 255);">while</span><span class="br0" style="color: rgb(0, 128, 0);">(</span><span class="sy3" style="color: rgb(0, 0, 64);">!</span>unvisited.<span class="me1" style="color: rgb(0, 119, 136);">empty</span><span class="br0" style="color: rgb(0, 128, 0);">(</span><span class="br0" style="color: rgb(0, 128, 0);">)</span><span class="br0" style="color: rgb(0, 128, 0);">)</span> <span class="co1" style="color: rgb(102, 102, 102);">//只有unvisited不空</span> <span class="br0" style="color: rgb(0, 128, 0);">{</span> current <span class="sy1" style="color: rgb(0, 0, 128);">=</span> <span class="br0" style="color: rgb(0, 128, 0);">(</span>unvisited.<span class="me1" style="color: rgb(0, 119, 136);">front</span><span class="br0" style="color: rgb(0, 128, 0);">(</span><span class="br0" style="color: rgb(0, 128, 0);">)</span><span class="br0" style="color: rgb(0, 128, 0);">)</span><span class="sy4" style="color: rgb(0, 128, 128);">;</span> <span class="co1" style="color: rgb(102, 102, 102);">//目前應該檢驗的</span> <span class="kw1" style="color: rgb(0, 0, 255);">if</span><span class="br0" style="color: rgb(0, 128, 0);">(</span>current <span class="sy2" style="color: rgb(0, 0, 64);">-</span><span class="sy1" style="color: rgb(0, 0, 128);">></span> left <span class="sy3" style="color: rgb(0, 0, 64);">!</span><span class="sy1" style="color: rgb(0, 0, 128);">=</span> <span class="kw2" style="color: rgb(0, 0, 255);">NULL</span><span class="br0" style="color: rgb(0, 128, 0);">)</span> unvisited.<span class="me1" style="color: rgb(0, 119, 136);">push</span><span class="br0" style="color: rgb(0, 128, 0);">(</span>current <span class="sy2" style="color: rgb(0, 0, 64);">-</span><span class="sy1" style="color: rgb(0, 0, 128);">></span> left<span class="br0" style="color: rgb(0, 128, 0);">)</span><span class="sy4" style="color: rgb(0, 128, 128);">;</span> <span class="co1" style="color: rgb(102, 102, 102);">//把左邊放入queue中</span> <span class="kw1" style="color: rgb(0, 0, 255);">if</span><span class="br0" style="color: rgb(0, 128, 0);">(</span>current <span class="sy2" style="color: rgb(0, 0, 64);">-</span><span class="sy1" style="color: rgb(0, 0, 128);">></span> right <span class="sy3" style="color: rgb(0, 0, 64);">!</span><span class="sy1" style="color: rgb(0, 0, 128);">=</span> <span class="kw2" style="color: rgb(0, 0, 255);">NULL</span><span class="br0" style="color: rgb(0, 128, 0);">)</span> <span class="co1" style="color: rgb(102, 102, 102);">//右边压入。因为QUEUE是一个先进先出的结构,所以即使后面再压其他东西,依然会先访问这个。</span> unvisited.<span class="me1" style="color: rgb(0, 119, 136);">push</span><span class="br0" style="color: rgb(0, 128, 0);">(</span>current <span class="sy2" style="color: rgb(0, 0, 64);">-</span><span class="sy1" style="color: rgb(0, 0, 128);">></span> right<span class="br0" style="color: rgb(0, 128, 0);">)</span><span class="sy4" style="color: rgb(0, 128, 128);">;</span> visited.<span class="me1" style="color: rgb(0, 119, 136);">push</span><span class="br0" style="color: rgb(0, 128, 0);">(</span>current<span class="br0" style="color: rgb(0, 128, 0);">)</span><span class="sy4" style="color: rgb(0, 128, 128);">;</span> <span class="kw3" style="color: rgb(0, 0, 221);">cout</span> <span class="sy1" style="color: rgb(0, 0, 128);"><<</span> current <span class="sy2" style="color: rgb(0, 0, 64);">-</span><span class="sy1" style="color: rgb(0, 0, 128);">></span> self <span class="sy1" style="color: rgb(0, 0, 128);"><<</span> endl<span class="sy4" style="color: rgb(0, 128, 128);">;</span> unvisited.<span class="me1" style="color: rgb(0, 119, 136);">pop</span><span class="br0" style="color: rgb(0, 128, 0);">(</span><span class="br0" style="color: rgb(0, 128, 0);">)</span><span class="sy4" style="color: rgb(0, 128, 128);">;</span> <span class="br0" style="color: rgb(0, 128, 0);">}</span>
特性
空间复杂度
因为所有节点都必须被储存,因此BFS的空间复杂度为 O(|V| + |E|),其中 |V| 是节点的数目,而 |E| 是图中边的数目。注:另一种说法称BFS的空间复杂度为,其中
B 是最大分支系数,而 M 是树的最长路径长度。由于对空间的大量需求,因此BFS并不适合解非常大的问题。
时间复杂度
最差情形下,BFS必须寻找所有到可能节点的所有路径,因此其时间复杂度为 O(|V| + |E|),其中 |V| 是节点的数目,而 |E| 是图中边的数目。
完全性
广度优先搜索算法具有完全性。这意指无论图形的种类如何,只要目标存在,则BFS一定会找到。然而,若目标不存在,且图为无限大,则BFS将不收敛(不会结束)。
最佳解
若所有边的长度相等,广度优先搜索算法是最佳解——亦即它找到的第一个解,距离根节点的边数目一定最少;但对一般的图来说,BFS并不一定回传最佳解。这是因为当图形为加权图(亦即各边长度不同)时,BFS仍然回传从根节点开始,经过边数目最少的解;而这个解距离根节点的距离不一定最短。这个问题可以使用考虑各边权值,BFS的改良算法成本一致搜寻法来解决。然而,若非加权图形,则所有边的长度相等,BFS就能找到最近的最佳解。
广度优先搜索算法的应用
广度优先搜索算法能用来解决图论中的许多问题,例如:寻找图中所有连接元件(Connected Component)。一个连接元件是图中的最大相连子图。
寻找连接元件中的所有节点。
寻找非加权图中任两点的最短路径。
测试一图是否为二分图。
(Reverse) Cuthill–McKee算法
寻找连接元件
由起点开始,执行广度优先搜索算法后所经过的所有节点,即为包含起点的一个连接元件。
测试是否二分图
BFS 可以用以测试二分图。从任一节点开始搜寻,并在搜寻过程中给节点不同的标签。例如,给开始点标签 0,开始点的所有邻居标签 1,开始点所有邻居的邻居标签 2……以此类推。若在搜寻过程中,任一节点有跟其相同标签的邻居,则此图就不是二分图。若搜寻结束时这种情形未发生,则此图为一二分图。
应用于电脑游戏中平面网格
BFS 可用来解决电脑游戏(例如即时策略游戏)中找寻路径的问题。在这个应用中,使用平面网格来代替图形,而一个格子即是图中的一个节点。所有节点都与它的邻居(上、下、左、右、左上、右上、左下、右下)相接。值得一提的是,当这样使用BFS算法时,首先要先检验上、下、左、右的邻居节点,再检验左上、右上、左下、右下的邻居节点。这是因为BFS趋向于先寻找斜向邻居节点,而不是四方的邻居节点,因此找到的路径将不正确。BFS 应该先寻找四方邻居节点,接着才寻找斜向邻居节点1。
相关文章推荐
- 【转】解决svn Authorization failed错误
- 【杭电oj】1022 - Train Problem I(栈)
- JetBrains老用户转换全新订阅授权模式可享受6折优惠
- Mac Air 优势
- SonarQube EXECUTION FAILURE
- 马克扎克伯格谈未来科技趋势:视频、人工智能、VR
- 可失败构造器(Failable Initializers)
- 解析腾讯企业邮箱到自己域名,设置mail的cname
- Codeforces 628F Bear and Fair Set
- Codeforces 628F Bear and Fair Set
- Repaint/Reflow 的基本认识和优化
- tomcat架构分析(容器类 Container)
- GCDAsyncSocketErrorDomain Code=4
- No assembly found containing an OwinStartupAttribute
- 僵尸进程的产生和避免
- waitpid非阻塞时的用法
- 1014. Waiting in Line (30)
- 【技术】正则表达式—匹配电话号码,网址链接,Email地址
- AI快捷键
- Context initialization failed