最小生成树

oydxxynu 2013-12-25 02:44:02
#include <cstdlib>
#include <iostream>
#include<cstring>
#include<fstream>
#define Maxsize 30
#define INFINITY 99999

using namespace std;

typedef struct ArcNode
{
int adjvex;
int info;
struct ArcNode *nextarc;
}ArcNode;

typedef struct adjlist
{
char name[10];
ArcNode *firstarc;
}adjlist;

typedef struct
{
int vexnum,arcnum;
adjlist ver[Maxsize];
}ALGraph;

typedef struct
{
int vex1,vex2;
int weight;
}Edge;

int Locatecity(ALGraph G,char v[]);
void createGraph();
void output(ALGraph G);
void KruskalminTree(ALGraph G);
void swapedge(Edge &a,Edge &b);

int main()
{

createGraph();
system("PAUSE");
return 0;
}

void KruskalminTree(ALGraph G) //克鲁斯卡尔算法求最小生成树
{
int i,y=0;
Edge edge[G.arcnum]; //存边的数组
int set[G.vexnum]; //点得集合
int w[G.vexnum]; //存花费的数组
for(i=0;i<G.vexnum;i++) //初始化
set[i]=i;
for(i=0;i<G.vexnum;i++)
{
ArcNode *p;
if(G.ver[i].firstarc!=NULL)
{
p=G.ver[i].firstarc;
while(p)
{
edge[y].vex1=i;
edge[y].vex2=p->adjvex;
edge[y].weight=p->info;
p=p->nextarc;
y++;
}//while
}//if
}//for

for(i=0;i<G.arcnum-1;i++) //将边进行从小到大排序
{
for(int j=0;j<G.arcnum-i;j++)
if(edge[j].weight>edge[j+1].weight)
{
swapedge(edge[j],edge[j+1]);
}//if
}//for
int v=0,x;
w[v++]=edge[0].weight;
set[edge[0].vex2]=set[edge[0].vex1];
cout<<"选择的弧依次为: \n";
cout<<"<"<<G.ver[edge[0].vex1].name<<","<<G.ver[edge[0].vex2].name<<"> ";
for(i=1;i<G.arcnum;i++) //选择弧头弧尾不在同一个集合的弧
{
if(set[edge[i].vex1]!=set[edge[i].vex2])
{
if(set[edge[i].vex1]<set[edge[i].vex2])
{
x=set[edge[i].vex2];
set[edge[i].vex2]=set[edge[i].vex1];
for(int k=0;k<G.vexnum;k++)
{
if(set[k]==x)
set[k]=set[edge[i].vex1];
}//for
w[v++]=edge[i].weight;
cout<<"<"<<G.ver[edge[i].vex1].name<<","<<G.ver[edge[i].vex2].name<<"> ";
}//if
else if(set[edge[i].vex1]>set[edge[i].vex2])
{
x=set[edge[i].vex1];
set[edge[i].vex1]=set[edge[i].vex2];
for(int k=0;k<G.vexnum;k++)
{
if(set[k]==x)
set[k]=set[edge[i].vex2];
}//for
w[v++]=edge[i].weight;
cout<<"<"<<G.ver[edge[i].vex1].name<<","<<G.ver[edge[i].vex2].name<<"> ";
}//else
}//if
}//for
cout<<"\n依次对应的弧的花费为:";
for(int j=0;j<v;j++)
cout<<w[j]<<" ";
cout<<endl;
}

void swapedge(Edge &a,Edge &b) //交换两个变量的值的函数
{
Edge temp;
temp.vex1=a.vex1;
temp.vex2=a.vex2;
temp.weight=a.weight;
a.vex1=b.vex1;
a.vex2=b.vex2;
a.weight=b.weight;
b.vex1=temp.vex1;
b.vex2=temp.vex2;
b.weight=temp.weight;
}

void createGraph()
{
ALGraph G;
cout<<"请输入图的顶点个数: \n";
cin>>G.vexnum;
cout<<"请输入弧数:\n";
cin>>G.arcnum;
for(int i=0;i<G.vexnum;i++)
{
cout<<"第 "<<i+1<<" 个顶点的名称\n";
cin>>G.ver[i].name;
G.ver[i].firstarc=NULL;
}//for
int n,m,cost;
char v1[15],v2[15];
ifstream fin("ttt.txt");//打开文件
//读入数字

for(int j=0;j<G.arcnum;j++)
{
cout<<"请输入第 "<<j+1<<" 条弧的弧尾与弧头"<<endl;
scanf("%s",v1);
scanf("%s",v2);
n=Locatecity(G,v1); m=Locatecity(G,v2);
cout<<"请输入对应的花费: \n";
scanf("%d",&cost);
ArcNode *p,*q,*t;
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=m;
p->info=cost;
p->nextarc=NULL;
if(G.ver[n].firstarc==NULL)
G.ver[n].firstarc=p;
else
{
q=G.ver[n].firstarc;
while(q->nextarc)
q=q->nextarc;
q->nextarc=p;
}//else
}//for
fin.close();

output(G);
KruskalminTree(G);
}



void output(ALGraph G)
{
int i,j;
ArcNode *p;
cout<<"有向图的邻接边的对应关系为:\n";
for(i=0;i<G.vexnum;i++)
{

if(G.ver[i].firstarc!=NULL)
{
p=G.ver[i].firstarc;
while(p)
{
cout<<p->adjvex<<" "<<p->info<<" ";
p=p->nextarc;
}//while
}//if
cout<<endl;
}//for
}

int Locatecity(ALGraph G,char v[]) //定位v在G中的位置的函数
{
int i;
for(i=0;i<G.vexnum;i++)
if(!strcmp(G.ver[i].name,v))
break;
return i;
}
下面是需要输入的数据,要求是以文件的形式输入
1 2 0.0403
1 3 0.4747
1 4 0.7015
1 5 0.7936
1 6 0.2973
1 7 0.6139
1 8 0.4494
1 9 0.3155
1 10 0.8558
1 11 0.272
1 12 0.1045
1 13 0.6548
1 14 0.9271
1 15 0.3229
2 1 0.2182
2 3 0.9329
2 4 0.9521
2 5 0.8128
2 6 0.4044
2 7 0.6619
2 8 0.6596
2 9 0.3007
2 10 0.7244
2 11 0.2316
2 12 0.01
2 13 0.915
2 14 0.0878
2 15 0.0984
3 1 0.967
3 2 0.8945
3 4 0.749
3 5 0.9038
3 6 0.3022
3 7 0.2
3 8 0.7532
3 9 0.042
3 10 0.1991
3 11 0.8995
3 12 0.0592
3 13 0.4332
3 14 0.3324
3 15 0.17
4 1 0.434
4 2 0.3857
4 3 0.5991
4 5 0.5404
4 6 0.7573
4 7 0.96
4 8 0.8047
4 9 0.5279
4 10 0.1573
4 11 0.9087
4 12 0.3227
4 13 0.2898
4 14 0.5262
4 15 0.3712
5 1 0.7848
5 2 0.2921
5 3 0.2336
5 4 0.5421
5 6 0.3597
5 7 0.6651
5 8 0.0292
5 9 0.256
5 10 0.3705
5 11 0.6036
5 12 0.7795
5 13 0.6319
5 14 0.2466
5 15 0.0398
6 1 0.5252
6 2 0.234
6 3 0.0323
6 4 0.2821
6 5 0.7084
6 7 0.5413
6 8 0.7798
6 9 0.4087
6 10 0.8623
6 11 0.3652
6 12 0.3355
6 13 0.2954
6 14 0.5429
6 15 0.7092
7 1 0.3313
7 2 0.2009
7 3 0.5799
7 4 0.2449
7 5 0.0432
7 6 0.6172
7 8 0.5674
7 9 0.9475
7 10 0.6848
7 11 0.5986
7 12 0.6196
7 13 0.622
7 14 0.7809
7 15 0.6413
8 1 0.4316
8 2 0.3803
8 3 0.8422
8 4 0.2863
8 5 0.1459
8 6 0.3555
8 7 0.557
8 9 0.9193
8 10 0.6342
8 11 0.6685
8 12 0.9929
8 13 0.0475
8 14 0.5219
8 15 0.1741
9 1 0.7179
9 2 0.5948
9 3 0.5569
9 4 0.9631
9 5 0.2333
9 6 0.3629
9 7 0.0214
9 8 0.2516
9 10 0.1413
9 11 0.8946
9 12 0.648
9 13 0.9946
9 14 0.9319
9 15 0.0622
10 1 0.9162
10 2 0.2684
10 3 0.8399
10 4 0.2307
10 5 0.2467
10 6 0.0685
10 7 0.4827
10 8 0.1335
10 9 0.5919
10 11 0.0873
10 12 0.5398
10 13 0.2068
10 14 0.1471
10 15 0.4067
11 1 0.89
11 2 0.6224
11 3 0.205
11 4 0.5373
11 5 0.1703
11 6 0.8672
11 7 0.808
11 8 0.5645
11 9 0.3597
11 10 0.8761
11 12 0.2323
11 13 0.6074
11 14 0.4168
11 15 0.4631
12 1 0.1347
12 2 0.8046
12 3 0.6213
12 4 0.205
12 5 0.2351
12 6 0.4579
12 7 0.736
12 8 0.541
12 9 0.7193
12 10 0.4204
12 11 0.4284
12 13 0.3476
12 14 0.2803
12 15 0.2027
13 1 0.1199
13 2 0.104
13 3 0.174
13 4 0.434
13 5 0.2755
13 6 0.0776
13 7 0.5723
13 8 0.0689
13 9 0.5236
13 10 0.4877
13 11 0.6172
13 12 0.889
13 14 0.5981
13 15 0.8695
14 1 0.8935
14 2 0.7292
14 3 0.2895
14 4 0.1422
14 5 0.9516
14 6 0.9049
14 7 0.009
14 8 0.9884
14 9 0.2608
14 10 0.4603
14 11 0.5589
14 12 0.8598
14 13 0.028
14 15 0.5979
15 1 0.6531
15 2 0.6486
15 3 0.0185
15 4 0.3756
15 5 0.3467
15 6 0.2817
15 7 0.7183
15 8 0.2511
15 9 0.4931
15 10 0.5157
15 11 0.2259
15 12 0.5971
15 13 0.0668
15 14 0.0637


...全文
78 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
vipcxj 2013-12-25
  • 打赏
  • 举报
回复
我记得boost有个图的库,里面什么最短路径啊乱七八糟的东西都有,还有各种算法。
七月的狮子 2013-12-25
  • 打赏
  • 举报
回复
没看懂是什么东西啊
oydxxynu 2013-12-25
  • 打赏
  • 举报
回复
如果有有向图的的最小生成树kruskal代码,那将感激不尽
oydxxynu 2013-12-25
  • 打赏
  • 举报
回复
求大神 给改一下
oydxxynu 2013-12-25
  • 打赏
  • 举报
回复
如果分不够可以追加分

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧