Breadth-first traversal and depth-first traversal of graphs

Source: Internet
Author: User

Graph is a very important data structure, which is widely used in our programming life.

#include <iostream>using namespacestd;#defineINFINITY 32767#defineMax_vex 20//maximum number of vertices#defineQueue_size (max_vex+1)//Queue LengthBOOL*visited;//array of Access flags//the adjacency matrix storage structure of graphstypedefstruct{    Char*vexs;//vertex Vector    intArcs[max_vex][max_vex];//adjacency Matrix    intVexnum,arcnum;//the current number of vertices and arcs of the graph}graph;//Queue classclassqueue{ Public:    voidInitqueue () {Base=(int*)malloc(queue_size*sizeof(int)); Front=rear=0; }    voidEnQueue (inte) {Base[rear]=e; Rear= (rear+1)%queue_size; }    voidDeQueue (int&e) {e=Base[Front]; Front= (front+1)%queue_size; } Public:    int*Base; intFront; intrear;};//find the position of element C in Figure GintLocate (Graph G,Charc) {     for(intI=0; i<g.vexnum;i++)        if(g.vexs[i]==c)returni; return-1;}//Create a network without a directionvoidCreateudn (Graph &G) {    inti,j,w,s1,s2; Chara,b,temp; cout<<"Enter the number of vertices and number of arcs:"; CIN>>G.vexnum>>G.arcnum; G.vexs=(Char*)malloc(g.vexnum*sizeof(Char));//number of assigned verticescout<<"input"<<G.vexnum<<"a vertex."<<Endl;  for(i=0; i<g.vexnum;i++)    {         //initializing verticescout<<"input vertices"<<i+1<<":"; CIN>>G.vexs[i]; }     for(i=0; i<g.vexnum;i++)//initializing adjacency matrices         for(j=0; j<g.vexnum;j++) G.arcs[i][j]=INFINITY; cout<<"input"<<G.arcnum<<"arc."<<Endl;  for(i=0; i<g.arcnum;i++)    {         //Initialize Arccout<<"Input Arc"<<i+1<<":"; CIN>>a>>b>>w;//enter vertices and weights attached to an edges1=Locate (g,a); S2=Locate (G,B); G.ARCS[S1][S2]=g.arcs[s2][s1]=W; }}//the first adjacency vertex of vertex k in Figure GintFirstvex (Graph G,intk) {    if(k>=0&& k<g.vexnum) {//K Reasonable         for(intI=0; i<g.vexnum;i++)            if(g.arcs[k][i]!=INFINITY)returni; }    return-1;}//The next adjacency vertex of the first J adjacency Vertex of vertex i in Figure gintNextvex (Graph G,intIintj) {    if(i>=0&& I<g.vexnum && j>=0&& j<g.vexnum) {//I,j Reasonable         for(intk=j+1; k<g.vexnum;k++)            if(g.arcs[i][k]!=INFINITY)returnK; }    return-1;}//Depth-First traversalvoidDFS (Graph G,intk) {    inti; if(k==-1)    {         //The first time Dfs is executed, K is-1         for(i=0; i<g.vexnum;i++)            if(!Visited[i]) DFS (G,i); //call Dfs on a vertex that has not been accessed    }    Else{Visited[k]=true; cout<<G.vexs[k];//Access to the K-vertex         for(I=firstvex (g,k); i>=0; i=Nextvex (g,k,i))if(!Visited[i]) DFS (G,i); //The unreachable adjacency vertex i for K recursively calls Dfs    }}//breadth-First traversalvoidBFS (Graph G) {intK; Queue Q; //Auxiliary Queue QQ.initqueue ();  for(intI=0; i<g.vexnum;i++)        if(!Visited[i]) {             //I have not yet visitedvisited[i]=true; cout<<G.vexs[i]; Q.enqueue (i); //I into row             while(q.front!=q.rear) {q.dequeue (k);//team head element is dequeue and set to K                 for(intW=firstvex (g,k); w>=0; w=Nextvex (g,k,w))if(!Visited[w]) {                         //The unreachable adjacency vertex of W is Kvisited[w]=true; cout<<G.vexs[w];                    Q.enqueue (w); }            }        }}//Main functionintMain () {inti;    Graph G;    CREATEUDN (G); Visited=(BOOL*)malloc(g.vexnum*sizeof(BOOL)); cout<<"Depth-First traversal:";  for(i=0; i<g.vexnum;i++) Visited[i]=false; DFS (G,-1); cout<<"\ n Breadth-first traversal:";  for(i=0; i<g.vexnum;i++) Visited[i]=false;    BFS (G); System ("Pause"); return 0;}

Breadth-first traversal and depth-first traversal of 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: 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.