Figure--breadth-first traversal

Source: Internet
Author: User
Tags printf
Breadth-First traversal (breadth-firsttraversal)

1. Recursive definition of breadth-first traversal

The initial state of Figure g is that all vertices have not been accessed. If you select a vertex v as the source point in G, then the breadth-first traversal can be defined as: first access the starting point V, then access all the adjacency points of V in turn W 1,w 2,...,w T, and then access all the never visited vertices adjacent to W l,w 2,...,w T. And so on, until all the vertices in the diagram that have paths to the source point v have been accessed. The search process begins at the end of the V.
If G is a connected graph, then the traversal is done; otherwise, select another unused vertex in Figure C as the source point to continue the above search process until all vertices in g have been accessed.
Breadth-first traversal is similar to a tree-by-hierarchy traversal. The search method used is characterized by searching the landscape as far as possible, so it is called breadth-first search (Breadth-firstsearch). The corresponding traversal is naturally referred to as breadth-first traversal.

2. Breadth-First search process
In the breadth-first search process, setting x and Y is the two unreachable vertices that are to be accessed successively. Their adjacency points are recorded as x 1,x 2,...,x s and y 1,y 2,...,y T.
To ensure that the vertices visited first are also accessed first, the FIFO queue is used during the search to hold the vertices that have been visited. When x and y are accessed, the two vertices are queued successively. Thereafter, when X and Y were out of line, we searched for their adjacency points x 1,x 2,...,x s and y 1,y 2,...,y T, respectively, from X and Y, and visited and set up the team. This approach is to assign each vertex to the team that has been visited, so that each vertex is guaranteed to be at most one team at a time.

3. Breadth-First search algorithm
(1) The breadth-first search algorithm of adjacency table representation graph
void BFS (Algraph*g,int k)
{//V-K as source point for breadth-first search of graph G represented by adjacency table
int i;
Cirqueue Q;//must change datatype in queue definition to int
Edgenode *p;
Initqueue (&q);//Queue initialization
Access Source Point v K
printf ("Visit vertex:%e", G->adjlist[k].vertex);
Visited[k]=true;
EnQueue (&q,k);//v K has been visited and will be his team. (Actually it's the serial number of the People team)
while (! Queueempty (&q)) {//Team non-empty Execute
I=dequeue (&q);//equivalent to V I out of the team
p=g->adjlist[i].firstedge;//Take the side header pointer of V I
while (p) {//search for V i's adjacency Point v J (make P->adjvex=j)
if (!visited[p->adivex]) {//if V J has not been visited
printf ("visitvertex:%c", C->adjlistlp->adjvex].vertex);//Access V J
visited[p->adjvex]=true;
EnQueue (&q,p->adjvex);//visited V-J team
}//endif
p=p->next;//finding the next adjacency point of V I
}//endwhile
}//endwhile

}//end of BFS


(2) Breadth-first search algorithm for graphs represented by adjacency matrices
void Bfsm (Mgraph *g,int k)
{Use VK as the source point for the breadth-first search of graph G represented by the adjacency matrix
int i,j;
Cirqueue Q;
Initqueue (&Q);
printf ("Visit vertex:%c", g->vexs[k]);//access source point VK
Visited[k]=true;
EnQueue (&Q,K);
while (! Queueempty (&q)) {
I=dequeue (&q);//vi the team.
for (j=0;j<g->n;j++)//search for the adjacency point of VI VJ
if (G->edges[i][j]==1&&!visited[j]) {//vi not accessed
printf ("Visit vertex:%c", G->vexs[j]);//Access VI
Visited[j]=true;
EnQueue (&AMP;Q,J);//The VI team who visited
}
}//endwhile
}//bfsm

(3) Breadth-first traversal algorithm
Similar to Dfstraverse. "See Dfstraverse algorithm"

4. Breadth-first traversal sequence of graphs
The vertex sequence of breadth-first traversal graph is defined as the breadth-first traversal sequence of the graph, or the BFS sequence.
(1) The BFS sequence of a graph is not unique
(2) Given the source point and the storage structure of the graph, the BFS sequence given by the algorithm BFS and BFSM is the only


For example, the BFS search process and the BFS sequence "G7" with adjacency table as storage structure and V0 as the starting point "See animation Demo"

5. Algorithm Analysis
Each vertex is enqueued once for an no-map or a forward graph with n vertices and an e-bar edge. The time complexity of the breadth-first traversal (bfstraverse) graph is the same as the dfstraverse algorithm.
When the graph is a connected graph, the bfstraverse algorithm only needs to call a BFS or BFSM to complete the traversal operation, when the time complexity of BFS and BFSM is O (n+e) and 0 (N2) respectively.

Contact Us

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.