This article is used to learn graph traversal (depth-first traversal, breadth-first traversal)!!!
#include <iostream> using namespace std; typedef char VertexType; typedef int EdgeType; const int MAXSIZE = 100; bool visited[MAXSIZE]; typedef int QElemType; typedef struct { QElemType data[MAXSIZE]; int front;//head pointer int rear;//tail pointer }SqQueue;//Define circular queue order storage type void InitQueue(SqQueue &q)//Queue initialization { q.front = 0; q.rear = 0; } void EnQueue(SqQueue &q, QElemType &e)//Enter the queue, insert the element into the end of the circular queue { q.data[q.rear] = e; q.rear = (q.rear + 1) % MAXSIZE; } void DeQueue(SqQueue &q, QElemType &e)//Out of the queue, the head pointer is moved back { e = q.front; q.front = (q.front) % MAXSIZE; } bool QueueEmpty(SqQueue &q)//Determine whether the queue is empty, the head pointer is equal to the tail pointer { if (q.front == q.rear) return true; } typedef struct//Define the adjacency matrix type { int numVertexes, numEdges; VertexType vertex[MAXSIZE];//Vertex array EdgeType arc[MAXSIZE][MAXSIZE];//Edge array }MGraph; void createMGraph(MGraph *G)//Create an adjacency matrix to store an undirected graph { cout << "Enter the number of vertices and edges: "; cin >> G->numVertexes >> G->numEdges; cout << "Enter vertex information: "; for (int i = 0; i < G->numVertexes; i++) cin >> G->vertex[i]; for (int i = 0; i < G->numVertexes; i++)//Adjacency list initialization for (int j = 0; j < G->numVertexes; j++) G->arc[i][j] = false; cout << "input edge (subscript, subscript):" << endl; for (int i = 0; i < G->numEdges; i++) { int a, b; cin >> a >> b; G->arc[a][b] = true; G->arc[b][a] = G->arc[a][b];//The undirected graph is a symmetric matrix } } void DFS(MGraph *g, int i) { visited[i] = true; cout << "Visit vertex:" << g->vertex[i] << endl; for (int j = 0; j < g->numVertexes; j++) { if (g->arc[i][j] == 1 && !visited[i])//traverse each line DFS(g, j);//Unvisited vertex recursive operation } } void DFSTraverse(MGraph *g)//Depth-first traversal, similar to tree-like preorder traversal { for (int i = 0; i < g->numVertexes; i++)//Access flag array initialization visited[i] = false; for (int i = 0; i < g->numVertexes; i++) { if (!visited[i]) DFS(g, i); cout << "test!!!" << endl; } } void BFSTraverse(MGraph *g)//Breadth-first traversal, similar to tree-level order traversal { int i, j; SqQueue q = {}; InitQueue(q); for (i = 0; i < g->numVertexes; i++)//Initialize the access flag array visited[i] = false; for (i = 0; i < g->numVertexes; i++)//traverse each vertex { if (!visited[i]) { visited[i] = true; cout << "Currently visited vertex:" << g->vertex[i] << endl; EnQueue(q, i);//This vertex enters the queue while (!QueueEmpty(q)) { DeQueue(q, i);//The first element of the queue is dequeued for (j = 0; j < g->numVertexes; j++)//traverse all vertices connected to the dequeue element { if (g->arc[i][j] == 1 && !visited[j]) { visited[j] = true; cout << "Currently visited vertex:" << g->vertex[i] << endl; EnQueue(q, j);//This vertex enters the queue, and the elements in the queue have been visited } } } } } } typedef struct EdgeNode//Define the type of edge table node { EdgeType weight;//权重 int adjVex;//adjacent node struct EdgeNode *next;//point to the next node }EdgeNode; typedef struct VertexNode//Define the vertex node type { VertexType data;//Vertex information EdgeNode *firstEdge;//Point to the first node of the edge table }VertexNode,AdjList[MAXSIZE]; typedef struct//Define the type of the adjacency list { int numVertexes, numEdges; AdjList adjList; }GraphAdjList;//Create structure variables directly void createGraphAdjList(GraphAdjList *G)//Create an adjacency list to store an undirected graph { cout << "Enter the number of vertices and edges: "; cin >> G->numVertexes >> G->numEdges; cout << "Enter vertex information: "; for (int i = 0; i < G->numVertexes; i++)//Store vertex table node information { cin >> G->adjList[i].data; G->adjList[i].firstEdge = NULL; } EdgeNode *e; for (int k = 0; k < G->numEdges; k++)//Head insertion method, store edge table node information { int i, j; cout << "input edge (subscript, subscript):" << endl; cin >> i >> j; e = (EdgeNode *)malloc(sizeof(EdgeNode)); e->adjVex = j;//Store adjacent point j e->next = G->adjList[i].firstEdge;//Insert the e node before the head node pointed to by the vertex table G->adjList[i].firstEdge = e;//Point the vertex table node to the e node e = (EdgeNode *)malloc(sizeof(EdgeNode));//Undirected graph, reverse operation e->adjVex = i; e->next = G->adjList[j].firstEdge; G->adjList[j].firstEdge = e; } } void DFS(GraphAdjList *gl, int i) { EdgeNode *p; visited[i] = true; cout << "The currently visited node:" << gl->adjList[i].data << endl; p = gl->adjList[i].firstEdge;//Get the adjacent edge list node while (p) { if (!visited[p->adjVex]) DFS(gl, p->adjVex);//Unvisited vertex recursive operation p = p->next; } } void DFSTraverse(GraphAdjList *gl) { for (int i = 0; i < gl->numVertexes; i++) visited[i] = false; for (int i = 0; i < gl->numVertexes; i++) { if (!visited[i])//The connected graph only calls the DFS function once DFS (gl, i); cout << "test!!!" << endl; } } void BFSTraverse(GraphAdjList *gl) { int i; for (i = 0; i < gl->numVertexes; gl++) visited[i] = false; SqQueue q = {}; InitQueue(q); EdgeNode *p; for (i = 0; i < gl->numVertexes; gl++) { if (!visited[i]) { visited[i] = true; cout << "Currently visited vertex:" << gl->adjList[i].data << endl; EnQueue(q, i); if (!QueueEmpty(q)) { DeQueue(q, i); p = gl->adjList[i].firstEdge; while (p) { if (!visited[p->adjVex]) { visited[p->adjVex] = true; cout << "Currently visited vertices:" << gl->adjList[p->adjVex].data << endl; EnQueue(q, p->adjVex); } p = p->next; } } } } } int main(int *argc, int **argv) { MGraph G = {}; MGraph *g; g = &G; createMGraph(g); for (int i = 0; i < g->numVertexes; i++) { for (int j = 0; j < g->numVertexes; j++) { cout << g->arc[i][j] << " ";//Print adjacency list information } cout << endl; } DFSTraverse(g); BFSTraverse(g); GraphAdjList GL = {}; GraphAdjList *gl; gl = &GL; createGraphAdjList(gl); EdgeNode *p; for (int i = 0; i < gl->numVertexes; i++) { cout << "顶点:" << gl->adjList[i].data << " "; p = gl->adjList[i].firstEdge; while (p) { cout << "边表结点:" << p->adjVex << " "; p = p->next; } cout << endl; } DFSTraverse (gl); BFSTraverse (gl); return 0; }
References:
1. "Dahua Data Structure"