BFS:广度优先搜索 Breadth First Search,搜索是在宽度方向上一层一层遍历下去,很适合使用队列实现,所以在认识BFS前先熟悉队列
队列
队列是一种数据结构,队列(queue)内的元素满足先进先出原则,First In First Out,简写为FIFO;
队列的操作
队列应该有以下操作:
1.判断队列是否为空,时间复杂度O(1)
2.访问队列首元素而不出队,时间复杂度O(1)
3.新元素入队,如果空间不够,会overflow(上溢出)
4.旧元素出队,如果队列已经为空,会underflow(下溢出)
5.确定队列中元素个数,时间复杂度为O(1)
6.清空队列,时间复杂度为O(n)
队列有两种实现方式,一种是线状队列,另一种是环状队列
线状队列的实现
线状队列结构如下:

假设队列的头部为head,尾部为tail,size为head到tail的元素个数(闭区间),三者有如下关系:
t
a
i
l
=
h
e
a
d
+
s
i
z
e
−
1
tail=head+size-1
tail=head+size−1
所以,一个容量为n的队列应该满足以下关系:
- 初始化时,head=size=0
- 判断为空,size=0
- 判断队列已满,head+size=n
- 入队:queue[head+size]=elem,size++
- 出队:elem=queue[head],head++,size–
- 获取队列头部元素:queue[head]
- 获取队列尾部元素:queue[head+size-1]
该队列的实现如下:
#线状普通队列 x x tail x x x x head x x
#元素从tail进入,从head弹出
# tail=head+size-1
import numpy as np
class LinearQueue(object):
def __init__(self,capacity):
self.queue=np.zeros(capacity)
self.head=0
self.size=0
self.capacity=capacity
def isempty(self):
return True if self.size==0 else False
def isfull(self):
return True if self.capacity==self.head+self.size else False
#入队
def addelem(self,elem):
self.queue[self.head+self.size]=elem
self.size+=1
#出队
def popelem(self):
elem=self.queue[self.head]
self.head+=1
self.size-=1
return elem
#查看队列首部的元素
def gethead(self):
return self.queue[self.head]
#查看队列尾部的元素
def gettail(self):
return self.queue[self.head+self.size-1]
thequeue=LinearQueue(7)
elem=[2,3,1,6,7,9]
for e in elem:
thequeue.addelem(e)
print(thequeue.queue)
thequeue.popelem()
print(thequeue.gethead())
print(thequeue.gettail())
thequeue.popelem()
print(thequeue.gethead())
print(thequeue.gettail())
打印结果为:
[2. 3. 1. 6. 7. 9. 0.]
3.0
9.0
1.0
9.0
环状队列的实现
可以看出,线状队列有个缺点,当pop元素后,head向后移动,可用空间会逐渐减少,所以设计了环状队列:

线状队列在有关head的操作上用取余数改进,使队列的head可以在数组内循环移动,即构成环状队列;
环状队列应有以下操作:
- 初始化时,head=size=0
- 判断队列为空,size=0
- 判断队列已满,size=n
- 新元素入队:queue[(head+size)%n]=elem,size++
- 旧元素出队:elem=queue[head],head=(head+1)%n,size–
- 获取队列首部元素:queue[head]
- 获取队列尾部元素:queue[(head+size-1)%n]
环状队列实现如下:
#环状队列的空间不会随着head的移动减少
#环状队列
import numpy as np
class CycleQueue(object):
def __init__(self,capacity):
self.queue=np.zeros(capacity)
self.head=0
self.size=0
self.capacity=capacity
def isempty(self):
return True if slef.size==0 else False
def isfull(self):
return True if self.size==self.capacity else False
#入队
def addelem(self,elem):
self.queue[(self.head+self.size)%self.capacity]=elem
self.size+=1
#出队
def popelem(self):
elem=self.queue[self.head]
self.head=(self.head+1)%self.capacity
self.size-=1
return elem
#查看队列首部的元素
def gethead(self):
return self.queue[self.head]
#查看队列尾部的元素
def gettail(self):
return self.queue[(self.head+self.size-1)%self.capacity]
thequeue=CycleQueue(7)
elem=[2,3,1,6,7,9]
for e in elem:
thequeue.addelem(e)
print(thequeue.queue)
thequeue.popelem()
print(thequeue.gethead())
print(thequeue.gettail())
thequeue.popelem()
print(thequeue.gethead())
print(thequeue.gettail())
打印结果为:
[2. 3. 1. 6. 7. 9. 0.]
3.0
9.0
1.0
9.0
BFS算法
BFS常用于对隐式图(由题目构建出一个图)的搜索,BFS按宽度优先的顺序进行搜索,对于如下图:

步骤:
1、首先A入队列,
2、A出队列时,A的邻接结点B,C相应进入队列
3、B出队列时,B的邻接结点A,C,D中未进过队列的D进入队列
4、C出队列时,C的邻接结点A,B,D,E中未进过队列的E进入队列
5、D出队列时,D的邻接结点B,C,E,F中未进过队列的F进入队列
6、E出队列,没有结点可再进队列
7、F出队列,没有结点可再进队列
所以遍历为:A B C D E F ;
可以看出,BFS是自起点开始,从相邻节点一层一层向边缘递进访问的,访问相邻节点的相邻节点时会存在节点重复,对于已访问过的节点不再访问即可,由于一层一层的遍历,确保到达终点不会走多余的步骤,从而得到最短路径;
BFS流程总结
选择初始状态节点x入队,从queue中出队x,获得x的扩展点,得到很多y;然后对y进行判重,如果y是新节点,则入队;下次选择入队的y出队,重复以上操作
BFS应用
一个应用BFS的实例是wod ladder,给定字典和一个起点单词,还有一个终点单词,每次只能变换一个字母,问从起点单词是否可以到达终点单词?最短多少步?
注意:
每次只能改变一个字母;
每个转换的单词都必须存在于单词列表中;
例如:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
则顺序为“hit” -> “hot” -> “dot” -> “dog” -> “cog”;
或者“hit” -> “hot” -> “lot” -> “log” -> “cog”;
面对这个问题,看出实际上这是一个BFS问题:
从beginWord出发,然后访问它的邻居——出现在dict中并且只改变一个字符的单词,然后,访问这些邻居中尚未访问的邻居,直到访问到endWord位置;
这是隐式图,构建图如下:
hit入队,然后hit出队;
hot入队,hot出队,lot和dot入队;
lot出队,hot和dot已经访问过,所以log入队;
dot出队,hot和lot已经访问过,所以dog入队;
log出队,lot和dog已经访问过,cog入队,访问到cog,结束;
中间没有走多余步,hit到cog确实是最短路径
364

被折叠的 条评论
为什么被折叠?



