数据结构15-图的遍历
代码
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
// 定义队列的大小
#define QUEUE_SIZE 10
int* visitedPtr;
// 定义一个结构体来表示图节点队列
typedef struct GraphNodeQueue {
int* nodes; // 用于存储节点的数组
int front; // 队列的前端指针
int rear; // 队列的后端指针
} GraphNodeQueue, *QueuePtr;
// 初始化队列
QueuePtr initQueue() {
QueuePtr resultQueuePtr = (QueuePtr)malloc(sizeof(struct GraphNodeQueue));
resultQueuePtr->nodes = (int*)malloc(QUEUE_SIZE * sizeof(int));
resultQueuePtr->front = 0;
resultQueuePtr->rear = 1;
return resultQueuePtr;
}
// 判断队列是否为空
bool isQueueEmpty(QueuePtr paraQueuePtr) {
if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) {
return true;
}
return false;
}
// 入队操作
void enqueue(QueuePtr paraQueuePtr, int paraNode) {
if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE) {
printf("Error, trying to enqueue %d. queue full.\r\n", paraNode);
return;
}
paraQueuePtr->nodes[paraQueuePtr->rear] = paraNode;
paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
}
// 出队操作
int dequeue(QueuePtr paraQueuePtr) {
if (isQueueEmpty(paraQueuePtr)) {
printf("Error, empty queue\r\n");
return -1;
}
paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
return paraQueuePtr->nodes[paraQueuePtr->front];
}
// 定义一个结构体来表示图
typedef struct Graph {
int** connections; // 用于存储图的邻接矩阵
int numNodes; // 图中节点的数量
} *GraphPtr;
// 初始化图
GraphPtr initGraph(int paraSize, int** paraData) {
int i, j;
GraphPtr resultPtr = (GraphPtr)malloc(sizeof(struct Graph));
resultPtr->numNodes = paraSize;
resultPtr->connections = (int**)malloc(paraSize * sizeof(int*));
for (i = 0; i < paraSize; i++) {
resultPtr->connections[i] = (int*)malloc(paraSize * sizeof(int));
for (j = 0; j < paraSize; j++) {
resultPtr->connections[i][j] = paraData[i][j];
}
}
return resultPtr;
}
// 初始化遍历辅助数组
void initTranverse(GraphPtr paraGraphPtr) {
int i;
visitedPtr = (int*)malloc(paraGraphPtr->numNodes * sizeof(int));
for (i = 0; i < paraGraphPtr->numNodes; i++) {
visitedPtr[i] = 0;
}
}
// 深度优先遍历
void depthFirstTranverse(GraphPtr paraGraphPtr, int paraNode) {
int i;
visitedPtr[paraNode] = 1;
printf("%d\t", paraNode);
for (i = 0; i < paraGraphPtr->numNodes; i++) {
if (!visitedPtr[i]) {
if (paraGraphPtr->connections[paraNode][i]) {
depthFirstTranverse(paraGraphPtr, i);
}
}
}
}
// 广度优先遍历
void widthFirstTranverse(GraphPtr paraGraphPtr, int paraStart) {
int i, j, tempNode;
i = 0;
QueuePtr tempQueuePtr = initQueue();
printf("%d\t", paraStart);
visitedPtr[paraStart] = 1;
enqueue(tempQueuePtr, paraStart);
while (!isQueueEmpty(tempQueuePtr)) {
tempNode = dequeue(tempQueuePtr);
visitedPtr[tempNode] = 1;
i++;
for (j = 0; j < paraGraphPtr->numNodes; j++) {
if (visitedPtr[j])
continue;
if (paraGraphPtr->connections[tempNode][j] == 0)
continue;
printf("%d\t", j);
visitedPtr[j] = 1;
enqueue(tempQueuePtr, j);
}
}
}
// 测试图的遍历
void testGraphTranverse() {
int i, j;
int myGraph[5][5] = {
{0, 1, 0, 1, 0},
{1, 0, 1, 0, 1},
{0, 1, 0, 1, 1},
{1, 0, 1, 0, 0},
{0, 1, 1, 0, 0}
};
int** tempPtr;
printf("Preparing data\r\n");
tempPtr = (int**)malloc(5 * sizeof(int*));
for (i = 0; i < 5; i++) {
tempPtr[i] = (int*)malloc(5 * sizeof(int));
}
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
tempPtr[i][j] = myGraph[i][j];
}
}
printf("Data ready\r\n");
GraphPtr tempGraphPtr = initGraph(5, tempPtr);
printf("num nodes = %d \r\n", tempGraphPtr->numNodes);
printf("Graph initialized\r\n");
printf("Depth first visit:\r\n");
initTranverse(tempGraphPtr);
depthFirstTranverse(tempGraphPtr, 4);
printf("\r\nWidth first visit:\r\n");
initTranverse(tempGraphPtr);
widthFirstTranverse(tempGraphPtr, 4);
}
int main() {
testGraphTranverse();
return 1;
}
总结
这个代码实现了一个图的遍历程序,包括以下主要部分:
-
图节点队列:
GraphNodeQueue
结构体用于定义一个环形队列,支持基本的入队和出队操作。initQueue
函数用于初始化一个队列。isQueueEmpty
函数用于检查队列是否为空。enqueue
和dequeue
函数分别实现了入队和出队的操作。
-
图的表示和初始化:
Graph
结构体用于表示一个图,包括一个邻接矩阵和节点数量。initGraph
函数用于根据给定的邻接矩阵初始化图结构。
-
遍历辅助功能:
initTranverse
函数初始化辅助数组visitedPtr
,用于记录节点是否被访问过。depthFirstTranverse
函数实现深度优先遍历,通过递归方式访问所有连接的节点。widthFirstTranverse
函数实现广度优先遍历,通过队列实现逐层访问节点。
-
测试功能:
testGraphTranverse
函数定义了一个5节点的图并测试了深度优先遍历和广度优先遍历,打印出访问的节点顺序。
这个代码展示了如何使用基本的队列和递归来实现图的遍历,同时提供了详细的步骤和函数调用,使得整个图的遍历过程清晰明了。
运行结果
Graph initialized
Depth first visit:
4 1 0 3 2
Width first visit:
4 1 2 0 3
...Program finished with exit code 1
Press ENTER to exit console.