// adjacency matrix of the depth and breadth-first traversal #include <stdio.h> #define the OK 1 // execute successfully #define ERROR 0 // execution failed #define TRUE 1 // returns true #define FALSE 0 // returns false typedef int the Status; // execution state (the OK, ERROR) typedef int Boolean; // Boolean value (TRUE, FALSE) typedef char VertexType; // vertex element type typedef int EdgeType; // the type of edge weights #define the MAXSIZE. 9 // initial storage allocation queue #define MAXVEX 100 // maximum number of vertices // adjacency matrix structure (undirected graph) typedef struct { Vexs VertexType [MAXVEX]; // vertex table EdgeType ARC [MAXVEX] [MAXVEX]; // edge list int NumNodes, numEdges; // FIG number of vertices, the number of edge } MGraph; / * ************* used queue structure and function ************* * / // the circular queue sequential storage structure typedef struct { int Data [the MAXSIZE]; // for an array of values stored int Front; // head pointer int ; REAR // the tail pointer, if the queue is not empty, pointing to the tail element a position } Queue; /** * Initialize an empty queue * @Param Q queue * @Return execution state */ Status InitQueue(Queue* Q) { Q -> = a Q-Front> REAR = 0 ; // queue head and queue tail pointers to point 0 return the OK; } /** * Whether the queue is empty * @Param Q queue * @Return queue is empty */ Boolean QueueEmpty(Queue Q) { IF (Q.front == Q.rear) { // HOL equal to the tail pointer, the queue is empty return TRUE; } else { return FALSE; } } /** * The element e inserted into the tail of the queue Q * @Param Q queue * @Param e inserted element * @Return execution state * / The Status ENQUEUE (Queue * Q, int E) { // queue is full, the insert fails IF ((a Q-> + REAR . 1 ) == a Q-% the MAXSIZE> Front) { return ERROR; } // the element e inserted into the tail a Q-> Data [a Q-> REAR] = e; // Set the tail pointer to the next position, if the steering head and finally a Q-> REAR = (a Q-> + REAR . 1 )% the MAXSIZE; return the OK; } /** * The head elements dequeued return its value e * @Param Q queue * @Param e the value of the head elements * @Return execution state * / The Status DEQUEUE (Queue * Q, int * E) { // head pointer is equal to the tail pointer, then the queue is empty, dequeuing failure IF (a Q-> Front a Q-==> REAR) { return ERROR; } // the value of the element assigned to the head elements E * a Q-E => Data [a Q-> Front]; // Set Team head pointer to the next position, if the steering head and finally a Q-> Front = (a Q-> Front + . 1 )% the MAXSIZE; return the OK; } /*************************************************/ /** * Generate adjacency matrix * @Param G adjacency matrix * / Void CreateMGraph (MGraph * G) { int I, J; // for traversing element G -> numEdges = 15 ; // disposed sides 15 G-> = NumNodes . 6 ; // provided nine vertices // read the vertex information, the establishment of the vertex table G-> vexs [ 0 ] = ' A ' ; G->vexs[1] = 'B'; G->vexs[2] = 'C'; G->vexs[3] = 'D'; G->vexs[4] = 'E'; G->vexs[5] = 'F'; // Initialization FIG edge for (I = 0 ; I <G-> NumNodes; I ++ ) { for (J = 0 ; J <G-> NumNodes; J ++ ) { G -> ARC [I] [J] = 0 ; // set all edges are values 0 } } // set a specific side (if the arc [i] [j] = 1, the representative vertex i to vertex j is connected with a side) G-> ARC [ 0 ] [ . 1 ] = . 1 ; G->arc[0][2] = 1; G->arc[0][4] = 1; G->arc[1][0] = 1; G->arc[1][3] = 1; G->arc[1][4] = 1; G->arc[2][0] = 1; G->arc[2][4] = 1; G->arc[2][5] = 1; G->arc[3][1] = 1; G->arc[3][4] = 1; G->arc[3][5] = 1; G->arc[4][0] = 1; G->arc[4][1] = 1; G->arc[4][2] = 1; G->arc[4][3] = 1; G->arc[4][5] = 1; G->arc[5][2] = 1; G->arc[5][3] = 1; G->arc[5][4] = 1; // set symmetrical sides for (I = 0 ; I <G-> NumNodes; I ++ ) { for (J = I; J <G-> NumNodes; J ++ ) { G->arc[j][i] = G->arc[i][j]; } } } // array access flag Boolean visited [MAXVEX]; /** * Depth-first recursive algorithm adjacency matrix * @Param G adjacency matrix * @Param i subscript vertices * / Void the DFS (MGraph G, int I) { int J; // for traversing element visited [I] = TRUE; // record the target element has been accessed under the printf ( " % C " , G.vexs [I ]); // print position of the vertex values // vertex traversal figures for (j = 0 ; j <G.numNodes; j ++ ) { // vertex i to vertex j has connected edges, and vertices j not been accessed IF (G.arc [i] [j ] == . 1 &&! visited [J]) { The DFS (G, j); // vertices j access } } } /** * Adjacency matrix of depth traversal * @Param G adjacency matrix * / Void DFSTraverse (MGraph G) { int I; // for traversing element // initialization setting all vertices have not been accessed for (I = 0 ; I <G.numNodes; I ++ ) { visited[i] = FALSE; } // iterate vertex i for (i = 0 ; i <G.numNodes; i ++ ) { // if the node i has not visited IF (! Visited [i]) { The DFS (G, I); // visits vertices I } } } /** * Adjacency matrix breadth traversal algorithm * @Param G adjacency matrix * / Void BFSTraverse (MGraph G) { int I, J; // for traversing element Queue Q; // queue // all the vertices of the initial settings have not been accessed Fig for (I = 0 ; I <G.numNodes; I ++ ) { visited[i] = FALSE; } InitQueue ( & Q); // initialize queue // for each vertex do loop for (I = 0 ; I <G.numNodes; I ++ ) { IF (! Visited [I]) { // the vertex has not been visited, the process visited [i] = TRUE; // set the vertex i has been accessed the printf ( " % C " , G.vexs [i]); // print the value of vertex i eNQUEUE ( & Q, i); // the vertex i enqueue // When the queue is not empty, circulates the while (! QueueEmpty (Q)) { DEQUEUE ( & Q, & i); // will dequeue the head elements, assigned to i // iterate current node other than node j for (j = 0 ; j <G.numNodes; j ++ ) { // if there is an edge to the current vertex node j, and has not been visited IF (G.arc [I] [j ] == . 1 &&! visited [J]) { visited [j] = TRUE; // Set j vertices have been visited the printf ( " % C " , G.vexs [j]); // value of the vertices j of the print ENQUEUE ( & Q, j); // vertices j into team } } } } } } int main () { G MGraph; // adjacency matrix CreateMGraph (& G); // create the adjacency matrix of the printf ( " Depth traversal: " ); DFSTraverse (G); // depth adjacency matrix traversing the printf ( " \ n-span traversal: " ); BFSTraverse (G); // breadth traversal adjacency matrix return 0; }