最小生成树C语言(1)由给定的顶点和边的信息构造图的邻接矩阵存储; (2)对该图进行深度优先搜索,输出搜索得到的结点序列; 以邻接表作存储结构,用克鲁斯卡尔算法构造最小生成树

时间: 2024-02-19 15:59:05 浏览: 21
(1) 由给定的顶点和边的信息构造图的邻接矩阵存储 以下是构造邻接矩阵存储的示例代码: ```c #include <stdio.h> #define V 6 // 图中的顶点数 int graph[V][V] = { {0, 4, 1, 0, 0, 0}, {4, 0, 2, 5, 0, 0}, {1, 2, 0, 8, 10, 0}, {0, 5, 8, 0, 2, 6}, {0, 0, 10, 2, 0, 3}, {0, 0, 0, 6, 3, 0} }; int main() { printf("邻接矩阵:\n"); for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) printf("%d ", graph[i][j]); printf("\n"); } return 0; } ``` 输出结果为: ``` 邻接矩阵: 0 4 1 0 0 0 4 0 2 5 0 0 1 2 0 8 10 0 0 5 8 0 2 6 0 0 10 2 0 3 0 0 0 6 3 0 ``` (2) 对该图进行深度优先搜索,输出搜索得到的结点序列 以下是深度优先搜索的示例代码: ```c #include <stdio.h> #include <stdbool.h> #define V 6 // 图中的顶点数 int graph[V][V] = { {0, 4, 1, 0, 0, 0}, {4, 0, 2, 5, 0, 0}, {1, 2, 0, 8, 10, 0}, {0, 5, 8, 0, 2, 6}, {0, 0, 10, 2, 0, 3}, {0, 0, 0, 6, 3, 0} }; bool visited[V]; void DFS(int v) { visited[v] = true; printf("%d ", v); for (int i = 0; i < V; i++) if (graph[v][i] && !visited[i]) DFS(i); } void DFSTraversal() { printf("深度优先搜索序列:"); for (int i = 0; i < V; i++) if (!visited[i]) DFS(i); printf("\n"); } int main() { for (int i = 0; i < V; i++) visited[i] = false; DFSTraversal(); return 0; } ``` 输出结果为: ``` 深度优先搜索序列:0 1 2 3 4 5 ``` (3) 以邻接表作存储结构,用克鲁斯卡尔算法构造最小生成树 以下是用邻接表作存储结构,用克鲁斯卡尔算法构造最小生成树的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define V 6 // 图中的顶点数 #define E 9 // 图中的边数 struct Edge { int src, dest, weight; }; struct Graph { int V, E; struct Edge* edge; }; struct Subset { int parent, rank; }; struct Graph* createGraph() { struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); graph->V = V; graph->E = E; graph->edge = (struct Edge*)malloc(E * sizeof(struct Edge)); return graph; } int find(struct Subset subsets[], int i) { if (subsets[i].parent != i) subsets[i].parent = find(subsets, subsets[i].parent); return subsets[i].parent; } void Union(struct Subset subsets[], int x, int y) { int xroot = find(subsets, x); int yroot = find(subsets, y); if (subsets[xroot].rank < subsets[yroot].rank) subsets[xroot].parent = yroot; else if (subsets[xroot].rank > subsets[yroot].rank) subsets[yroot].parent = xroot; else { subsets[yroot].parent = xroot; subsets[xroot].rank++; } } int myComp(const void* a, const void* b) { struct Edge* a1 = (struct Edge*)a; struct Edge* b1 = (struct Edge*)b; return a1->weight - b1->weight; } void KruskalMST(struct Graph* graph) { struct Edge result[V]; int e = 0; int i = 0; qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp); struct Subset* subsets = (struct Subset*)malloc(V * sizeof(struct Subset)); for (int v = 0; v < V; v++) { subsets[v].parent = v; subsets[v].rank = 0; } while (e < V - 1 && i < graph->E) { struct Edge next_edge = graph->edge[i++]; int x = find(subsets, next_edge.src); int y = find(subsets, next_edge.dest); if (x != y) { result[e++] = next_edge; Union(subsets, x, y); } } printf("最小生成树的边:\n"); for (int i = 0; i < e; i++) printf("%d - %d : %d\n", result[i].src, result[i].dest, result[i].weight); } int main() { struct Graph* graph = createGraph(); graph->edge[0].src = 0; graph->edge[0].dest = 2; graph->edge[0].weight = 1; graph->edge[1].src = 0; graph->edge[1].dest = 1; graph->edge[1].weight = 4; graph->edge[2].src = 1; graph->edge[2].dest = 2; graph->edge[2].weight = 2; graph->edge[3].src = 1; graph->edge[3].dest = 3; graph->edge[3].weight = 5; graph->edge[4].src = 2; graph->edge[4].dest = 3; graph->edge[4].weight = 8; graph->edge[5].src = 2; graph->edge[5].dest = 4; graph->edge[5].weight = 10; graph->edge[6].src = 3; graph->edge[6].dest = 4; graph->edge[6].weight = 2; graph->edge[7].src = 3; graph->edge[7].dest = 5; graph->edge[7].weight = 6; graph->edge[8].src = 4; graph->edge[8].dest = 5; graph->edge[8].weight = 3; KruskalMST(graph); return 0; } ``` 输出结果为: ``` 最小生成树的边: 0 - 2 : 1 1 - 2 : 2 3 - 4 : 2 4 - 5 : 3 1 - 3 : 5 ```

相关推荐

最新推荐

recommend-type

基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip

基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip基于Android Studio的个人记账应用帮助用户轻松管理和跟踪他们的财务源码.zip
recommend-type

基于python实现树莓派和传感器的植物生长环境评估信息系统

【作品名称】:基于python实现树莓派和传感器的植物生长环境评估信息系统 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】:基于python实现树莓派和传感器的植物生长环境评估信息系统
recommend-type

优质资源,Yearning Mysql SQL审核平台

Yearning Mysql SQL审核平台(使用go语言)
recommend-type

c语言课程设计-职工资源管理系统.rar

void displayMenu() { printf("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); printf("+ 职工资源管理系统 +\n"); printf("+ +\n"); printf("+ 1. 录入职工信息 +\n"); printf("+ 2. 显示全部职工信息 +\n"); printf("+ 3. 根据工号查询
recommend-type

华为OD机试D卷 - 来自异国的客人 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。