Week Summary: Algorithm learning summary of DFS and BFS
One: DFS algorithm
Objective: To reach the leaf node of the structure being searched.
Definition: Assuming that the initial state of the given figure G is all the points have not been visited, in G, select a point V as the initial starting point, first access to the starting point and mark, and then sequentially from V to search for each of the V adjacent point W, if W has not appeared, the W is a depth-first traversal (DFS), Know that all the points that are connected to V are accessed.
If you start looking for a path of length 4 from V0:
Thought steps:
First look for all the neighboring points of V0: Dis{v1,v2,v3},v1 has not been visited, so the V1 is deeply traversed and marked V1 for access, at this time the path length is 1, and then find the neighbor of V1: Dis={v2}, continue the deep traversal of V2, at this time the length of 2, when dis={} , found that the dis is empty, returned to the V1, found that V1 no other neighboring points (because V2 has been marked) return V0, found that the adjacent point is only V3, the length is equal to 2, the depth of the V3 traverse and will V3 mark, adjacent point dis = {V4,v5},v4 not visited, Deep traversal of the V4 and the V4 marker, length 3, neighbor dis={}, return V3, find the neighboring V5, finally found V6, found the length = 4, to meet the test instructions.
Self-Summary: DFS is a recursive call, as you meet a hole, there are many forked holes in the hole, you have to walk every step of the pre-determined road to find the most appropriate way.
Pseudo code:
1 BOOLDFS (intNintd)2 if(End (N,D))//Meet End Condition3 return true;4 for(Node NextNodeintN) {5 if(!Visited[nextnode]) {6Visited[nextnode] =true;7 if(DFS (nextnode,n+1)){8 //do some things9 //return true;Ten } One } AVisited[nextnode] =false; - } - return false; the}
Through its own recursive call, you can constantly adjust the depth, know the last to find the desired depth or can not be found. To make a dynamic adjustment to whether or not to access it, it is convenient to find the final path.
Example: Hex number (very simple example of Dfs): the title is not written
Directly on the code:
#include "stdio.h" int a[13] = {0};int V[13] = {0}; void Jude (); void DFS (int x); void Main () { a[1] = 1;a[2] = 8;a[12] = 3; V[1] = 1;v[8] = 1;v[3] = 1; DFS (1); }void DFS (int x) { int i = 0; if (x==1 | | x==2 | | x==12) { DFS (x+1); } if (x==13) { Jude (); } for (i=1;i<13;i++) { if (v[i]==0) { v[i] = 1; A[X] = i; DFS (x+1); V[i] = 0;}} } void Jude () { int i = 0; int b[6] = {0}; B[0] = a[1]+a[3]+a[6]+a[8]; B[1] = a[1]+a[4]+a[7]+a[11]; B[2] = a[2]+a[3]+a[4]+a[5]; B[3] = a[2]+a[6]+a[9]+a[12]; B[4] = a[5]+a[7]+a[10]+a[12]; B[5] = a[8]+a[9]+a[10]+a[11]; for (i=1;i<6;i++) { if (b[i]! = B[i-1]) { return; } } printf ("a[6] =%d", a[6]);}
(Tested, the code has no problems.) )
Two: BFS algorithm
The width-first algorithm belongs to the blind search method, which starts from the root node and traverses the tree's nodes along the tree's width, and terminates if all the nodes are accessed.
Process: Set the starting position of the node is V0, the end of the node is VD, the V0 into the gray aggregate (the node to be accessed), remove the element vn from the gray set, mark it as black, and then observe all the critical points of the VN into the set neighbor set, to observe whether there is vd in neighbor, If not, the elements in the neighbor are placed in the gray collection body. Until VD is found or all nodes are traversed.
Refer to Wikipedia pictures:
The picture looks more visualized, a is the starting point H (the picture may be a bit wrong) at the end of the point.
Pseudo code (directly from Wikipedia):
/*** ADDQ (q, p)-p push into q* DELQ (q)-POP Q and return to Q Top * FIRSTADJ (G,V)-V's first neighbor, cannot find the next neighbor that returns -1* Nextadj (g,v)-V, find Not to return -1* VISIT (v)-interview v* visited []-whether or not the *//* breadth-first search algorithm */void BFS (vlink g[], int v) { int w; /* Visit V and enter team * /VISIT (v); Visited[v]=1; ADDQ (q,v); /* The elements of the team Q * /while (! EMPTYQ (q)) { V=DELQ (q); W=firstadj (g,v); /* The neighboring points */do {/* to access and enter the team */ if (visited[w] = = 0) { VISIT (w); ADDQ (q,w); visited[w]=1; } } while (W=nextadj (g,v))! =-1) }}/* The main algorithm for the first search of the map g= (v,e) */void Travel_bfs (Vlink g[], bool visited[], int n) { I NT I; Clear 0 Label for (i = 0; i < n; i + +) visited[i] = 0; for (i = 0; i < n; i + +) if (visited[i] = = 0) BFS (g,i);}
The code seems a bit difficult to understand, because the concept of the stack, and in the C language, stack can be used to do an array instead.
Summary: BFS is a blind search method that finds the results you want by traversing the neighboring points of all neighboring nodes.
Example: Walk the Maze:
#include "stdio.h" #include "stdlib.h" int dir[4][2]={1,0,//x+1,y-1,0,//x-1,y 0,1,//x,y+1 0,-1}; x,y-1//can walk in four directions struct node{int x; Inty;}; struct node queue[50]; The queue record can go to the point struct node Record[5][5];//record record pity Dorado of the precursor void BFs () {int head,tail,i; struct node cur; struct node Next;//cur is the current position, next is the next position head=tail=0; cur.x=queue[tail].x; CUR.Y=QUEUE[TAIL].Y; tail++; while (Head<tail) {cur=queue[head++]; for (i=0;i<4;i++) {next.x=cur.x+dir[i][0]; NEXT.Y=CUR.Y+DIR[I][1]; if (next.x>=0&&next.y>=0&&next.x<5&&next.y<5&&map[next.x][next.y]== 0)//0 is a walking route, can not run out of the map range {record[next.x][next.y].x=cur.x; record[next.x][next.y].y=cur.y;//Records Next's predecessor, the coordinates of next (because next records the first precursor to the site, and is then marked to pass,So do not worry about being covered by the subsequent coordinates of the precursor) if (next.x==4&&next.y==4) return; else {map[next.x][next.y]=1;//tag go Over Queue[tail++]=next; }}}}}int main () {int i,j,k,m,n; struct node cur; for (i=0;i<5;i++) for (j=0;j<5;j++) scanf ("%d", &map[i][j]); cur.x=0; cur.y=0; Map[0][0]=1; Queue[0]=cur; BFS (); k=0; queue[k].x=4; queue[k++].y=4; i=j=4; while (i!=0| | j!=0)//Based on record records, retrace its path from back to forward and exist in the queue {m=i;n=j; i=record[m][n].x; J=RECORD[M][N].Y; Queue[k].x=i; Queue[k++].y=j; } for (i=k-1;i>=0;i--)//Output path printf ("(%d,%d) \ n", queue[i].x,queue[i].y); Return 0;}
Code taken from: http://blog.sina.com.cn/s/blog_7e5541250100ssue.html
The ideas are clear and the notes are clear. You can refer to that blog post (worship the great God).
I hope you can leave your ideas, we can communicate together. I am a rookie, but I believe that I will become stronger.
Fighting ........
Week Summary (2017.2.16): First week algorithm learning.