Graph depth first traversal and breadth first traversal __ data structure

Source: Internet
Author: User

1. Depth first traversal (DFS)

(1) Starting from a vertex V, accessing the vertex and marking it as visited

(2) Access to the adjacency point of V, if not visited, access to the vertex and marked as visited, and then access the vertex adjacent points, recursive execution

If the vertex has been accessed, return to the previous vertex, and then check that the vertex's adjacency point has been accessed, if there is any access to continue down access, if all have been visited to continue to return to the previous vertex, continue the same steps.


The depth-first traversal is similar to the first-order traversal of a tree, and the depth-first traversal algorithm is not unique.


Select V1 as the starting point, access the V1, and then access the V1 adjacent points, adjacent points have V2 V3 and V4, assuming all from the adjacent points on the left to access

Access the leftmost adjacency point of the V1 V2, and access the adjacent points of the V2 V5

Access to the adjacency point of the V5 v3,v3 the left adjacency point is V1,v1 has been visited, so visit V4

V4 to the left adjacent point V6, access to v6,v6 all adjacent points have been visited, return v4,v4 all adjacent points have also visited the return v3,v3 of the adjacent points all access to return V5,

The adjacency points of the V5 all visit the neighboring points of the returned V2,V2 all access to the point of departure v1,v1 all adjacent points visited, traversal ended.

traversal sequence is v1→v2→v5→v3→v4→v6



2. Breadth First traversal (BFS)

(1) from a vertex V, access to all adjacent points of the vertex v1,v2 ... VN

(2) from the adjacent point v1,v2 ... VN set out to visit all of their neighboring points

(3) Repeat the above steps until all vertices have been accessed


(1) from the vertex V1, V1 team, access to the V1,V1 adjacent points have V2 V3 V4, will they join, V1 teams

Queue: V2 V3 V4

(2) Visit team head v2,v2 adjacent points for V1 (have been visited, ignored) and V5, will V5 team, V2 out

Queue: V3 V4 V5

(3) Access to the v3,v3 of the adjacent points for V1 (access ignored) V4 (has been ignored in the queue) V6 V5 (has been queued, ignored), V6 team, V3 teams

Queue: V4 V5 V6

......


/* adjacency Matrix breadth Traversal algorithm *
/void Bfstraverse (Mgraph G)
{
	int i, J;
	Queue Q;
	for (i = 0; i < g.numvertexes i++)
       	visited[i] = FALSE;
        Initqueue (&q);		/* Initialize an auxiliary queue * *
        for (i = 0; i < g.numvertexes i++)  /* to cycle each vertex/
        {
		if (!visited[i))	* * If not accessed On processing
		/{
			visited[i]=true;		/* Set the current vertex access *
			/printf ("%c", g.vexs[i]);//print vertex, can also be other operation * *
			EnQueue (&q,i);		/* Put this vertex into queue
			/while (! Queueempty (Q))	* * If the current queue is not
			empty
				/{dequeue (&q,&i);	/* The team to the element out of the queue, assigned to I
				/for (j=0;j<g.numvertexes;j++) 
				{/* to 
					determine other vertices with the current vertex existence edge and does not have access to  * *
					if ( G.ARC[I][J] = = 1 &&!visited[j]) 
					{ 
 						visited[j]=true;			/* Mark this vertex found as visited
						/printf ("%c", G.vexs[j]);	/* Print vertex * *
						EnQueue (&q,j);				/* will find this vertex in queue/}}}}

-----------from the "Liar data Structure"

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.