Data Structure-graph Storage Structure Representation and traversal (DFS & amp; BFS)

Source: Internet
Author: User
Tags cmath

Data Structure-graph Storage Structure Representation and traversal (DFS & amp; BFS)



1. Graph structure represented by the adjacent matrix

/* Graph structure represented by the adjacent matrix */# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; typedef char VertexType; // The vertex type should be defined by the user typedef int EdgeType; // The weight type on the edge should be defined by the user # define MAXVEX 100 // maximum number of vertices, user-Defined # define INF 0 // 0 indicates that this edge does not exist # define DEBUG typedef struct {VertexType vexs [MAXVEX]; // vertex table EdgeType arc [MAXVEX] [MAXVEX]; // The adjacent matrix, which can be seen as an edge int numVertexes or numEdges; // The number of vertices and edges in the Graph} Graph; // locate int locates (Graph * g, char ch) {int I = 0; for (I = 0; I <g-> numVertexes; I ++) {if (g-> vex S [I] = ch) {return I ;}if (I >=g-> numVertexes) {return-1 ;}} // create an undirected network Graph's Adjacent matrix to represent void CreateGraph (Graph * g) {int I, j, k, w; printf ("Number of input vertices and edges: \ n "); scanf (" % d ", & (g-> numVertexes), & (g-> numEdges )); # ifdef DEBUG printf ("% d \ n", g-> numVertexes, g-> numEdges); # endifprintf ("input vertex: \ n "); for (I = 0; I <g-> numVertexes; I ++) {g-> vexs [I] = getchar (); while (g-> vexs [I] = '\ n') {g-> vexs [I] = getchar () ;}# Ifdef DEBUG for (I = 0; I <g-> numVertexes; I ++) {printf ("% c ", g-> vexs [I]);} printf ("\ n"); # endif for (I = 0; I <g-> numVertexes; I ++) {for (j = 0; j <g-> numVertexes; j ++) {g-> arc [I] [j] = INF; // adjacent matrix initialization }}for (k = 0; k <g-> numEdges; k ++) {char p, q; printf ("input edge (vi, vj) subscript I, subscript j and weight: \ n "); p = getchar (); while (p = '\ n') {p = getchar ();} q = getchar (); while (q = '\ n') {q = getchar ();} Scanf ("% d", & w); int m =-1; int n =-1; m = locates (g, p); n = locates (g, q); if (n =-1 | m =-1) {fprintf (stderr, "there is no this vertex. \ n "); return;} // getchar (); g-> arc [m] [n] = w; g-> arc [n] [m] = g-> arc [m] [n]; // It is an undirected graph, matrix symmetry} // print the figure void printGraph (Graph g) {int I, j; for (I = 0; I <g. numVertexes; I ++) {for (j = 0; j <g. numVertexes; j ++) {printf ("% 6d", g. arc [I] [j]);} printf ("\ n "); }}# Define MAXVEX 100 // maximum number of vertices bool visited [MAXVEX]; // access flag array # define TRUE 1 # define FALSE 0 // depth-first recursive algorithm of the adjacent matrix void DFS (Graph g, int I) {int j; visited [I] = TRUE; printf ("% c", g. vexs [I]); // print the vertex. You can also perform other operations for (j = 0; j <g. numVertexes; j ++) {if (g. arc [I] [j]! = 0 &&! Visited [j]) {DFS (g, j); // recursive call to the accessed adjacent vertex} // void DFSTraverse (Graph g) {int I; for (I = 0; I <g. numVertexes; I ++) {visited [I] = FALSE; // Initialize all vertices in a State that has not been accessed} for (I = 0; I <g. numVertexes; I ++) {if (! Visited [I]) // call DFS for unaccessed vertices. If it is a connected graph, only {DFS (g, I) ;}} is executed once );}} printf ("\ n");} // The breadth traversal algorithm of the adjacent matrix void BFSTraverse (Graph g) {int I, j; queue
       
         Q; for (I = 0; I <g. numVertexes; I ++) {visited [I] = FALSE;} for (I = 0; I <g. numVertexes; I ++) // loops each vertex {if (! Visited [I]) // If {visited [I] = TRUE; printf ("% c", g. vexs [I]); // print the node. You can also perform other operations. push (I); // put this node into the queue while (! Q. empty () {int m = q. front (); q. pop (); for (j = 0; j <g. numVertexes; j ++) {// determines if other vertices have an edge with the current vertex and have not accessed if (g. arc [m] [j]! = 0 &&! Visited [j]) {visited [j] = TRUE; printf ("% c", g. vexs [j]); q. push (j) ;}}}} printf ("\ n");} int main (int argc, char ** argv) {Graph g; // CreateGraph (& g); printGraph (g); DFSTraverse (g); BFSTraverse (g); return 0 ;}
       
      
     
    
   
  
 



2. Graph structure represented by the adjacent table

/* Graph structure represented by the adjacent table */# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; # define MAXVEX 100 // maximum number of vertices bool visited [MAXVEX]; // access flag array # define TRUE 1 # define FALSE 0 # define DEBUG # define MAXVEX 1000 // maximum number of vertices typedef char VertexType; // The vertex type should be defined by the user-defined typedef int EdgeType; // The weight type on the edge should be defined by the user-defined typedef struct EdgeNode // edge table node {int adjvex; // The adjacent vertex field, store the corresponding subscript EdgeType weigth of the vertex; // used to store the weight value. For non-net graphs, you do not need struct EdgeNode * next; // link the domain to the next adjacent vertex} EdgeNode; typedef struct VertexNode // vertex Table Structure {V ErtexType data; // vertex field, storing vertex information EdgeNode * firstedge; // edge header pointer} VertexNode, AdjList [MAXVEX]; typedef struct {AdjList adjList; int numVertexes, numEdges; // Number of vertices and edges} GraphList; int Locate (GraphList * g, char ch) {int I; for (I = 0; I <MAXVEX; I ++) {if (ch = g-> adjList [I]. data) {break ;}}if (I >= MAXVEX) {fprintf (stderr, "there is no vertex. \ n "); return-1;} return I;} // create the graph's adjacent table structure void CreateGraph (Grap HList * g) {int I, j, k; EdgeNode * e; EdgeNode * f; printf ("Number of input vertices and edges: \ n "); scanf ("% d", & g-> numVertexes, & g-> numEdges); # ifdef DEBUG printf ("% d \ n ", g-> numVertexes, g-> numEdges); # endif for (I = 0; I <g-> numVertexes; I ++) {printf ("Enter the vertex % d: \ n ", I); g-> adjList [I]. data = getchar (); // enter the vertex information g-> adjList [I]. firstedge = NULL; // set the edge table to an empty table while (g-> adjList [I]. data = '\ n') {g-> adjList [I]. data = getchar () ;}}// Create an edge table for (k = 0; k <g-> numEdges; k ++) {printf ("input edge (vi, vj) vertex number on: \ n "); char p, q; p = getchar (); while (p = '\ n') {p = getchar ();} q = getchar (); while (q = '\ n') {q = getchar ();} int m, n; m = Locate (g, p ); n = Locate (g, q); if (m =-1 | n =-1) {return ;} # ifdef DEBUG printf ("p = % c \ n", p); printf ("q = % c \ n", q ); printf ("m = % d \ n", m); printf ("n = % d \ n", n); # endif // apply for a space from the memory, generate edge table knot Point e = (EdgeNode *) malloc (sizeof (EdgeNode); if (e = NULL) {fprintf (stderr, "malloc () error. \ n "); return;} // The adjacent serial number is j e-> adjvex = n; // point the e pointer to the structure pointed to by the current vertex e-> next = g-> adjList [m]. firstedge; // point the pointer of the current vertex to e g-> adjList [m]. firstedge = e; f = (EdgeNode *) malloc (sizeof (EdgeNode); if (f = NULL) {fprintf (stderr, "malloc () error. \ n "); return;} f-> adjvex = m; f-> next = g-> adjList [n]. firstedge; g-> adjList [n]. fi Rstedge = f ;}} void printGraph (GraphList * g) {int I = 0; # ifdef DEBUG printf ("printGraph () start. \ n "); # endif while (g-> adjList [I]. firstedge! = NULL & I <MAXVEX) {printf ("vertex: % c", g-> adjList [I]. data); EdgeNode * e = NULL; e = g-> adjList [I]. firstedge; while (e! = NULL) {printf ("% d", e-> adjvex); e = e-> next;} I ++; printf ("\ n ");}} // deep recursive algorithm void DFS (GraphList g, int I) {EdgeNode * p; visited [I] = TRUE; printf ("% c", g. adjList [I]. data); // print the vertex. Other operations can be performed, such as p = g. adjList [I]. firstedge; while (p) {if (! Visited [p-> adjvex]) {DFS (g, p-> adjvex); // recursive call to the accessed adjacent vertex} p = p-> next ;}} // void DFSTraverse (GraphList g) {int I; for (I = 0; I <g. numVertexes; I ++) {visited [I] = FALSE;} for (I = 0; I <g. numVertexes; I ++) {if (! Visited [I]) {DFS (g, I) ;}} printf ("\ n");} // void BFSTraverse (GraphList g) {int I; EdgeNode * p; queue
       
         Q; for (I = 0; I <g. numVertexes; I ++) {visited [I] = FALSE;} for (I = 0; I <g. numVertexes; I ++) {if (! Visited [I]) {visited [I] = TRUE; printf ("% c", g. adjList [I]. data); // print the vertex. You can also perform other operations on q. push (I); while (! Q. empty () {int m; m = q. front (); q. pop (); p = g. adjList [m]. firstedge; // locate the table link header pointer of the current vertex edge while (p) {if (! Visited [p-> adjvex]) {visited [p-> adjvex] = TRUE; printf ("% c", g. adjList [p-> adjvex]. data); q. push (p-> adjvex);} p = p-> next ;}}} printf ("\ n");} int main (int argc, char ** argv) {GraphList g; CreateGraph (& g); printGraph (& g); DFSTraverse (g); BFSTraverse (g); return 0 ;}
       
      
     
    
   
  
 


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.