Prim 算法代码例子
来源:互联网 发布:免费诊股软件 编辑:程序博客网 时间:2024/06/10 17:20
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
typedef int VRType;
typedef int InfoType;
typedef char VerTexType;
typedef struct ArcCell
{
VRType adj;
InfoType *info;
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
VerTexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum, arcnum;
}MGraph;
typedef struct
{
VerTexType adjvex;
VRType lowcost;
}closedge[MAX_VERTEX_NUM];
void CreateGraph(MGraph &G);
void MiniSpanTree_PRIM(MGraph G, VerTexType u);
int LocateVex(MGraph G, VerTexType u);
int minimum(closedge close);
void main( void )
{
int i, j;
MGraph G;
CreateGraph(G);
for(i = 0; i < G.vexnum; i++)
{
for(j = 0; j < G.vexnum; j++)
{
cout<<G.arcs[i][j].adj;
cout<<" ";
}
cout<<endl;
}
MiniSpanTree_PRIM(G, 'a');
}
void CreateGraph(MGraph &G)
{
int weigh;
int i, j = 0, k = 0;
char hand, tide;
cout<<"input the number for vexnum and arcnum:";
cin>>G.vexnum>>G.arcnum;
for(i = 0; i < G.vexnum; i++)
{
for(j = 0; j < G.vexnum; j++)
G.arcs[i][j].adj = 88;
}
cout<<endl;
cout<<"input"<<G.vexnum<<"char for vexs:";
for(i=0; i < G.vexnum; i++)
cin>>G.vexs[i];
cout<<endl;
cout<<"input"<<G.arcnum<<"arc(char,char,weigh):"<<endl;
j = 0;
k = 0;
for(i=0; i < G.arcnum; i++)
{
cout<<i<<":";
cin>>hand;
cin>>tide;
cin>>weigh;
while (hand != G.vexs[j])
j++;
while (tide != G.vexs[k])
k++;
G.arcs[j][k].adj = weigh;
G.arcs[k][j].adj = weigh;
j = 0;
k = 0;
cout<<endl;
}
}
void MiniSpanTree_PRIM(MGraph G,VerTexType u)
{
int i, j, k = 0;
closedge close;
k = LocateVex ( G, u );
for ( j = 0; j < G.vexnum; j++ )
{
if (j != k)
{
close[j].adjvex = G.vexs[k];
close[j].lowcost = G.arcs[k][j].adj;
}
}
close[j].lowcost = 88;
close[j].adjvex = '/0';
close[k].lowcost = 0;
close[k].adjvex = u;
for (i = 1; i < G.vexnum; i++)
{
k = minimum(close);
cout<<close[k].adjvex;
cout<<"---->";
cout<<G.vexs[k]<<" ";
cout<<close[k].lowcost<<endl;
close[k].lowcost = 0;
for (j=0; j<G.vexnum; j++)
{
if (G.arcs[k][j].adj < close[j].lowcost)
{
close[j].adjvex = G.vexs[k];
close[j].lowcost = G.arcs[k][j].adj;
}
}
}
}
int LocateVex(MGraph G, VerTexType u)
{
int k = 0;
while(G.vexs[k++] == u)
return k-1;
return 0;
}
int minimum(closedge close)
{
int j1=0, client = 88, j2;
while(close[j1].adjvex != '/0')
{
if (client > close[j1].lowcost && close[j1].lowcost != 0)
{
client = close[j1].lowcost;
j2 = j1;
}
j1++;
}
return j2;
}
输入输出实例:
input the number for vexnum and arcnum:6 10
input6char for vexs:a
b
c
d
e
f
input10arc(char,char,weigh):
0:a
b
6
1:b
c
5
2:a
c
1
3:a
d
5
4:c
d
5
5:b
e
3
6:c
e
6
7:c
f
4
8:e
f
6
9:d
f
2
88 6 1 5 88 88
6 88 5 88 3 88
1 5 88 5 6 4
5 88 5 88 88 2
88 3 6 88 88 6
88 88 4 2 6 88
a---->c 1
c---->f 4
f---->d 2
c---->b 5
b---->e 3
Press any key to continue
- Prim 算法代码例子
- Prim算法代码
- prim算法 伪代码
- prim算法简单代码
- 最小生成树的prim算法代码
- 经典Prim算法题目及代码
- prim 算法
- Prim算法
- Prim算法
- prim算法
- Prim算法
- Prim算法。
- Prim 算法
- Prim算法
- prim算法
- Prim算法
- Prim算法
- Prim算法
- 在Eclipse环境调试shark的JSPClient
- 上课有感!
- 如今学JAVA是越来越好找工作了
- [Python]用Python编写程序修复"恶邮差"变种U病毒感染文件。
- 中华人民共和国劳动法
- Prim 算法代码例子
- Wap push点对点短消息通知模式的实现
- 下面的例子将说明在applet中去调用javascript函数
- 转换linux到windows的文本文件,消除不可读小黑块
- 深圳市员工工资支付条例
- PHP编程:探索字串的奥秘
- InstallShield_X_10.0使用指南(全)
- 使用Linux备份Windows
- 项目开发总结(v1.0)