bfs广度优先搜索算法_广度优先搜索(BFS)和深度优先搜索(DFS)算法

bfs广度优先搜索算法

1)广度优先搜索(BFS) (1) Breadth first search (BFS))

Breadth first search explores the space level by level only when there are no more states to be explored at a given level does the algorithm move on to the next level.

广度优先搜索仅当在给定级别上没有更多状态要探索时,算法才继续进行下一个级别。

We implement BFS using lists open and closed to keep track of progress through the state space. In the order list, the elements will be those who have been generated but whose children have not been examined. The closed list records the states that have been examined and whose children have been generated. The order of removing the states from the open list will be the order of searching. The open is maintained as a queue on the first in first out data structure. States are added to the right of the list and removed from the left.

我们使用打开和关闭列表来实现BFS ,以跟踪整个状态空间的进度。 在订单列表中,元素将是已生成但未检查其子元素的元素。 封闭列表记录了已检查的状态及其子代。 从打开列表中删除状态的顺序将是搜索的顺序。 开放保持为先进先出数据结构上的队列。 状态将添加到列表的右侧,然后从左侧删除。

After initialization, each vertex is enqueued at most once and hence dequeued at most once. The operations of enqueuing and dequeuing take O (1) time, so the total time devoted to queue operations is O (v). Because the adjacency list of each vertex is scanned only when the vertex is dequeued, the adjacency list of each vertex is scanned at most once. Since the sum of the lengths of all the adjacency lists is the ta(E), at most O(E) time is spent in total scanning adjacency lists. The overhead for the initialization is O(v), and thus the total running time of BFS is O(v+E). Thus, breadth first search runs in time linear in the size of the adjacency list representation.

初始化后,每个顶点最多入队一次,因此最多出队一次。 入队和出队操作花费O(1)时间,因此用于排队操作的总时间为O(v) 。 因为仅在顶点出列时才扫描每个顶点的邻接表,所以每个顶点的邻接表最多被扫描一次。 由于所有邻接表的长度之和为ta(E),因此在总扫描邻接表中最多花费O(E)时间。 初始化的开销为O(v) ,因此BFS的总运行时间为O(v + E) 。 因此, 广度优先搜索在时间上与邻接列表表示的大小成线性关系。

算法-广度优先搜索(BFS) (Algorithm - Breadth First Search (BFS))

    1.	Begin
    2.	Open = [start];
    3.	Closed =[];
    4.	While open != [] do
    5.	Begin
    6.	Remove leftmost state from open call it x;
    7.	If x is a goal then return success
    8.	Else
    9.	Begin
    10.	Generate children of x;
    11.	Put x on closed;
    12.	Put children on right end of open;
    13.	End
    14.	End
    15.	Return(failure)
    16.	End

2)深度优先搜索(DFS) (2) Depth First Search (DFS))

In depth first search, when a state is examined all of its children and their descendants are examined before any of its siblings. Depth first search goes deeper into the search space whenever this is possible, only when no further descendants of a state can be found, are its siblings considered.

深度优先搜索 ,当检查一个状态时,将在其任何同级之前检查其所有子级及其子孙。 只要有可能, 深度优先搜索就会深入搜索空间,只有在找不到状态的其他后代时,才考虑其同级。

A depth first traversal takes O(N*E) time for adjacency list representation and O(N2) for matrix representation.

深度优先遍历的O(N * E)时间用于邻接表表示, O(N2)用于矩阵表示。

算法-深度优先搜索(DFS) (Algorithm - Depth First Search (DFS))

    1.	Begin
    2.	Open = [start];
    3.	Closed = [];
    4.	While open != [] do
    5.	Begin
    6.	Remove left most state from open call it x;
    7.	If x is a goal then return success
    8.	Else
    9.	Begin
    10.	Generate children of x;
    11.	Put x on closed;
    12.	Put children on left end of open;
    13.	End
    14.	End
    15.	Return (failure)
    16.	End

References:

参考文献:

翻译自: https://www.includehelp.com/algorithms/breadth-first-search-bfs-and-depth-first-search-dfs.aspx

bfs广度优先搜索算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值