图的最小生成树

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;
}

### 关于有向最小生成树 对于无向最小生成树是一个经典的优化问题,可以通过 Kruskal 或 Prim 算法来解决。然而,在有向的情况下,定义和求解最小生成树会有所不同。 #### 定义 在有向中,“最小生成树”的概念通常被扩展为 **最小生成森林** 或者更具体地说是 **最小强连通分支树 (Arborescence)**。一个 Arborescence 是一种特殊的有向生成树,其中所有的边都指向根节点或者从根节点出发[^3]。换句话说: - 如果存在一个指定的根节点 \( r \),那么该有向的一个最小生成树可以看作是从这个根节点出发的一棵最小权值的树形结构。 这种情况下,我们称之为 **最小成本有向生成树 (Minimum Cost Directed Spanning Tree)** 或简称 **最小 Arborescence**。 #### Edmonds' Algorithm(Edmonds 算法) 针对有向中的最小生成树问题,最著名的算法是由 Jack Edmonds 提出的一种贪心策略,称为 Edmonds’ algorithm。此算法能够有效地计算以某个特定顶点作为根节点的最小 Arborescence。以下是其核心思想: 1. 初始化:假设给定一个带权重的有向 \( G=(V,E) \),以及选定的根节点 \( r \in V \)[^4]。 2. 构建候选集合:对于每一个非根节点 \( v \neq r \),选取进入 \( v \) 的具有最小权重的一条弧加入到当前候选集中。 3. 处理循环:如果这些选出的弧形成了若干个独立回路,则通过收缩技术把这些回路压缩成单个超级节点,并重复上述过程直到不再形成新的回路为止。 4. 展开操作:最后一步是对之前所做的所有收缩动作逆序展开恢复原始形并得到最终的结果。 这种方法的时间复杂度大约为 O(|E||V|),虽然不是最优效率级别,但在实际应用当中表现良好[^5]。 ```csharp // 下面给出的是伪代码框架用于说明逻辑流程而非完整的C#实现版本 public class Edge { public int From { get; set; } public int To { get; set; } public double Weight { get; set; } } List<Edge> FindMinSpanningArborescence(List<List<Edge>> graph, int root){ List<int>[] inverseGraph = BuildInverseGraph(graph); bool[] visitedNodes = new bool[graph.Count]; Array.Fill(visitedNodes, false); PriorityQueue<(int node, double cost),double> pq=new(); foreach(var edge in graph[root]){ pq.Enqueue((edge.To, edge.Weight), edge.Weight); } while(pq.TryDequeue(out var item)){ if(!visitedNodes[item.node]){ // Process the current minimum weight incoming arc... visitedNodes[item.node]=true; foreach(var outgoingEdge from graph[item.node]){ if(!visitedNodes[outgoingEdge.To]) UpdatePriorityQueueWithNewCosts(pq,outgoingEdge); } } } } ``` 上面展示了一个简化版的思路转换为程序设计形式的例子,注意这并非严格意义上的Edmond's Algorithm完全体,仅提供理解上的帮助[^6]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值