1. Graph structure represented by adjacency matrix
/* Graph structure represented by the adjacency matrix */#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath># Include <queue> #include <stack>using namespace std; typedef char Vertextype; The vertex type should be user-defined typedef int EDGETYPE; The weight type on the edge should be defined by the user #define MAXVEX 100//maximum number of vertices, which should be defined by user define # define INF 0//With a limit of Typ for the absence of the edge #define DEBUG Edef struct{Vertextype Vexs[maxvex]; Vertex table Edgetype Arc[maxvex][maxvex]; Adjacency matrix, can be regarded as edge int numvertexes, numedges; The current number of vertices and the number of sides of}graph in the graph; Position int locates (Graph *g, char ch) {int i = 0; for (i = 0; i < g->numvertexes; i++) {if (g->vexs[i] = = ch) {return i; }} if (I >= g->numvertexes) {return-1; }}//Establish an adjacency matrix of a non-creategraph graph to represent void (graph *g) {int I, J, K, W; printf ("Number of input vertices and number of edges: \ n"); scanf ("%d%d", & (G->numvertexes), & (G->numedges)); #ifdef DEBUG printf ("%d%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; Adjacency Matrix Initialization}} for (k = 0; k < g->numedges; k++) {char p, q; printf ("Subscript I on input edge (VI,VJ), 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]; Because it is an printgraph graph, the matrix is symmetric}}//print diagram void (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//adjacency matrix Depth-first recursive algorithm void DFS (Graph g, int i) {int J; Visited[i] = TRUE; printf ("%c", G.vexs[i]); Print vertices or other actions for (j = 0; J < G.numvertexes; J + +) {if (g.arc[i][j]!=0 &&!visited[j]) { DFS (G, J); The deep traversal operation of the adjacency vertex recursive call to the Access}}}//adjacency matrix void Dfstraverse (Graph g) {int i; for (i = 0; i < g.numvertexes; i++) {visited[i] = FALSE; Initialization of all vertex states is not an accessed state } for (i = 0; i < g.numvertexes; i++) {if (!visited[i])//DFS is called for an unreachable vertex, and if connected, it is executed only once { DFS (G,i); }} printf ("\ n");} Breadth traversal algorithm for adjacency matrices void Bfstraverse (Graph g) {int I, J; Queue<int> Q; for (i = 0; i < g.numvertexes; i++) {visited[i] = FALSE; } for (i = 0; i < g.numvertexes; i++)//loop for each vertex {if (!visited[i])//if not visited { Visited[i] = TRUE; printf ("%c", G.vexs[i]); Print nodes, or other operations Q.push (i); Put this node into the queue while (!q.empty ()) {int m = Q.front (); Q.pop (); for (j = 0; J < G.numvertexes; J + +) {//To determine if the other vertex has an edge with the current vertex and has not accessed the if (g.a RC[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; Adjacency matrix creation Diagram Creategraph (&g); Printgraph (g); Dfstraverse (g); Bfstraverse (g); return 0;}
2. Graph structure represented by adjacency table
/* Graph structure represented by the adjacency table */#include <cstdio> #include <cstdlib> #include <cstdlib> #include <cmath> #include <queue> #include <stack>using namespace std; #define MAXVEX 100//maximum vertex number bool Visited[maxvex]; Access Flag Array # define TRUE 1#define FALSE 0#define debug#define maxvex 1000//MAX vertex number typedef char Vertextype; The vertex type should be user-defined typedef int EDGETYPE; The weight type on the edge should be defined by the user's typedef struct EDGENODE//Benzi node {int Adjvex; adjacency Point field, storing the corresponding subscript edgetype weigth of the vertex; For storing weights, no struct edgenode *next is required for non-network graphs; Chain field, pointing to the next adjacency point}edgenode; typedef struct VERTEXNODE//Vertex table structure {vertextype data; Vertex field, storing vertex information edgenode *firstedge; Side table head pointer}vertexnode, Adjlist[maxvex]; typedef struct{Adjlist Adjlist; int numvertexes, numedges; The current number of vertices and the number of}graphlist in the graph; 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;} Building the adjacency table structure of the graph void Creategraph (graphlist *g) {int I, j, K; Edgenode *e; Edgenode *f; printf ("Number of input vertices and number of edges: \ n"); scanf ("%d%d", &g->numvertexes, &g->numedges); #ifdef DEBUG printf ("%d%d\n", g->numvertexes, g->numedges); #endif for (i = 0; i < g->numvertexes; i++) {printf ("Please enter vertex%d:\n", i); G->adjlist[i].data = GetChar (); Enter 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 (); }}//Establish edge table for (k = 0; k < g->numedges; k++) {printf ("Vertex number on input edge (VI,VJ): \ 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//To memory request space, generate edge table node E = (Edgenode *) malloc (sizeof (Edgenode)); if (e = = NULL) {fprintf (stderr, "malloc () error.\n"); Return }//Adjacency sequence number is J E->adjvex = n; Point the E pointer to the structure that the current vertex points to E->next = g->adjlist[m].firstedge; Points the current vertex pointer 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].firstedge = 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->ADJL Ist[i].data); Edgenode *e = NULL; E = g->adjlist[i].firstedge; while (E! = NULL) {printf ("%d", E->adjvex); E = e->next; } i++; printf ("\ n"); }}//adjacency table depth recursive algorithm void DFS (graphlist g, int i) {Edgenode *p; Visited[i] = TRUE; printf ("%c", g.adjlist[i].data); Print vertices, or other operations P = G.adjlist[i].firstedge; while (p) {if (!visited[p->adjvex]) {DFS (g, P->adjvex); Neighbor Vertex recursive call to access} p = p->next; }}//Deep traversal of adjacency table operation 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");} Breadth traversal algorithm for adjacency table void Bfstraverse (Graphlist g) { int i; Edgenode *p; Queue<int> 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 vertices, or other operations Q.push (i); while (!q.empty ()) {int m; m = Q.front (); Q.pop (); p = G.adjlist[m].firstedge; Locate the current Vertex edge table header pointer 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;}
Data structure-storage structure representation of graphs and their traversal (DFS && BFS)