Data structure (15): Graph Depth-first traversal (DFS)

Source: Internet
Author: User

/*-----------------------------------------------*// * DFS of adjacency matrix * ///Structure of adjacency matrix based on data structure (14)#include <iostream>using namespace STD;typedef CharVertextype;typedef intEdgetype;Const intMaxvex = -;Const intINFINITY =65535;typedef struct{Vertextype Vexs[maxvex]; Edgetype Arc[maxvex][maxvex];intNumvertexes, Numedges;} Mgraph;voidCreatemgraph (Mgraph &g) {intI,j,k,w;cout<<input vertex count and number of edges:;Cin>>G.numVertexes>>G.numEdges;cout<<"Enter vertex information:"<<endl; for(i=0; i<g.numvertexes;i++)Cin>>G.vexs[i]; for(i=0; i<g.numvertexes;i++) for(j=0; j<g.numvertexes;j++) G.arc[i][j] = INFINITY; for(k=0; k<g.numedges;k++) {cout<<"Subscript I on input edge (VI,VJ), Subscript J, and Right W:"<<endl;Cin>>i>>j>>w;        G.ARC[I][J] = W;    G.arc[j][i] = G.arc[i][j]; }}/*-----------------------------------------------*///DFSBOOLVisit[maxvex];//To mark whether the vertex has been traversedvoidDFS (Mgraph G,intI//DFS search{intJ Visit[i] =true;//The current vertex is marked as traversed    cout<<G.vexs[i]<<ends;//Output vertex information (can be changed to other operations)     for(j=0; j<g.numvertexes;j++)//Traverse other vertices that are associated with the current vertex        if(G.arc[i][j] = =1&&!visit[j]) DFS (G,J);//Recursive}//DfstraversevoidDfstraverse (Mgraph G) {intI for(i=0; i<g.numvertexes;i++)//Initialize tag array as not traversedVisit[i] =false; for(i=0; i<g.numvertexes;i++)//Dfs for each vertex that has not been traversed        if(!visit[i]) DFS (g,i);}intMain () {mgraph G;    Createmgraph (G); Dfstraverse (G);return 0;}
/*-----------------------------------------------*// * DFS for adjacency table * ///Structure of adjacency table based on data structure (14)#include <iostream>using namespace STD;Const intMaxvex = -;typedef CharVertextype;typedef intEdgetype;typedef structedgenode{intAdjvex; Edgetype weight;structEdgenode *next; } Edgenode;typedef structvertexnode{Vertextype data; Edgenode *firstedge;} Vertexnode, Adjlist[maxvex];typedef struct{Adjlist adjlist;intNumvertexes, Numedges;} Graphadjlist;voidCreatealgraph (Graphadjlist &g) {intI,j,k,w; Edgenode *e;cout<<input vertex count and number of edges:;Cin>>G.numVertexes>>G.numEdges;cout<<"Enter information for each vertex:"<<endl; for(i=0; i<g.numvertexes;i++) {Cin>>G.adjList[i].data;    G.adjlist[i].firstedge = NULL; } for(k=0; k<g.numedges;k++) {cout<<The vertex number on the input edge (VI,VJ):<<endl;Cin>>i>>j>>w; E =NewEdgenode;        E->adjvex = j;        E->weight = W;        E->next = G.adjlist[i].firstedge;        G.adjlist[i].firstedge = e;        E->adjvex = i;        E->weight = W;        E->next = G.adjlist[j].firstedge;    G.adjlist[j].firstedge = e; }}/*-----------------------------------------------*///DFSBOOLVisit[maxvex];//To mark whether the vertex has been traversedvoidDFS (Graphadjlist GL,intI//DFS search{Edgenode *p; Visit[i] =true;//The current vertex is marked as traversed    cout<<GL.adjList[i].data<<ends;//Output vertex information (can be changed to other operations)p = Gl.adjlist[i].firstedge;//points to sub-nodes pointed to by the current adjacency vertex     while(p) {if(!visit[p->adjvex])//Traverse other vertices that are associated with the current adjacency vertexDFS (Gl,p->adjvex); p = p->next;//Continue traversing the next sub-node.}}voidDfstraverse (Graphadjlist GL) {intI for(i=0; i<gl.numvertexes;i++)//Initialize tag array as not traversedVisit[i] =false; for(i=0; i<gl.numvertexes;i++)//Dfs for each vertex that has not been traversed        if(!visit[i]) DFS (gl,i);}intMain () {graphadjlist G;    Createalgraph (G); Dfstraverse (G);return 0;}

Data structure (15): Graph Depth-first traversal (DFS)

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.