CS 188 (3) Breadth First Search BFS(广度优先搜索算法)

2023-10-28 10:59

本文主要是介绍CS 188 (3) Breadth First Search BFS(广度优先搜索算法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

    本文要实现 Breadth First Search BFS(广度优先搜索算法) ,首先搜索搜索树中最浅的节点,广度搜索算法搜索到达目标。

    Queue是一个先进先出(FIFO)队列策略的容器,Queue是一个类,使用列表List初始化,入站在列表List中头部插入一个元素,出栈使用pop实现,将仍在队列中的最早进入队列的项目出列,这个操作从队列中删除该项。从列表的长度为0判断栈是否为空。   

class Queue:"A container with a first-in-first-out (FIFO) queuing policy."def __init__(self):self.list = []def push(self,item):"Enqueue the 'item' into the queue"self.list.insert(0,item)def pop(self):"""Dequeue the earliest enqueued item still in the queue. Thisoperation removes the item from the queue."""return self.list.pop()def isEmpty(self):"Returns true if the queue is empty"return len(self.list) == 0

 

广度优先算法BFS代码:

# search.py
# ---------
# Licensing Information:  You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
# 
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).def breadthFirstSearch(problem):"""Search the shallowest nodes in the search tree first.""""*** YOUR CODE HERE ***"#util.raiseNotDefined()path =Path([problem.getStartState()],[],0)if problem.isGoalState(problem.getStartState()):return path.directions#新建一个队列,起始状态入队列queue =util.Queue()    queue.push(path)#取得初始状态列表visited =[problem.getStartState()]while not queue.isEmpty():#如队列不为空,取最先进入队列的元素(List的最后一个元素),获取当前路径currentPath = queue.pop()currentLocation = currentPath.locations[-1]#如果当前位置已经是终点的位置,则返回当前路径的方向列表,用于移动pac man。if problem.isGoalState(currentLocation):return currentPath.directionselse:#在搜索问题中取得当前位置后继的下一个状态.getSuccessors中for循环遍历北、南、东、西四个方向,#directionToVector取得方向到坐标偏移向量的转换值,在当前坐标上加上位移的坐标偏移量值,#如果下一步坐标移动的点不是围墙,则在后续状态列表中加入三元组( nextState, action, cost)nextSteps = problem.getSuccessors(currentLocation)for nextStep in nextSteps:#遍历下一步的状态,依次获得位置、方向、成本信息nextLocation =nextStep[0]nextDirection = nextStep[1]nextCost = nextStep[2]# 不在当前路径里面而且下一个位置还没被访问(多条路径交叉点)if (nextLocation not in currentPath.locations) and (nextLocation not in visited):if not problem.isGoalState(nextLocation):visited.append(nextLocation)print("访问的位置:", visited)#获取当前路径列表集nextLocations =currentPath.locations[:]#将新的位置加入到当前路径的列表里面nextLocations.append(nextLocation)print("当前位置:",currentLocation)print("当前位置下一步可能的移动位置:",nextLocation)print("加到当前位置列表集:",nextLocations)print()print()#print(currentLocation,nextLocation,nextLocations)#获取当前的方向集nextDirections = currentPath.directions[:]#将新的方向加入到当前方向集的列表里面nextDirections.append(nextDirection)nextCosts = currentPath.cost +nextCostnextPath =Path(nextLocations,nextDirections,nextCosts)#下一步的状态,入队列queue.push(nextPath)                    #队列为空,仍未到达终点,返回空集return []

    案例使用tinyMaze布局,从搜索问题problem的walls属性信息中可以获取迷宫墙的数字信息,如下图所示,要从(5,5)点到达(1,1)点,和深度优先算法不同,深度优先算法采样堆栈沿着一条路径深入遍历,然后遍历另一条路径;广度优先算法采用队列先进先出,层层遍历两条路径:

     pac man移动北、南、东、西移动与x,y的坐标变换关系:

广度优先搜索算法的遍历如下:

[SearchAgent] using function breadthFirstSearch
[SearchAgent] using problem type PositionSearchProblem
访问的位置: [(5, 5), (5, 4)]
当前位置: (5, 5)
当前位置下一步可能的移动位置: (5, 4)
加到当前位置列表集: [(5, 5), (5, 4)]访问的位置: [(5, 5), (5, 4), (4, 5)]
当前位置: (5, 5)
当前位置下一步可能的移动位置: (4, 5)
加到当前位置列表集: [(5, 5), (4, 5)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3)]
当前位置: (5, 4)
当前位置下一步可能的移动位置: (5, 3)
加到当前位置列表集: [(5, 5), (5, 4), (5, 3)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5)]
当前位置: (4, 5)
当前位置下一步可能的移动位置: (3, 5)
加到当前位置列表集: [(5, 5), (4, 5), (3, 5)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3)]
当前位置: (5, 3)
当前位置下一步可能的移动位置: (4, 3)
加到当前位置列表集: [(5, 5), (5, 4), (5, 3), (4, 3)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5)]
当前位置: (3, 5)
当前位置下一步可能的移动位置: (2, 5)
加到当前位置列表集: [(5, 5), (4, 5), (3, 5), (2, 5)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5), (4, 2)]
当前位置: (4, 3)
当前位置下一步可能的移动位置: (4, 2)
加到当前位置列表集: [(5, 5), (5, 4), (5, 3), (4, 3), (4, 2)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5), (4, 2), (1, 5)]
当前位置: (2, 5)
当前位置下一步可能的移动位置: (1, 5)
加到当前位置列表集: [(5, 5), (4, 5), (3, 5), (2, 5), (1, 5)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5), (4, 2), (1, 5), (3, 2)]
当前位置: (4, 2)
当前位置下一步可能的移动位置: (3, 2)
加到当前位置列表集: [(5, 5), (5, 4), (5, 3), (4, 3), (4, 2), (3, 2)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5), (4, 2), (1, 5), (3, 2), (1, 4)]
当前位置: (1, 5)
当前位置下一步可能的移动位置: (1, 4)
加到当前位置列表集: [(5, 5), (4, 5), (3, 5), (2, 5), (1, 5), (1, 4)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5), (4, 2), (1, 5), (3, 2), (1, 4), (2, 2)]
当前位置: (3, 2)
当前位置下一步可能的移动位置: (2, 2)
加到当前位置列表集: [(5, 5), (5, 4), (5, 3), (4, 3), (4, 2), (3, 2), (2, 2)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5), (4, 2), (1, 5), (3, 2), (1, 4), (2, 2), (1, 3)]
当前位置: (1, 4)
当前位置下一步可能的移动位置: (1, 3)
加到当前位置列表集: [(5, 5), (4, 5), (3, 5), (2, 5), (1, 5), (1, 4), (1, 3)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5), (4, 2), (1, 5), (3, 2), (1, 4), (2, 2), (1, 3), (2, 3)]
当前位置: (2, 2)
当前位置下一步可能的移动位置: (2, 3)
加到当前位置列表集: [(5, 5), (5, 4), (5, 3), (4, 3), (4, 2), (3, 2), (2, 2), (2, 3)]访问的位置: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5), (4, 2), (1, 5), (3, 2), (1, 4), (2, 2), (1, 3), (2, 3), (2, 1)]
当前位置: (2, 2)
当前位置下一步可能的移动位置: (2, 1)
加到当前位置列表集: [(5, 5), (5, 4), (5, 3), (4, 3), (4, 2), (3, 2), (2, 2), (2, 1)]......

欢迎关注微信公众号:“从零起步学习人工智能”。

 

这篇关于CS 188 (3) Breadth First Search BFS(广度优先搜索算法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/292748

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

hdu1180(广搜+优先队列)

此题要求最少到达目标点T的最短时间,所以我选择了广度优先搜索,并且要用到优先队列。 另外此题注意点较多,比如说可以在某个点停留,我wa了好多两次,就是因为忽略了这一点,然后参考了大神的思想,然后经过反复修改才AC的 这是我的代码 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

poj 3190 优先队列+贪心

题意: 有n头牛,分别给他们挤奶的时间。 然后每头牛挤奶的时候都要在一个stall里面,并且每个stall每次只能占用一头牛。 问最少需要多少个stall,并输出每头牛所在的stall。 e.g 样例: INPUT: 51 102 43 65 84 7 OUTPUT: 412324 HINT: Explanation of the s

poj 2431 poj 3253 优先队列的运用

poj 2431: 题意: 一条路起点为0, 终点为l。 卡车初始时在0点,并且有p升油,假设油箱无限大。 给n个加油站,每个加油站距离终点 l 距离为 x[i],可以加的油量为fuel[i]。 问最少加几次油可以到达终点,若不能到达,输出-1。 解析: 《挑战程序设计竞赛》: “在卡车开往终点的途中,只有在加油站才可以加油。但是,如果认为“在到达加油站i时,就获得了一

poj 2195 bfs+有流量限制的最小费用流

题意: 给一张n * m(100 * 100)的图,图中” . " 代表空地, “ M ” 代表人, “ H ” 代表家。 现在,要你安排每个人从他所在的地方移动到家里,每移动一格的消耗是1,求最小的消耗。 人可以移动到家的那一格但是不进去。 解析: 先用bfs搞出每个M与每个H的距离。 然后就是网络流的建图过程了,先抽象出源点s和汇点t。 令源点与每个人相连,容量为1,费用为

AI基础 L9 Local Search II 局部搜索

Local Beam search 对于当前的所有k个状态,生成它们的所有可能后继状态。 检查生成的后继状态中是否有任何状态是解决方案。 如果所有后继状态都不是解决方案,则从所有后继状态中选择k个最佳状态。 当达到预设的迭代次数或满足某个终止条件时,算法停止。 — Choose k successors randomly, biased towards good ones — Close

POJ 3057 最大二分匹配+bfs + 二分

SampleInput35 5XXDXXX...XD...XX...DXXXXX5 12XXXXXXXXXXXXX..........DX.XXXXXXXXXXX..........XXXXXXXXXXXXX5 5XDXXXX.X.DXX.XXD.X.XXXXDXSampleOutput321impossible

POJ2010 贪心优先队列

c头牛,需要选n头(奇数);学校总共有f的资金, 每头牛分数score和学费cost,问合法招生方案中,中间分数(即排名第(n+1)/2)最高的是多少。 n头牛按照先score后cost从小到大排序; 枚举中间score的牛,  预处理左边与右边的最小花费和。 预处理直接优先队列贪心 public class Main {public static voi

深度优先(DFS)和广度优先(BFS)——算法

深度优先 深度优先搜索算法(英语:Depth-First-Search,DFS)是一种用于遍历或搜索树或图的算法。 沿着树的深度遍历树的节点,尽可能深的搜索树的分支,当节点v的所在边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访