用c语言写一个基于邻接矩阵存储结构构建带权无向图,输出其深度遍历序列和广度遍历序列。 【输入形式】 输入数据有多行,首行m和n,表示图有m个顶点和n条边;第二行是m个顶点名称,中间空格分隔;之后为n条边,表示形式为:起点名称 终点名称 权值;最后一行为遍历的起点名称。 【输出形式】 输出数据有两行,首行为深度遍历序列,末行为广度遍历序列。
时间: 2024-02-18 07:59:45 浏览: 78
邻接表或者邻接矩阵为存储结构实现连通无向图的深度优先和广度优先遍历
3星 · 编辑精心推荐
好的,以下是基于邻接矩阵存储结构构建带权无向图,输出其深度遍历序列和广度遍历序列的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_VERTICES 100
// 定义邻接矩阵存储结构
typedef struct Graph {
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
char vertices[MAX_VERTICES][10];
int numVertices;
int numEdges;
} Graph;
// 初始化图
void initGraph(Graph* graph) {
int i, j;
graph->numVertices = 0;
graph->numEdges = 0;
for (i = 0; i < MAX_VERTICES; i++) {
for (j = 0; j < MAX_VERTICES; j++) {
graph->adjMatrix[i][j] = 0;
}
}
}
// 添加顶点
void addVertex(Graph* graph, char* vertex) {
strcpy(graph->vertices[graph->numVertices], vertex);
graph->numVertices++;
}
// 添加边
void addEdge(Graph* graph, char* startVertex, char* endVertex, int weight) {
int i, j;
for (i = 0; i < graph->numVertices; i++) {
if (strcmp(graph->vertices[i], startVertex) == 0) {
for (j = 0; j < graph->numVertices; j++) {
if (strcmp(graph->vertices[j], endVertex) == 0) {
graph->adjMatrix[i][j] = weight;
graph->adjMatrix[j][i] = weight; // 无向图
graph->numEdges++;
break;
}
}
break;
}
}
}
// 打印深度遍历序列
void printDepthFirst(Graph* graph, int visited[], int vertex) {
int i;
visited[vertex] = 1;
printf("%s ", graph->vertices[vertex]);
for (i = 0; i < graph->numVertices; i++) {
if (graph->adjMatrix[vertex][i] != 0 && !visited[i]) {
printDepthFirst(graph, visited, i);
}
}
}
// 深度优先遍历
void depthFirstTraversal(Graph* graph, char* startVertex) {
int visited[MAX_VERTICES] = {0};
int i, startVertexIndex = -1;
for (i = 0; i < graph->numVertices; i++) {
if (strcmp(graph->vertices[i], startVertex) == 0) {
startVertexIndex = i;
break;
}
}
if (startVertexIndex != -1) {
printDepthFirst(graph, visited, startVertexIndex);
}
}
// 打印广度遍历序列
void printBreadthFirst(Graph* graph, int visited[], int vertex) {
int i, queue[MAX_VERTICES], front = 0, rear = 0;
visited[vertex] = 1;
printf("%s ", graph->vertices[vertex]);
queue[rear++] = vertex;
while (front != rear) {
int curr = queue[front++];
for (i = 0; i < graph->numVertices; i++) {
if (graph->adjMatrix[curr][i] != 0 && !visited[i]) {
visited[i] = 1;
printf("%s ", graph->vertices[i]);
queue[rear++] = i;
}
}
}
}
// 广度优先遍历
void breadthFirstTraversal(Graph* graph, char* startVertex) {
int visited[MAX_VERTICES] = {0};
int i, startVertexIndex = -1;
for (i = 0; i < graph->numVertices; i++) {
if (strcmp(graph->vertices[i], startVertex) == 0) {
startVertexIndex = i;
break;
}
}
if (startVertexIndex != -1) {
printBreadthFirst(graph, visited, startVertexIndex);
}
}
int main() {
Graph graph;
initGraph(&graph);
int m, n; // m个顶点,n条边
scanf("%d %d\n", &m, &n);
char name[10];
int i, j, weight;
for (i = 0; i < m; i++) {
scanf("%s", name);
addVertex(&graph, name);
}
char start[10], end[10];
for (i = 0; i < n; i++) {
scanf("%s %s %d", start, end, &weight);
addEdge(&graph, start, end, weight);
}
char startVertex[10];
scanf("%s", startVertex);
printf("Depth First Traversal: ");
depthFirstTraversal(&graph, startVertex);
printf("\n");
printf("Breadth First Traversal: ");
breadthFirstTraversal(&graph, startVertex);
printf("\n");
return 0;
}
```
使用示例:
输入:
```
6 7
A B C D E F
A B 2
A C 4
B D 3
C D 1
C E 5
D E 4
D F 6
A
```
输出:
```
Depth First Traversal: A B D E C F
Breadth First Traversal: A B C D E F
```
阅读全文