Kruskal算法是有关图的最小生成树的算法。Kruskal算法是两个经典的最小生成树算法之一,另外一个是Prim算法。

程序来源:Minimum Spanning Tree using Krushkal’s Algorithm。

百度百科:Kruskal算法。

维基百科:Kruskal's Algorithm。

C++语言程序:

/* Krushkal's Algorithm to find minimum spanning tree by TheCodersPortal */
#include <iostream>
#include <map>
using namespace std;/* Structure of a Disjoint-Set node */
typedef struct dset
{char data;int rank;struct dset* parent;
}dset;
/* To count the number of disjiont sets present at a time */
int count=0;/* Map is used to get the address of the node by its data */
map <char,dset*> mymap;/* MakeSet function is responsible for creating a disjoint-set whose only member is data,initially the rank is 0 and it is the representative of itself */
void MakeSet(char data)
{dset* node=new dset;node->data=data;mymap[data]=node;node->rank=0;node->parent=node;count++;
}/* FindSetUtil takes input to the pointer to the node and return the pointer tothe representative of that node. A Heuristic is used here, path compressionwhich means that all the nodes on the find path point directly to the root */
dset* FindSetUtil(dset* node)
{if(node!=node->parent)node->parent=FindSetUtil(node->parent);return node->parent;
}/*FindSet function is used to find the node using the data and pass this node to FindSetUtil*/
char FindSet(char x)
{dset* node=mymap[x];node=FindSetUtil(node);return node->data;
}/*isconnected checks whether the two elements of the set are connected or not */
bool isconnected(char x,char y)
{return (FindSet(x)==FindSet(y));
}/* LinkSet is responsible for union of two disjoint set according to a Heuristics, Union by Rank */
void LinkSet(char x,char y)
{if(isconnected(x,y))return;dset* node1=mymap[x];dset* node2=mymap[y];if(node1->rank>node2->rank)node2->parent=node1;elsenode1->parent=node2;if(node1->rank==node2->rank){node1->rank=node1->rank+1;}count--;
}/* UnionSet calls the LinkSet function with arguments as FindSet(x),FindSet(y) */
void UnionSet(char x,char y)
{LinkSet(FindSet(x),FindSet(y));
}/* freeset is used to free the memory allocated during the run-time */
void freeset(char arr[],int n)
{for(int i=0;i<n;i++){dset* node=mymap[arr[i]];delete node;}
}void QuickSort(int array[],int low,int high,char edgein[],char edgeout[])
{int i=low,j=high;/* Here we are taking pivot as mid element */int pivot=array[(low+high)/2];/* Partitioning the array */while(i<=j){while(array[i]<pivot)i++;while(array[j]>pivot)j--;if (i<=j){/* Swapping of elements at ith index and jth index */int temp = array[i];array[i] = array[j];array[j] = temp;char tmp=edgein[i];edgein[i]=edgein[j];edgein[j]=tmp;tmp=edgeout[i];edgeout[i]=edgeout[j];edgeout[j]=tmp;i++;j--;}}/* Recursion */if (i<high)QuickSort(array, i, high,edgein,edgeout);if (j>low)QuickSort(array, low, j,edgein, edgeout);}/* MSTKrushkal is the function which is used to find minimum spanning treeusing Krushkal'sAlgorithm */
void MSTKrushkal(char vertices[],char edgein[],char edgeout[],int weight[],int n,int m)
{//int n=sizeof(vertices)/sizeof(vertices[0]);for(int i=0;i<n;i++){MakeSet(vertices[i]);}/*Sorting of edges in non-decreasing order of their weights */QuickSort(weight,0,m-1,edgein,edgeout);int A=0;cout<<"Minimum spanning tree containing edges"<<endl;for(int i=0;i<9;i++){/* if adding the edge result in cycle, ignore the edge */if(isconnected(edgein[i],edgeout[i]))continue;/* Add the edge to the minimum spanning tree */UnionSet(edgein[i],edgeout[i]);A+=weight[i];cout<<edgein[i]<<"--"<<edgeout[i]<<endl;}cout<<"Total weight = "<<A<<endl;freeset(vertices,n);
}/* Driver program to test the above functions */
int main()
{/* vertices array contains all the vertices of the graph */char vertices[]={'A','B','C','D','E','F'};/* edgein,edgeout and weight array indicates that there is an edgebetween vertices edgein[i] and edgeout[i] having weight weight[i] */char edgein[]={'A','A','A','B','B','C','C','D','D'};char edgeout[]={'B','D','C','D','E','D','F','E','F'};int weight[]={6,5,3,8,2,2,6,5,3};int n=sizeof(vertices)/sizeof(vertices[0]);int m=sizeof(weight)/sizeof(weight[0]);MSTKrushkal(vertices,edgein,edgeout,weight,n,m);
}

程序运行结果:

Minimum spanning tree containing edges
B--E
C--D
D--F
A--C
D--E
Total weight = 15

Kruskal算法的C++语言程序相关推荐

  1. Kruskal算法的C语言程序

    Kruskal算法是有关图的最小生成树的算法.Kruskal算法是两个经典的最小生成树算法之一,另外一个是Prim算法. 程序来源:Kruskal's Algorithm. 百度百科:Kruskal算 ...

  2. Dijkstra算法的C语言程序

    Dijkstra算法用来寻找图的结点间最短路径,通常是指定一个起始结点后,寻找从该结点出发,到达各个结点的最短路径.该算法是有关最短路径问题的一个算法.由Dijkstra于1959年提出. 百度百科: ...

  3. Prim算法的C语言程序

    Prim算法是有关图的最小生成树的算法.1957年由美国计算机科学家罗伯特·普里姆(Robert C. Prim)独立发现. 百度百科:Prim算法. 维基百科:Prim's Algorithm. 参 ...

  4. c语言滤出是个最小值,经典滤波算法及C语言程序

    <经典滤波算法及C语言程序>由会员分享,可在线阅读,更多相关<经典滤波算法及C语言程序(9页珍藏版)>请在人人文库网上搜索. 1.经典的滤波算法经典的滤波算法 可以用用可以用用 ...

  5. 小型温控系统c语言程序,pid算法温度控制c语言程序 - 全文

    温度控制PID自整定原理介绍 整定PID(三模式)控制器 整定温度控制器涉及设置比例.积分和微分值,以得到对特定过 程的可能的最佳控制.如果控制器不包含自动整定算法,或者自 动整定算法未提供适合特定应 ...

  6. 最小生成树构造算法--Prim算法,Kruskal算法(C语言)

    最小生成树 最小生成树(minimum spanning tree)是由n个顶点,n-1条边,将一个连通图连接起来,且使权值最小的结构. 最小生成树可以用Prim(普里姆)算法或kruskal(克鲁斯 ...

  7. 算法分支定界法C语言程序,常用算法大全-分枝定界

    任何美好的事情都有结束的时候.现在我们学习的是本书的最后一章.幸运的是,本章用到的大部分概念在前面各章中已作了介绍.类似于回溯法,分枝定界法在搜索解空间时,也经常使用树形结构来组织解空间(常用的树结构 ...

  8. pid温度控制c语言程序及仿真,温度控制PID算法的C语言程序实例代码

    //PID算法温控C语言 #include #include #include #include struct PID { unsigned int SetPoint; // 设定目标 Desired ...

  9. pid温度控制c语言程序及仿真,pid算法温度控制c语言程序

    描述 温度控制PID自整定原理介绍 整定PID(三模式)控制器 整定温度控制器涉及设置比例.积分和微分值,以得到对特定过 程的可能的最佳控制.如果控制器不包含自动整定算法,或者自 动整定算法未提供适合 ...

最新文章

  1. 怎么安装python3-python3怎么安装
  2. centos6.3下搭建LAMP环境
  3. Angular安装教程
  4. java编译容器_Java基础:容器
  5. FFmpeg编码支持与定制(三)
  6. 关于Mac电脑更新IP地址的解决方法!
  7. java扫雷游戏设计总结_JAVA版扫雷游戏的设计与实现
  8. 人人开源代码生成器的使用
  9. 如何用一句话激怒设计师
  10. windows11桌面删除ie图标
  11. 蓝桥杯--第七届决赛:圆圈舞
  12. 【总目录】人工智能、机器学习、深度学习总结大全----目录.未完待续...
  13. 春风得意的 jQuery
  14. 2020年,阿里最新的java程序员面试题目含答案带你吊打面试官
  15. 手机蓝牙音响音质测试软件,多款蓝牙音箱对比评测 | 声音性能及产品硬素质对比评测_什么值得买...
  16. 程序员职业发展路线图(完整版+珍藏版)
  17. 黑底白字html代码,如何用chrome扩展将网页变成黑底白字,用以保护视力
  18. js小游戏 (飞行的小鸟--canvas)
  19. 英特尔®以太网控制器E810介绍:面向5G核心网络
  20. 一种电子病历系统软件框架思想——B/S与C/S混合架构

热门文章

  1. 在C#中获取如PHP函数time()一样的时间戳
  2. Spark RDD的运行机制 工作节点分布关系
  3. spark-dataframe与rdd的区别
  4. php 向文件夹中添加HTML文件,批量向html中插入内容
  5. python 数据库模块_MySQl 数据库 之 python模块 pymysql 简单介绍
  6. c++ 编译添加dll_使用MinGW编译OpenCV4源码与配置使用演示
  7. 使用Jenkins自动化打包并且生成二维码
  8. Linux基础——怎么样用 TeamViewer 和 VNC 从远程控制电脑
  9. Linux下 C++遍历目录文件
  10. javascript对table的添加,删除行的操作