kruskal算法的实现

无向图中找出一颗最小生成数可以使用kruskal算法。连续地按最小的权选择边,当所选取的边不产生圈时把它作为取定的边。

Kruskal算法一般步骤:

1.选定权值最小的边。

2.判断边两端的顶点是否在同一个集合,如果不在,则此边有效,否则放弃。

3.重复上述步骤,直到将图连通。


代码如下:

优先队列部分:

#ifndef BIN_HEAP_H_
#define BIN_HEAP_H_

#include <stdbool.h>
#include "kruskal.h"

#define MIN_PQ_SIZE 1
#define MIN_DATA 0

typedef struct heap
{
	int capacity;
	int size;
	EdgeElement *elements;
} Heap;

typedef Heap *PriorityQueue;

/****************************************************************
*函数名称:	InitQueue
*函数功能:	初始化一个优先队列
*入口参数:	maxElements:	优先队列的大小
*返 回 值:	queue:			初始化好的优先队列
*****************************************************************/
PriorityQueue InitQueue(int maxElements);

/****************************************************************
*函数名称:	IsEmpty
*函数功能:	判断队列是否为空
*入口参数:	queue:	需要判断的队列
*返 回 值:	true:	队列为空
*			false:	队列不为空
*****************************************************************/
bool IsEmpty(const PriorityQueue queue);

/****************************************************************
*函数名称:	IsEmpty
*函数功能:	判断队列是否为空
*入口参数:	queue:	需要判断的队列
*返 回 值:	true:	队列为空
*			false:	队列不为空
*****************************************************************/
bool IsFull(const PriorityQueue queue);

/****************************************************************
*函数名称:	Insert
*函数功能:	插入元素到队列中
*入口参数:	element:	需要插入的元素
*			queue:		插入元素的队列
*返 回 值:	无
*****************************************************************/
void Insert(EdgeElement element, const PriorityQueue queue);

/****************************************************************
*函数名称:	DeleteMin
*函数功能:	删除最小的元素
*入口参数:	queue:		需要删除元素的队列
*返 回 值:	minElement:	被删除的最小元素的值
*****************************************************************/
EdgeElement DeleteMin(const PriorityQueue queue);

/****************************************************************
*函数名称:	PrintQueue
*函数功能:	打印整个队列
*入口参数:	queue:	需要打印的队列
*返 回 值:	无
*****************************************************************/
void PrintQueue(const PriorityQueue queue);

/****************************************************************
*函数名称:	EmptyQueue
*函数功能:	清空整个队列
*入口参数:	queue:	需要清空的队列
*返 回 值:	无
*****************************************************************/
void EmptyQueue(const PriorityQueue queue);

#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "bin_heap.h"

/****************************************************************
*函数名称:	InitQueue
*函数功能:	初始化一个优先队列
*入口参数:	maxElements:	优先队列的大小
*返 回 值:	queue:			初始化好的优先队列
*************
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值