Data Structure-Recursive Implementation of image depth-first Traversal

Source: Internet
Author: User

I. depth-first Traversal Deep priority TraversalTraversal is similar to the first root traversal of a tree. It is the promotion of the first root traversal of a book. 1. in the initial state, all vertices are not accessed. If a vertex v is started, access this vertex 2. traverse the graph from the inaccessible neighboring contacts of v in sequence until all vertices in the graph with the desired path of v are accessed to 3. if any vertex in the graph is not accessed, start from this vertex and repeat 1 or 2. Until all nodes are accessed.

Int FirstAdjVex (ALGraph G, VertexType v) {// initial condition: Graph G exists, v is a vertex in G // Operation Result: returns the sequence number of the first adjacent vertex of v. If the vertex is not adjacent to the vertex in G,-1 ArcNode * p; int v1; v1 = LocateVex (G, v) is returned ); // v1 is the sequence number of vertex v in graph G, p = G. vertices [v1]. firstarc; if (p) return p-> adjvex; else return-1;} int NextAdjVex (ALGraph G, VertexType v, VertexType w) {// initial condition: Graph G exists, v is a vertex in G, and w is the adjacent vertex of v // Operation Result: returns the sequence number of the next adjacent vertex of v (relative to w. // If w is the last adjacent vertex of v,-1 ArcNode * p; int v1, w1; v1 = LocateVex (G, v) is returned ); // v1 is the sequence number of vertex v in graph G w1 = LocateVex (G, w); // w1 is the sequence number of vertex w in graph G p = G. vertices [v1]. firstarc; while (p & p-> adjvex! = W1) // the pointer p is not null and the table node is not w p = p-> nextarc; if (! P |! P-> nextarc) // w is not found or w is the last adjacent point return-1; else // p-> adjvex = w return p-> nextarc-> adjvex; // return the serial number of the next adjacent vertex of v (relative to w)} Boolean visited [MAX_VERTEX_NUM]; // access flag array (global volume) void (* VisitFunc) (char * v); // function variable (global volume) // recursively traverse graph G in depth first from vertex v. Void DFS (ALGraph G, int v) {int w; VertexType v1, w1; strcpy (v1, GetVex (G, v); visited [v] = TRUE; // set the access flag to TRUE (accessed) VisitFunc (G. vertices [v]. data); // access the v vertex for (w = FirstAdjVex (G, v1); w> = 0; w = NextAdjVex (G, v1, strcpy (w1, getVex (G, w) if (! Visited [w]) DFS (G, w); // recursively call DFS for the unaccessed neighbor w of v} // performs depth-first traversal on Graph G. Void DFSTraverse (ALGraph G, void (* Visit) (char *) {int v; VisitFunc = Visit; // use the global variable VisitFunc, make DFS unnecessary to set the function pointer parameter for (v = 0; v
 
  


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.