Depth-first search for graphs

Source: Internet
Author: User

1. Overviewthe process of depth-first search traversal diagram is a recursive process, which can be implemented by recursive algorithm. In order to avoid repeated accesses to the vertex after accessing a vertex and then back to the vertices, it is necessary to identify each visited vertex during the traverse of the graph, so as to avoid the occurrence of a vertex being repeatedly accessed. So we set the graph of n vertices in the traversal algorithm to an array of access flags of length n visited[n], each element of the array is initialized to 0, and once a vertex i is accessed then the corresponding visited[i] is placed as the visited flag.
Reference Code
1#include <stdio.h>2#include <stdlib.h>3 #defineMAXSIZE 304 5typedefstructNode//adjacency Table Nodes6 {7     intAdjvex;//adjacency Point Field8     structNode *next;//pointer field pointing to the next adjacent edge node9}edgenode;//adjacency table node typeTen  OnetypedefstructVnode//Vertex table Nodes A { -     intVertex//Vertex Fields -Edgenode *firstedge;//pointer field pointing to the first adjacency edge node of the adjacency table the}vertexnode;//Vertex table node types -  - voidCreatadjlist (Vertexnode g[],intEintN//Create an adjacency table of an g[graph, n is the number of vertices, E is the number of edges, and the N -node stores the nodes - { +Edgenode *p; -     inti,j,k; +printf"Input Date of Vetex (0~n-1): \ n"); A      for(i=0; i<n;i++)//Create a table of vertices with n vertices at     { -G[i].vertex=i;//read in vertex i information -G[i].firstedge=null;//Initialize the adjacency table header pointer to vertex i -     } -     for(k=1; k<=e;k++)//Enter the e-bar -    { inprintf"Input edge of (I,J):"); -scanf"%d,%d",&i,&j); top= (Edgenode *)malloc(sizeof(Edgenode)); +p->adjvex=j;//adding nodes with adjacency points to J in the adjacency table of vertex VI -p->next=g[i].firstedge;//inserts are made in the Adjacency table header theG[i].firstedge=p; *p= (Edgenode *)malloc(sizeof(Edgenode)); $p->adjvex=i;//adding nodes with adjacency points to I in adjacency table of vertex VJPanax Notoginsengp->next=g[j].firstedge;//inserts are made in the Adjacency table header -G[j].firstedge=p; the    } + } A  the intVisited[maxsize];//MaxSize is a constant greater than or equal to the number of non-graph vertices + voidDFS (Vertexnode g[],intI//refine the search from the specified vertex i - { $Edgenode *p; $printf"%4d", G[i].vertex);//output vertex i information, i.e. access vertex i -visited[i]=1; -P=g[i].firstedge;//finds the first adjacency edge node of its adjacency table based on the pointer of vertex i firstedge the  -      while(P!=null)//When the adjacency node is not emptyWuyi     { the         if(!visited[p->adjvex])//If this edge node of the adjacency has not been accessed -DFS (G,p->adjvex);//Depth-First search for this edge node. Wup=p->next;//find the next adjacent edge node of vertex i -     } About } $  - voidDfstraverse (Vertexnode g[],intN//Depth-First search traverses graphs stored in adjacency tables where g is the vertex table and n is the number of vertices - { -     inti; A      for(i=0; i<n;i++) +visited[i]=0;//Access Flag 0 the      for(i=0; i<n;i++)//A graph of n vertices finds the vertices that have not been visited and begins the traversal by that vertex -         if(!visited[i])//when visited[i] equals 0 o'clock that vertex i has not visited $DFS (G,i);//vertex I never visited begins to traverse the } the  the voidMain () the { -     intE,n; inVertexnode G[maxsize];//defines the vertex table node type array g theprintf"Input Number of node:\n");//Enter the number of nodes in the graph . thescanf"%d",&n); Aboutprintf"Input Number of edge:\n");//enter the number of edges in the graph thescanf"%d",&e); theprintf"Make adjlist:\n"); theCreatadjlist (G,e,n);//Establishment of adjacency tables for graphs with no direction +printf"dfstraverse:\n"); -Dfstraverse (G,n);//Depth-first traversal of a graph stored in adjacency table theprintf"\ n");Bayi}


Analysis:by Creatadjlist (G,e,n) to establish a graph adjacency table, know, g[i].firstedge->next=g[i].firstedge,g[j].firstedge->next=g[j]. Firstedge; This data type is like a linked list that is inserted from the "head" and satisfies the "last in First Out" feature; the depth traversal first iterates through the connected graphs, and if the graph connection is interrupted, a new node traversal is initiated. Based on the deep traversal process of a simple analysis diagram:1 with "0" vertex traversal, because the "0" Vertex connected Edge has "3" and "1" vertex, because the vertex "3" is "back into" the linked list, so first traverse;2 with vertex "3" traversal, find the "3" vertex connected to the edge has "0" and "4" vertex, because "0" vertex has been traversed, so access to "4" vertex;3 with the vertex "4" traversal, find the edge with the vertex "4" has "2" and "3" vertex , because the vertex "2" is "back into" the linked list, so first traverse; 4 with vertex "2" traversal, find the edge with vertex "2" has "4" and "3" vertices , because "4" and "3" vertices have been visited, and then back to the same place;5 from the new vertex "1" to start the traversal, find the vertex "1" is connected to the edge has "5" and "0" vertex, because the vertex "5" is "back into" the linked list, so first traverse; 6 finally from the vertex "5" to start the traversal, found that the 0~5 node has been traversed, the end of the traversal;

Depth-first search for graphs

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: 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.