贪心算法之Prim最小生成树(2021/1/21)

问题引入

Prim生成最小树算法(邻接矩阵存储图)

代码实现

/*Create by Wanlu Gao
* 2021/1/21 
* 实现功能:
* 在此程序中实现的为邻接矩阵存储图Prim最小生成树
*/
#include<iostream>
#include<cstdlib>
using namespace std;
//定义无穷
const int INF=9999; 
//存储图的顶点数 
const int map_dot_num=7;
//初始化邻接矩阵
const int map[map_dot_num][map_dot_num]={
{INF,23,INF,INF,INF,28,36},
{23,INF,20,INF,INF,INF,1},
{INF,20,INF,15,INF,INF,4},
{INF,INF,15,INF,3,INF,9},
{INF,INF,INF,3,INF,17,16},
{28,INF,INF,INF,17,INF,25},
{36,1,4,9,16,25,INF}};

//函数功能:输出邻接矩阵 
void PrintMap(void){
	int i,j;
	cout<<"Map:\n";
	for(i=0;i<map_dot_num;i++){
		cout<<"\t";
		for(j=0;j<map_dot_num;j++){
			if(map[i][j]==INF)
				cout<<" INF";
			else
				cout<<" "<<map[i][j];
		}
		cout<<endl;
	} 
}
int main(int argc,char**argv){
	int i=0,j=0;
	//输出邻接矩阵
	PrintMap();
	//定义集合数组
	int s[map_dot_num]={0};//{0}表示s集合为空集
	//定义起点
	int start=0; 
	//定义lowcost数组
	int lowcost[map_dot_num]={INF};
	//初始化closest数组
	int closest[map_dot_num]={0};
	//首先让起点0加入s集合
	s[start]=1;
	//初始化lowcost数组与closest数组
	for(i=0;i<map_dot_num;i++){
		if(i!=start){
			lowcost[i]=map[i][start];
			closest[i]=start; 
		}else{
			//更新lowcost
			lowcost[i]=0; 
		}
	}
	//使用Prim生成最小树,需要map_dot_num-1选最小边
	for(i=1;i<map_dot_num;i++){
		//在lowcost选一个在v-s,且距离s最小的目标
		int target=start;
		int temp=INF;
		for(j=0;j<map_dot_num;j++){
			if(j!=start&&s[j]==0&&temp>lowcost[j]){
				temp=lowcost[j];
				target=j;
			}
		}
		if(target==start){
			break;//没有找到目标 
		}
		//加入s集合
		s[target]=1;
		//更新lowcost与closest数组
		for(j=0;j<map_dot_num;j++){
			if(j!=start&&s[j]==0&&lowcost[j]>map[target][j]){
				lowcost[j]=map[target][j];
				closest[j]=target;
			}
		} 
	}
	//输出lowcost数组
	cout<<"lowcost[]:\n\t";
	for(i=0;i<map_dot_num;i++){
		cout<<" "<<lowcost[i];
	}
	//输出closest数组
	cout<<"\nclosest[]:\n\t";
	for(i=0;i<map_dot_num;i++){
		cout<<" "<<closest[i];
	}	
	return 0;
}
 

程序输出

Map:
         INF 23 INF INF INF 28 36
         23 INF 20 INF INF INF 1
         INF 20 INF 15 INF INF 4
         INF INF 15 INF 3 INF 9
         INF INF INF 3 INF 17 16
         28 INF INF INF 17 INF 25
         36 1 4 9 16 25 INF
lowcost[]:
         0 23 4 9 3 17 1
closest[]:
         0 0 6 6 3 4 1
--------------------------------
Process exited after 0.06 seconds with return value 0
请按任意键继续. . .



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高万禄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值