图的最小生成树

1、最小生成树概述

在电子电路设计中,我们常常将多个组件的针脚连接在一起。假如要连接n个针脚,那么我们需要使用n-1条连

线, 且希望连线长度最短。针对这个问题,我们可以采用无向连通图G=(V,E)来表示,其中V表示针脚,E表示针脚之间的连线,并且我们给每条边(u,v) ∈E赋予权重,用来表示连接两个针脚所花费的代价。我们希望找到一个无环的子集T ⊑E,既能将n个针脚连接起来,又具有最小的权重,即w(T)= ∑w(u,v)为最小(其中边(u,v)∈T)。由于是无环的,又是连通的,因此T必然是一个树,我们称这样的树为图G的最小生成树。我们称求取该生成树的问题为最小生成树问题。图1描述的是一个连通图及其最小生成树的例子:


2、最小生成树的形成

在本博客中我们主要讨论Kruskal算法,该算法采用的是贪心策略,所谓的贪心策略就是在多种可能选择中,选

择在当时看来最优的一个。 Kruskal算法的基本思想是:从图中选取权重最小的一条边,如果此边连接的两个顶点不在同一颗树中,那么我们就将该边加入到集合A中,然后重复从剩下的边中选取权重最小的边,并判断此边是否应该被加入到集合中,直到选出n-1条边为止。下面我们以图1为例,来说明算法的过程:



3、实现代码



</pre><pre name="code" class="cpp">#include <iostream>
#include <queue>
/*图的存储结构:邻接链表、邻接矩阵*/
/******************图的数据结构***************/
typedef char ElemType;
const int WHITE = 0;
const int BLACK = 1;
const int MAX_VERTEX = 30;//最多顶点数
const int MAX_ARC = 900;//最多边
/*边的数据结构*/
typedef struct arc_node
{
	int position;//存储边的另一个顶点下标
	int weight;//边的权重
	struct arc_node *next;//指向下一条边
}ANode, *pArc;
/*顶点的数据结构*/
typedef struct vertex_node
{
	ElemType data;
	pArc first_arc;//指向第一条弧
}VNode, *pVNode;
/*图的数据结构*/
typedef struct graphic_node
{
	int vertex_num;//顶点数量
	int arc_num;//边的数量
	pVNode vertex[MAX_VERTEX];//用来存放顶点的数组
}Graphic, *pGNode;

void create_graphic(pGNode g, int direction);//创建图,direction=0无向图,=1有向图
/*最小生成树:
*/
typedef struct node
{
	int u, v;//用来存储有边顶点
	int weight;//用来存储边上的权重
	int select;//在最小生成树中,该边是否被选中,0未选中,1选中
}Node;
void MGT(Graphic g);
void get_edge_info(Graphic g, Node *temp);
void quick_sort(Node *temp, int start, int end);
int partion(Node *temp, int start, int end);

using namespace std;
int main()
{
	Graphic g;
	create_graphic(&g, 0);
	MGT(g);
}

void create_graphic(pGNode g, int direction)//direction = 0表示创建的是无向图,非0值是有向图
{
	cout << "输入顶点数" << endl;
	cin >> g->vertex_num;
	cout << "输入边数" << endl;
	cin >> g->arc_num;

	int i;
	cout << "输入" << g->vertex_num << "个顶点" << endl;
	for (i = 0; i < g->vertex_num; i++)
	{
		g->vertex[i] = (pVNode)malloc(sizeof(VNode));

		cin >> (g->vertex[i]->data);
		g->vertex[i]->first_arc = NULL;
	}
	cout << "输入" << g->arc_num << "个边和边的权重(例如:输入0 1 20表示下标为0和下标为1的顶点有一条边且权重为20)" << endl;
	for (i = 0; i < g->arc_num; i++)
	{
		int x, y, w;
		cin >> x >> y >> w;

		pArc temp1 = (pArc)malloc(sizeof(ANode));
		temp1->position = y;
		temp1->weight = w;
		/*将边加入到链接链表中*/
		temp1->next = g->vertex[x]->first_arc;
		g->vertex[x]->first_arc = temp1;

		if (direction == 0)//说明是无向图
		{
			pArc temp2 = (pArc)malloc(sizeof(ANode));
			temp2->position = x;
			temp2->weight = w;

			temp2->next = g->vertex[y]->first_arc;
			g->vertex[y]->first_arc = temp2;
		}

	}
}
void MGT(Graphic g)
{
	Node *temp = (Node *)malloc(sizeof(Node)*g.arc_num);
	int *parent = (int *)malloc(sizeof(int)*g.vertex_num);
	/*初始化父节点,每个节点的父节点指向自身*/
	int i;
	for (i = 0; i < g.vertex_num; i++)
		parent[i] = i;
	/*将边的信息存入到Node结构体中*/
	get_edge_info(g, temp);
	/*采用快速排序,按照边的权重排序*/
	quick_sort(temp, 0, g.arc_num-1);
	/*选取n-1条边(n为顶点数)*/
	int k = 1;
	for (i = 0; i < g.arc_num && k < g.vertex_num;i++)
	{
		int x, y;
		x = temp[i].u;
		y = temp[i].v;
		/*求节点x、y的父节点*/
		while (parent[x] != x)
			x = parent[x];
		while (parent[y] != y)
			y = parent[y];
		if (x != y)//如果父节点不相等则说明这两个节点不在同一个树中
		{
			parent[x] = y;//把两棵树合并成一颗树
			temp[i].select = 1;
			k++;
		}
	}
	/*输出最小生成树*/
	k = 1;
	for (i = 0; i < g.arc_num && k < g.vertex_num; i++)
	{
		if (temp[i].select == 1)
		{
			cout << "(" << (g.vertex[temp[i].u])->data << "," << (g.vertex[temp[i].v])->data << ") 权重:" 
				 << temp[i].weight << endl;
			k++;
		}
	}
}

void get_edge_info(Graphic g, Node *temp)
{
	int k = 0;
	for (int i = 0; i < g.vertex_num;i++)
	{
		ANode *x = g.vertex[i]->first_arc;
		while (x != NULL)
		{
			int position = x->position;
			if (position < i)
			{
				temp[k].u = i;
				temp[k].v = position;
				temp[k].weight = x->weight;
				temp[k].select = 0;
				k++;
			}
			x = x->next;
		}
	}
}

void quick_sort(Node *temp,int start,int end)
{
	if (start < end)
	{
		int x = partion(temp, start, end);
		quick_sort(temp, start, x - 1);
		quick_sort(temp, x + 1, end);
	}
}

int partion(Node *temp,int start,int end)
{
	Node x = temp[end];
	int i = start, j = i-1;
	Node y;
	for (; i < end;i++)
	{
		if (temp[i].weight < x.weight)
		{
			y = temp[i];
			temp[i] = temp[++j];
			temp[j] = y;
		}
	}
	j++;
	temp[end] = temp[j];
	temp[j] = x;
	return j;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值