搜索算法讲解课件.ppt
《搜索算法讲解课件.ppt》由会员分享,可在线阅读,更多相关《搜索算法讲解课件.ppt(58页珍藏版)》请在三一办公上搜索。
1、搜索,人肉搜索,google,度娘,爬虫,文件查找,什么是搜索算法呢?,搜索算法是利用计算机的高性能来有目的地穷举,一个问题的部分或所有的可能情况,从而求出问题,的解的一种方法。,搜索过程实际上是根据初始条件和扩展规则构造,一棵解答树并寻找符合目标状态的节点的过程。,what,?,广度优先搜索,:,从初始状态开始,通过规,则来生成第一层结点,同时检查生成结点中,是否有目标结点,.,若没有则生成下一层接点,并检查是否有目标结点,广度优先搜索,广度优先搜索,?,采用队列存储,?,每次扩展出当前结点的所有子结点,0,1,2,3,4,5,6,广度优先搜索,void BFS(int curNode,in
2、t curDepth),while(front rear),+front;,for(i=0;i m;i+),newNode=Expend(qfront.node),if(!Exist(newNode),qrear+.node=newnode;,if(newNode=target)return;,return;,搜索算法举例:八数码难题,?,在,3,3,的方格棋盘上,分别放置了标有数字,1,、,2,、,3,、,4,、,5,、,6,、,7,和,8,的八个棋子,5,8,4,7,3,6,2,1,5,8,4,7,3,6,2,1,S,0,初始,状态,Sg,目标,状态,空出一个位置使棋子可以移动,形成不同的
3、局面,问题,要使棋盘进入某种预定局面应如何移动棋子,广度,优先,搜索,算法,举例,2 3,1 8 4,7 6 5,1,2,5,6,7,3,目标,8,4,0,层,1,层,2,层,3,层,2 3,1 8 4,7 6 5,2 8 3,1 4,7 6 5,2 3,1 8 4,7 6 5,下,左,右,2 8 3,1 4,7 6 5,2 8 3,1 6 4,7 5,2 8 3,1 4,7 6 5,下,左,右,1 2 3,8 4,7 6 5,下,2 3 4,1 8,7 6 5,下,2 8 3,1 6 4,7 5,2 8 3,1 6 4,7 5,左,右,2 8 3,7 1 4,6 5,8 3,2 1 4,7
4、6 5,上,下,2 8,1 4 3,7 6 5,2 8 3,1 4 5,7 6,上,下,1 2 3,7 8 4,6 5,1 2 3,8 4,7 6 5,下,右,规则:空格上下左右,深度优先搜索,?,用堆栈存储,?,当前结点为下一次扩展结点的父结点,0,1,2,3,4,5,6,void DFS(int curNode,int curDepth),if(curNode=Target)return;,if(curEdpth MaxDepth)return;,for(int i=0;in;+i),int newNode=,Expend,(curNode,i);,DFS(newNode,+curDept
5、h);,return;,?,函数的返回值可以根据具体的情况来设定,深度,优先,搜索,算法,举例,2 3,1 8 4,7 6 5,2 3,1 8 4,7 6 5,2 8 3,1 4,7 6 5,2 3,1 8 4,7 6 5,2 8 3,1 4,7 6 5,2 8 3,1 6 4,7 5,2 8 3,1 4,7 6 5,2 8 3,1 6 4,7 5,2 8 3,1 6 4,7 5,2 8 3,7 1 4,6 5,8 3,2 1 4,7 6 5,2 8,1 4 3,7 6 5,2 8 3,1 4 5,7 6,1 2 3,7 8 4,6 5,1 2 3,8 4,7 6 5,2 8 3,6 4,1
6、7 5,2 8 3,1 6,7 5 4,8 3,2 1 4,7 6 5,2 8 3,7 1 4,6 5,2 8,1 4 3,7 6 5,2 8 3,1 4 5,7 6,1,2,3,4,5,6,7,8,9,10,11,12,13,1 2 3,8 4,7 6 5,目标,0,层,1,层,2,层,3,层,4,层,规则:空格上下左右,下,左,右,?,1241,?,Description,?,The GeoSurvComp geologic survey company is responsible for detecting underground oil,deposits.GeoSurvComp wo
7、rks with one large rectangular region of land at a time,and,creates a grid that divides the land into numerous square plots.It then analyzes each plot,separately,using sensing equipment to determine whether or not the plot contains oil.A plot,containing oil is called a pocket.If two pockets are adja
8、cent,then they are part of the same,oil deposit.Oil deposits can be quite large and may contain numerous pockets.Your job is to,determine how many different oil deposits are contained in a grid.,?,Input,?,The input contains one or more grids.Each grid begins with a line containing m and n,the,number
9、 of rows and columns in the grid,separated by a single space.If m=0 it signals the,end of the input;otherwise 1=m=100 and 1=n=100.Following this are m lines of n,characters each(not counting the end-of-line characters).Each character corresponds to one,plot,and is either*,representing the absence of
10、 oil,or,representing an oil pocket.,?,Output,?,are adjacent horizontally,vertically,or diagonally.An oil deposit will not contain more than,100 pockets.,?,Sample Input,?,1 1,?,*,?,3 5,?,*,?,*,?,*,?,1 8,?,*,?,5 5,?,*,?,*,?,*,?,*,?,*,?,0 0,?,Sample Output,?,0,?,1,?,2,?,2,?,题目的意思就是在给出的图中,代表有石油,*,代表没有石油
11、,而在一个有石油的地方它的,周围,8,个方向的地方如果也有石油,那么这,2,块石油是属于一块的,给出图,问图中有几块石,油田,.,?,若图为下图:,?,?,?,?,是连成一块的,所以图中只有一个油田,?,解题方法:,?,采用深度优先搜索策略,对给出的图中一旦出现一个则对其个方向进行搜索,并,对搜索过的标记,直到一次搜索结束则油田总数加一,最后的总数即为所求的石油,的方块数,。,?,#include,?,using namespace std;,?,?,const int MAX=100;,?,int m,n;,?,char mapMAXMAX;,?,bool flagMAXMAX;,?,int
12、 move_x8=-1,0,1,1,1,0,-1,-1;,?,int move_y8=-1,-1,-1,0,1,1,1,0;,?,?,void Dfs(int x,int y),?,int i;,?,if(mapxy=&flagxy=false),?,flagxy=true;,?,for(i=0;i 8;i+),?,int tx=x+move_xi;,?,int ty=y+move_yi;,?,if(tx=0&ty=0&tx m&ty n&maptxty=&flag txty=false),?,Dfs(tx,ty);,?,?,?,return;,?,?,?,int main(),?,?,whi
13、le(cin m n&m!=0&n!=0),?,memset(flag,false,sizeof(flag);,?,int i,j,sum=0;,?,for(i=0;i m;i+),?,for(j=0;j n;j+),?,cin mapij;,?,?,?,for(i=0;i m;i+),?,for(j=0;j n;j+),?,if(mapij=&flagij=false),?,Dfs(i,j);,?,sum+;,?,?,?,?,cout sum endl;,?,?,return 0;,深度优先搜索,?,优点,空间需求少,深度优先搜索的存储要求是深度,约束的线性函数,?,问题,可能搜索到错误的路
14、径上,在无限空间中可能,陷入无限的搜索,最初搜索到的结果不一定是最优的,广度优先搜索,?,优点,目标节点如果存在,用广度优先搜索算法总可,以找到该目标节点,而且是最小(即最短路径),的节点,?,缺点,当目标节点距离初始节点较远时,会产生许多,无用的节点,搜索效率低,双向广度优先搜索(,DBFS,),?,DBFS,算法是对,BFS,算法的一种扩展。,BFS,算法从起始节点以广度优先的顺序不断扩展,直到遇到目,的节点,DBFS,算法从两个方向以广度优先的顺序同时扩展,一个是从起,始节点开始扩展,另一个是从目的节点扩展,直到一个扩展队,列中出现另外一个队列中已经扩展的节点,也就相当于两个扩,展方向出
15、现了交点,那么可以认为找到了一条路径。,?,比较,DBFS,算法相对于,BFS,算法来说,由于采用了从两个根开始扩展,的方式,搜索树的宽度得到了明显的减少,所以在算法的时间,复杂度和空间复杂度上都有优势!,?,技巧,:,每次扩展结点总是选择结点比较少的那边进行下次,搜索,并不是机械的两边交替。,双向广度优先搜索,?,双向广度优先算法编程的基本框架如下:,?,数据结构:,Queue q1,q2;/,两个队列分别用于两个方向的,扩展(注意在一般的广度优先算法中,只,需要一个队列),int head2,tail2;/,两个队列的头指针和尾指,针,?,算法流程,:,?,一、主控函数,:,?,void
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 搜索 算法 讲解 课件

链接地址:https://www.31ppt.com/p-3766661.html