The graphs are primarily depth-first traversal (DFS) and breadth-first traversal (BFS).
1 Depth-First traversal--dfs
The depth preference is similar to the first sequence traversal of a tree, starting from the node to be accessed (0), selecting any node adjacent to it (3), accessing it, then accessing it, and 3 adjacent nodes (4), accessing it until it accesses a node that has no neighboring nodes, such as 4 without adjacent nodes, then backtracking one layer, Access 3 of the unreachable adjacency node: Until all the vertices have been accessed.
So depth first requires us to check each vertex recursively.
The storage structure of the diagram used is the adjacency table store in the previous article.
void DFS (agraph *g,int v) { *p; VISIT[V]=1; printf ("%d\n", v); P=g->Adjlist[v].firstedge; while (p!=NULL) { if(visit[p->adjvex]==0) DFS (g,p-Adjvex); P=p->Nextedge; }}
The visit array records whether the current is being accessed.
2 breadth-First traversal--BFS
Breadth-first traversal is similar to a hierarchical traversal of a tree. For example, first access node 0, access and 0 contiguous all nodes (1, 2, 3), and then access the nodes (1, 2, 3) all the neighboring nodes until all the nodes are accessed.
Therefore, BFS will need to use the queue, initially, the initial node to the team, access to the first node, all and its adjacent and not visited the node access and queue, vertex out of the team, loop the above operations, until all the nodes access once (that is, team empty).
The summary is--access, all neighboring nodes access the queue, out of the queue, access to all neighboring nodes enqueued ... Until the team is empty
1 voidBFS (Agraph *g,intVintN)2 {3Edge *p;4 intQue[maxsize];5 intfro=0;//Team First6 intRea=0;//End of Team7printf"%d\n", v);8visit[v]=1;9REA = (rea+1)%N;Tenque[rea]=v; One intJ; A while(fro!=Rea) - { -Fro= (fro+1)%N; thej=Que[fro]; -P=g->Adjlist[j].firstedge; - while(p!=NULL) - { + if(visit[p->adjvex]==0) - { +printf"%d\n",p->Adjvex); Avisit[p->adjvex]=1; atRea= (rea+1)%N; -Que[rea]=p->Adjvex; - } -P=p->Nextedge; - } - } in}
The traversal of graphs