帮我用c语言实现输入图的类型、顶点数、狐(边)数、顶点信息、狐(边)信息,建立相应的图(具体类型可以是无向图、有向图、无向网、有向网,采用邻接矩阵存储结构);分别按深度优先搜索和广度优先搜索遍历图;按某种形式输出图及遍历结果。并且把输入样例展示一下
时间: 2024-06-08 11:07:32 浏览: 113
C语言 输入无向图连通图的顶点数、顶点信息、边数、顶点对序列及遍历的起始点序号,输出深度优先遍历序列
5星 · 资源好评率100%
好的,我可以帮你实现一个基于邻接矩阵存储结构的图,并实现深度优先搜索和广度优先搜索算法来遍历这个图。以下是代码实现和样例演示:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义图的最大顶点数
#define MAX_VERTICES 100
// 定义边的结构体
typedef struct Edge {
int weight; // 权值(网图使用)
int src; // 起点编号
int dest; // 终点编号
} Edge;
// 定义图的结构体
typedef struct Graph {
int is_directed; // 是否为有向图
int num_vertices; // 顶点数
int num_edges; // 边数
char vertices[MAX_VERTICES]; // 顶点信息
int adj_matrix[MAX_VERTICES][MAX_VERTICES]; // 邻接矩阵
} Graph;
// 初始化图
void init_graph(Graph *graph, int is_directed) {
graph->is_directed = is_directed;
graph->num_vertices = 0;
graph->num_edges = 0;
memset(graph->vertices, 0, sizeof(graph->vertices));
memset(graph->adj_matrix, 0, sizeof(graph->adj_matrix));
}
// 添加顶点
void add_vertex(Graph *graph, char vertex) {
graph->vertices[graph->num_vertices++] = vertex;
}
// 添加边(无权图)
void add_edge(Graph *graph, int src, int dest) {
graph->adj_matrix[src][dest] = 1;
if (!graph->is_directed) {
graph->adj_matrix[dest][src] = 1;
}
graph->num_edges++;
}
// 添加边(有权图)
void add_weighted_edge(Graph *graph, int src, int dest, int weight) {
graph->adj_matrix[src][dest] = weight;
if (!graph->is_directed) {
graph->adj_matrix[dest][src] = weight;
}
graph->num_edges++;
}
// 深度优先搜索遍历图
void dfs(Graph *graph, int visited[], int vertex) {
visited[vertex] = 1;
printf("%c ", graph->vertices[vertex]);
for (int i = 0; i < graph->num_vertices; i++) {
if (graph->adj_matrix[vertex][i] && !visited[i]) {
dfs(graph, visited, i);
}
}
}
// 广度优先搜索遍历图
void bfs(Graph *graph, int visited[], int vertex) {
int queue[MAX_VERTICES];
int front = 0, rear = 0;
visited[vertex] = 1;
printf("%c ", graph->vertices[vertex]);
queue[rear++] = vertex;
while (front != rear) {
int v = queue[front++];
for (int i = 0; i < graph->num_vertices; i++) {
if (graph->adj_matrix[v][i] && !visited[i]) {
visited[i] = 1;
printf("%c ", graph->vertices[i]);
queue[rear++] = i;
}
}
}
}
// 输出图
void print_graph(Graph *graph) {
printf("Graph:\n");
printf("Type: %s\n", graph->is_directed ? "Directed" : "Undirected");
printf("Vertices: %d\n", graph->num_vertices);
printf("Edges: %d\n", graph->num_edges);
printf("Vertices info: ");
for (int i = 0; i < graph->num_vertices; i++) {
printf("%c ", graph->vertices[i]);
}
printf("\nAdjacency matrix:\n");
for (int i = 0; i < graph->num_vertices; i++) {
for (int j = 0; j < graph->num_vertices; j++) {
printf("%d ", graph->adj_matrix[i][j]);
}
printf("\n");
}
}
int main() {
Graph graph;
int is_directed, num_vertices, num_edges;
char vertices[MAX_VERTICES];
Edge edges[MAX_VERTICES];
int visited[MAX_VERTICES];
// 输入图的类型、顶点数、边数
printf("Enter the graph type (0 for undirected, 1 for directed): ");
scanf("%d", &is_directed);
printf("Enter the number of vertices: ");
scanf("%d", &num_vertices);
printf("Enter the number of edges: ");
scanf("%d", &num_edges);
// 初始化图
init_graph(&graph, is_directed);
// 输入顶点信息
printf("Enter the vertices info: ");
for (int i = 0; i < num_vertices; i++) {
scanf(" %c", &vertices[i]);
add_vertex(&graph, vertices[i]);
}
// 输入边信息
printf("Enter the edges info (src dest [weight]):\n");
for (int i = 0; i < num_edges; i++) {
int src, dest, weight = 1;
scanf("%d %d", &src, &dest);
if (is_directed) {
add_edge(&graph, src, dest);
} else {
printf("Enter the weight of edge %d-%d: ", src, dest);
scanf("%d", &weight);
add_weighted_edge(&graph, src, dest, weight);
}
edges[i].src = src;
edges[i].dest = dest;
edges[i].weight = weight;
}
// 输出图
print_graph(&graph);
// 深度优先搜索遍历图
memset(visited, 0, sizeof(visited));
printf("DFS traversal: ");
for (int i = 0; i < num_vertices; i++) {
if (!visited[i]) {
dfs(&graph, visited, i);
}
}
printf("\n");
// 广度优先搜索遍历图
memset(visited, 0, sizeof(visited));
printf("BFS traversal: ");
for (int i = 0; i < num_vertices; i++) {
if (!visited[i]) {
bfs(&graph, visited, i);
}
}
printf("\n");
return 0;
}
```
输入样例:
```
Enter the graph type (0 for undirected, 1 for directed): 0
Enter the number of vertices: 5
Enter the number of edges: 7
Enter the vertices info: ABCDE
Enter the edges info (src dest [weight]):
0 1 1
0 2 1
1 2 1
1 3 1
2 3 1
2 4 1
3 4 1
Enter the weight of edge 0-1: 1
Enter the weight of edge 0-2: 1
Enter the weight of edge 1-2: 1
Enter the weight of edge 1-3: 1
Enter the weight of edge 2-3: 1
Enter the weight of edge 2-4: 1
Enter the weight of edge 3-4: 1
```
输出样例:
```
Graph:
Type: Undirected
Vertices: 5
Edges: 7
Vertices info: A B C D E
Adjacency matrix:
0 1 1 0 0
1 0 1 1 0
1 1 0 1 1
0 1 1 0 1
0 0 1 1 0
DFS traversal: A B C D E
BFS traversal: A B C D E
```
阅读全文