/////////////////////////////////////////////////////////////////adjacency table notation for graphs and DFS and BFS///////////////////////////////////////////////////////////////#include <iostream>#include<stdlib.h>#include<queue>using namespacestd;//adjacency table notation for graphs#defineMaxvertexnum 100enumGRAPHTYPE{DG, UG, DN, UN};//Forward Graph, non-direction graph, mesh graph, non-meshtypedefstructnode{intADJV;//adjacency Point Field structNode *next;//pointer field pointing to next adjacency point//to represent the weight information on the edge, you should add a data field weight}edgenode;//Edge Table NodetypedefCharVertextype;//vertices are represented by characterstypedefstructvnode{vertextype Vertex;//Vertex FieldsEdgenode *firstedge;//Edge table Header pointer}vertexnode;typedef Vertexnode Adjlist[maxvertexnum]; //adjlist is an adjacency table typetypedefstruct{adjlist adjlist;//adjacency Table intN, E;//number of vertices and number of edges enumGraphtype GType;//type of diagram}algraph;//Algraph is a graph type that is stored in adjacency table modevoidCreatealgraph (Algraph *G) { intI, j, K; Edgenode*Edge; G->gtype = UG;//a map of the directioncout <<"Please enter the number of vertices and the number of edges (input format: Top count, number of sides): \ n"<<Endl; CIN>> g->n >> g->e;//number of vertices and edges read incout <<"Please enter the vertex information (input format: vertex number <CR>): \ n"; for(i =0; I < g->n; i++) {//Create a table of vertices with n verticesCIN >> &g->adjlist[i]. Vertex;//read-in vertex informationG->adjlist[i]. Firstedge = NULL;//the edge header pointer of the vertex is set to null} cout<<"Please enter the information for the side (input format is:i,j<cr>): \ n"; for(k =0; K < g->e; k++)//Create a side table{cin>> I >>J; Edge=NewEdgenode; Edge->ADJV =J; Edge->next = g->Adjlist[i]. Firstedge; //Insert new Edge table node edge into Benzi head of Vertex VIG->adjlist[i]. Firstedge =Edge; //In the case of an <vj graph, a node is created to represent the edge of the vi>Edgenode *edge2 =NewEdgenode; Edge2->ADJV =i; Edge2->next = g->Adjlist[j]. Firstedge; G->ADJLIST[J]. Firstedge =Edge2; }}//diagram--dfs for adjacency table storage//visited[] is a global variable that has been initialized to falseBOOLVisited[maxvertexnum] = {false };voidDFS (Algraph *g,inti) { //using VI as the starting point for the adjacency table storage of Figure G for DFS searchEdgenode *v; cout<<"visit vertex:"<< G->adjlist[i]. Vertex <<" "; //equivalent to access vertex VIVisited[i] =true; for(W = G->adjlist[i]. Firstedge; W W = w->Next)if(! Visited[w->ADJV]) DFS (G, W-adjv);}//diagram--dfs for adjacency table storage//visited[] is a global variable that has been initialized to falsevoidBFS (Algraph *g,inti) {Queue<VertexNode>Q; Q.push (G-Adjlist[i]); Visited[i]=true;//sequentially into the queue, each time the next node to be accessed is removed while(!Q.empty ()) {Vertexnode tmp=Q.front (); cout<< tmp. Vertex <<" "; Q.pop (); Edgenode*p =tmp. Firstedge; while(P! =NULL) { if(VISITED[P->ADJV]! =true) {Q.push (G->adjlist[p->ADJV]); Visited[p->ADJV] =true; } P= p->Next; } }}voidPrint (Algraph *G) { for(inti =0; I < g->n; i++) {Edgenode*p = g->Adjlist[i]. Firstedge; cout<< G->adjlist[i]. Vertex <<" "; while(P! =NULL) {cout<< P->ADJV <<" "; P= p->Next; } cout<<Endl; }}intMain () {algraph*g =Newalgraph; Createalgraph (G); Print (G); //DFS (G, 0);BFS (G,0); System ("Pause"); return 0;}
Test results:
Data Structures (11)--DFS and BFS for adjacency table storage graphs