Languages: C/C++
Categories: 编程语言基础

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;

#define MVNUM 30     //最大顶点数
#define MAXINT INT_MAX   //表示极大值，即∞
//图的邻接矩阵
typedef struct {
char vexs[MVNUM];          //顶点表
int arcs[MVNUM][MVNUM];    //邻接矩阵
int vexnum,arcnum;        //图的当前点数和边数
}MGraph;

//辅助数组Edges的定义
typedef struct{
char Tail;                  //边的终点
int lowcost;                //边上的权值
}Edges;
Edges Edge[MVNUM*(MVNUM-1) / 2];

int Vexset[MVNUM];

int LocateVex(MGraph G , char v){
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vexs[i] == v)
return i;
return -1;
}//LocateVex

void Create(MGraph &G)
{
int i, j, k;
FILE *fp;
char v1 , v2;
int w;
if((fp = fopen("Tree.txt","r"))==NULL) {
printf("Could not open Tree.txt file\n");
exit(0);
}
//cout<<"请输入总顶点数，总边数，以空格隔开:";
//cin >> G.vexnum >> G.arcnum;
//cout<<endl;
fscanf(fp, "%d %d\n", &G.vexnum, &G.arcnum);
//cout << "输入点的名称，如a" << endl;
for(i = 0; i < G.vexnum; ++i){
//cout << "请输入第" << (i+1) << "个点的名称:";
//cin >> G.vexs[i];                        		//依次输入点的信息
fscanf(fp, "%c ", &G.vexs[i]);
}
//cout << endl;
for(i = 0; i < G.vexnum; ++i)                		//初始化邻接矩阵，边的权值均置为极大值MaxInt
for(j = 0; j < G.vexnum; ++j)
G.arcs[i][j] = MAXINT;
//cout << "输入边依附的顶点及权值，如a b 6" << endl;
for(k = 0; k < G.arcnum;++k){						//构造邻接矩阵
//cout << "请输入第" << (k + 1) << "条边依附的顶点及权值:";
//cin >> v1 >> v2 >> w;                           //输入一条边依附的顶点及权值
fscanf(fp, "%c %c %d\n", &v1, &v2, &w);

i = LocateVex(G, v1);  j = LocateVex(G, v2);	//确定v1和v2在G中的位置，即顶点数组的下标
G.arcs[i][j] = w;								//边<v1, v2>的权值置为w
G.arcs[j][i] = G.arcs[i][j];					//置<v1, v2>的对称边<v2, v1>的权值为w
Edge[k].lowcost = w;
Edge[k].Tail = v2;
}
}

bool cmp(Edges a, Edges b)
{
return a.lowcost < b.lowcost;
}
//----------冒泡排序-------------------
void Sort(MGraph G){
int m = G.arcnum;
for(int i = 0; i < m-1; i++ ){
for(int j = 0 ; j <= m-2-i; j++){
if(Edge[j].lowcost > Edge[j+ 1].lowcost){
char temp_Tail = Edge[j].Tail;
Edge[j].Tail = Edge[j+ 1].Tail;
Edge[j + 1].Tail = temp_Tail;
int temp_lowcost = Edge[j].lowcost;
Edge[j].lowcost = Edge[j+ 1].lowcost;
Edge[j + 1].lowcost = temp_lowcost;
}//if
}//for
}//while
}//Sort

void MiniSpanTree_Kruskal(MGraph G)
{
int i , j , v1 , v2 , vs1 , vs2;
sort(Edge, Edge+G.arcnum, cmp);           //将数组Edge中的元素按权值从小到大排序
for(i = 0; i < G.vexnum; ++i)     					//辅助数组，表示各顶点自成一个连通分量
Vexset[i] = i;
for(i = 0; i < G.arcnum; ++i){
//依次查看排好序的数组Edge中的边是否在同一连通分量上
v2 =LocateVex(G, Edge[i].Tail);     			//v2为边的终点Tail的下标
vs1 = Vexset[v1];       						//获取边Edge[i]的始点所在的连通分量vs1
vs2 = Vexset[v2];       						//获取边Edge[i]的终点所在的连通分量vs2
if(vs1 != vs2){         						//边的两个顶点分属不同的连通分量
cout << Edge[i].Head << "-->" << Edge[i].Tail <<"    "<<Edge[i].lowcost<< endl;		//输出此边
for(j = 0; j < G.vexnum; ++j)      			//合并vs1和vs2两个分量，即两个集合统一编号
if(Vexset[j] == vs2) Vexset[j] = vs1;	//集合编号为vs2的都改为vs1
}//if
}//for
}//MiniSpanTree_Kruskal

int main(){
MGraph G;
Create(G);

cout <<endl;
cout << "*****无向网G创建完成！*****" << endl;

cout <<endl;
MiniSpanTree_Kruskal(G);
return 0;
}