# Learning Notes (d) Traversal of graphs

Source: Internet
Author: User
Tags stdin

There are two ways to traverse the graph: depth-first traversal (Depth-first search) and breadth-first traversal (Breadth-first search).

DFS is usually implemented using recursion, and BFS is usually implemented using queues. The traversal of the graph is the generalization of the traversal of the tree, which accesses the vertices of each vertex sequentially and only once in accordance with some rule (or order), and is the process of linearization the network structure according to some rules. 1.DFS

Basic idea:

Start with a vertex v0 in the diagram, and access this vertex, then, in turn, the depth is first traversed from the adjacent vertices of the v0 until all vertices connected to the V0 path are accessed, and if there are vertices not yet accessed, repeat the process from the selected vertex as the starting point, until all vertices are accessed. It can be seen that depth-first traversal is a recursive process.

Cases:

Enter a graph with a length and width of M and N, which consists of a * number and a # number, and a number of connected blocks for the # number.

Such as:

****#

****#

*#**#

*##**

*#***

This figure has two connected blocks.

Code:

```DFS quadrature block #include <cstdio> #include <cstring> #include <iostream> #include <string>//#define

File #define MAXN namespace std;
struct data{char pic;
int idx;

};

Data ATLAS[MAXN][MAXN];
int m,n; void Dfs (int x,int y,int ID) {if x<0| | x>m| | y<0| |
Y>n) return; if (atlas[x][y].idx>0| |
atlas[x][y].pic!= ' @ ') return;
Atlas[x][y].idx=id; for (int nx=-1;nx<=1;nx++) {for (int ny=-1;ny<=1;ny++) {if nx!=0| |
ny!=0) DFS (X+NX,Y+NY,ID);
int main () {#ifdef file Freopen ("Test.in", "R", stdin);
Freopen ("Test.out", "w", stdout);
#endif//cin>>m>>n; for (int j=0;j<n;j++) {for (int i=0;i<m;i++) {cin>>atlas[i][j].pic;//with CIN to filter not
Visible character Furu return} int countn=0; for (int j=0;j<n;j++) {for (int i=0;i<m;i++) {if (atlas[i][j].pic== ' @ ' &&atlas[i][j].idx==0) DFS (I,J,++COUNTN);
}} cout<<countn<<endl;
return 0;
}```

2.BFS

Basic idea: First of all, from a vertex v0 of the graph, after accessing the V0, sequentially accesses the unreachable vertices adjacent to v0, and then embarks from these vertices, breadth first traversal, until all vertices are accessed.

To implement the method, the vertices are added to the queue, and each element is then added to the queue by the child nodes of the element, and then traversed until the first solution is searched.

BFS's typical example should be a maze, the following is a maze of the shortest path to the program, enter a maze, 1 for the wall, 0 for the road, 2 for the entrance, 3 for the export, to find the shortest distance to the entrance to the exit.

Code:

```BFS Maze #include <queue> #include <iostream> #include <string> #include <cstdio>//#define File

#define MAXN-using namespace std;
struct data{int step;
int x;
int y; };//int mis[maxn][maxn];//Stores the map data start,eend,temp,temp2;//start point, endpoint, temporary variable int main () {#ifdef file freopen ("Test.i
N "," R ", stdin);
Freopen ("Test.out", "w", stdout);
#endif//FILE0;
Queue<data> s;//stores data int inx,iny,outx,outy with queues;
int m,n;
cin>>m>>n;
BOOL Flag=false;
for (int j=1;j<=n;j++) {for (int i=1;i<=m;i++) {cin>>mis[i][j];
if (mis[i][j]==2) {//record start point start.x=i;
Start.y=j;
Start.step=0;
} if (mis[i][j]==3) {//record end eend.x=i;
Eend.y=j;
Eend.step=0;
}} s.push (start); while (!flag) {temP=s.front ();
S.pop (); if (temp.y-1>=1) {//To determine whether or not to be out of bounds if (mis[temp.x][temp.y-1]==0| |
mis[temp.x][temp.y-1]==3) {//When the judgment point is not a wall, the team mis[temp.x][temp.y-1]=1;
temp2.step=temp.step+1;
Temp2.y=temp.y-1;
temp2.x=temp.x;
if (TEMP2.X==EEND.X&AMP;&AMP;TEMP2.Y==EEND.Y) {//To end, mark, end Search S.push (TEMP2);
Flag=true;
} s.push (TEMP2); }}//up if (temp.x+1<=m) {if mis[temp.x+1][temp.y]==0| |
mis[temp.x+1][temp.y]==3) {mis[temp.x+1][temp.y]=1;
temp2.step=temp.step+1;
TEMP2.Y=TEMP.Y;
temp2.x=temp.x+1;
if (TEMP2.X==EEND.X&AMP;&AMP;TEMP2.Y==EEND.Y) {s.push (TEMP2);
Flag=true; } S.pusH (TEMP2); }}//right if (temp.y+1<=n) {if mis[temp.x][temp.y+1]==0| |
mis[temp.x][temp.y+1]==3) {mis[temp.x][temp.y+1]=1;
temp2.step=temp.step+1;
temp2.y=temp.y+1;
temp2.x=temp.x;
if (TEMP2.X==EEND.X&AMP;&AMP;TEMP2.Y==EEND.Y) {s.push (TEMP2);
Flag=true;
} s.push (TEMP2); }}//down if (temp.x-1>=1) {if mis[temp.x-1][temp.y]==0| |
mis[temp.x-1][temp.y]==3) {mis[temp.x-1][temp.y]=1;
temp2.step=temp.step+1;
TEMP2.Y=TEMP.Y;
Temp2.x=temp.x-1;
if (TEMP2.X==EEND.X&AMP;&AMP;TEMP2.Y==EEND.Y) {s.push (TEMP2);
Flag=true;
} s.push (TEMP2); }}//left} while (!s.empty ()) {Temp=s.front ();
S.pop ();
} cout<<temp.step<<endl;
return 0;
}```

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.