用c语言编写程序且用邻接矩阵或邻接表创建图然后进行DFS和BFS操作。
时间: 2024-05-01 17:23:49 浏览: 175
好的,以下是用邻接矩阵和邻接表创建图并进行DFS和BFS操作的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
// 邻接矩阵结构体
typedef struct {
int vertex[MAX_VERTEX_NUM]; // 存储顶点值
int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存储边的权值
int vertex_num; // 顶点数
int edge_num; // 边数
} AdjacencyMatrix;
// 邻接表结构体
typedef struct EdgeNode {
int adjvex; // 邻接点下标
int weight; // 权值
struct EdgeNode *next; // 指向下一个邻接点的指针
} EdgeNode;
typedef struct {
int vertex; // 顶点值
EdgeNode *first_edge; // 指向第一个邻接点的指针
} VertexNode;
typedef struct {
VertexNode adj_list[MAX_VERTEX_NUM]; // 顶点数组
int vertex_num; // 顶点数
int edge_num; // 边数
} AdjacencyList;
// 初始化邻接矩阵
void init_matrix(AdjacencyMatrix *matrix) {
matrix->vertex_num = 0;
matrix->edge_num = 0;
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
matrix->vertex[i] = 0;
for (int j = 0; j < MAX_VERTEX_NUM; j++) {
matrix->edge[i][j] = 0;
}
}
}
// 初始化邻接表
void init_list(AdjacencyList *list) {
list->vertex_num = 0;
list->edge_num = 0;
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
list->adj_list[i].vertex = 0;
list->adj_list[i].first_edge = NULL;
}
}
// 添加邻接矩阵的边
void add_edge_matrix(AdjacencyMatrix *matrix, int v1, int v2) {
if (v1 >= matrix->vertex_num || v2 >= matrix->vertex_num || v1 < 0 || v2 < 0) {
printf("Error: invalid vertex\n");
return;
}
matrix->edge[v1][v2] = 1;
matrix->edge[v2][v1] = 1; // 无向图需要双向添加
matrix->edge_num++;
}
// 添加邻接表的边
void add_edge_list(AdjacencyList *list, int v1, int v2) {
if (v1 >= list->vertex_num || v2 >= list->vertex_num || v1 < 0 || v2 < 0) {
printf("Error: invalid vertex\n");
return;
}
EdgeNode *node1 = (EdgeNode*)malloc(sizeof(EdgeNode));
node1->adjvex = v2;
node1->weight = 1; // 权值默认为1
node1->next = list->adj_list[v1].first_edge;
list->adj_list[v1].first_edge = node1;
EdgeNode *node2 = (EdgeNode*)malloc(sizeof(EdgeNode));
node2->adjvex = v1;
node2->weight = 1;
node2->next = list->adj_list[v2].first_edge;
list->adj_list[v2].first_edge = node2; // 无向图需要双向添加
list->edge_num++;
}
// 邻接矩阵的DFS
void dfs_matrix(AdjacencyMatrix *matrix, int v, int visited[]) {
visited[v] = 1;
printf("%d ", matrix->vertex[v]);
for (int i = 0; i < matrix->vertex_num; i++) {
if (matrix->edge[v][i] == 1 && visited[i] == 0) {
dfs_matrix(matrix, i, visited);
}
}
}
// 邻接表的DFS
void dfs_list(AdjacencyList *list, int v, int visited[]) {
visited[v] = 1;
printf("%d ", list->adj_list[v].vertex);
EdgeNode *node = list->adj_list[v].first_edge;
while (node != NULL) {
if (visited[node->adjvex] == 0) {
dfs_list(list, node->adjvex, visited);
}
node = node->next;
}
}
// 邻接矩阵的BFS
void bfs_matrix(AdjacencyMatrix *matrix, int v, int visited[]) {
int queue[MAX_VERTEX_NUM];
int front = 0, rear = 0;
visited[v] = 1;
printf("%d ", matrix->vertex[v]);
queue[rear++] = v;
while (front < rear) {
int u = queue[front++];
for (int i = 0; i < matrix->vertex_num; i++) {
if (matrix->edge[u][i] == 1 && visited[i] == 0) {
visited[i] = 1;
printf("%d ", matrix->vertex[i]);
queue[rear++] = i;
}
}
}
}
// 邻接表的BFS
void bfs_list(AdjacencyList *list, int v, int visited[]) {
int queue[MAX_VERTEX_NUM];
int front = 0, rear = 0;
visited[v] = 1;
printf("%d ", list->adj_list[v].vertex);
queue[rear++] = v;
while (front < rear) {
int u = queue[front++];
EdgeNode *node = list->adj_list[u].first_edge;
while (node != NULL) {
if (visited[node->adjvex] == 0) {
visited[node->adjvex] = 1;
printf("%d ", list->adj_list[node->adjvex].vertex);
queue[rear++] = node->adjvex;
}
node = node->next;
}
}
}
int main() {
AdjacencyMatrix matrix;
init_matrix(&matrix);
matrix.vertex_num = 5;
for (int i = 0; i < matrix.vertex_num; i++) {
matrix.vertex[i] = i;
}
add_edge_matrix(&matrix, 0, 1);
add_edge_matrix(&matrix, 0, 2);
add_edge_matrix(&matrix, 0, 4);
add_edge_matrix(&matrix, 1, 4);
add_edge_matrix(&matrix, 2, 3);
add_edge_matrix(&matrix, 2, 4);
add_edge_matrix(&matrix, 3, 4);
int visited_matrix[MAX_VERTEX_NUM] = {0};
dfs_matrix(&matrix, 0, visited_matrix);
printf("\n");
bfs_matrix(&matrix, 0, visited_matrix);
printf("\n");
AdjacencyList list;
init_list(&list);
list.vertex_num = 5;
for (int i = 0; i < list.vertex_num; i++) {
list.adj_list[i].vertex = i;
}
add_edge_list(&list, 0, 1);
add_edge_list(&list, 0, 2);
add_edge_list(&list, 0, 4);
add_edge_list(&list, 1, 4);
add_edge_list(&list, 2, 3);
add_edge_list(&list, 2, 4);
add_edge_list(&list, 3, 4);
int visited_list[MAX_VERTEX_NUM] = {0};
dfs_list(&list, 0, visited_list);
printf("\n");
bfs_list(&list, 0, visited_list);
return 0;
}
```
上述程序中,我们首先定义了邻接矩阵和邻接表两种数据结构,分别用来存储图的信息。然后通过相应的函数来添加边、进行DFS和BFS遍历,其中DFS使用递归算法,BFS使用队列实现。最后在主函数中创建了一个无向图,并进行了两种数据结构的遍历操作。
阅读全文