无向图中找出一颗最小生成数可以使用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: 初始化好的优先队列
*************