作业帮 > 数学 > 作业

实现prim算法或kruscal算法中的一种最小生成树算法

来源:学生作业帮 编辑:作业帮 分类:数学作业 时间:2024/05/21 06:22:26
实现prim算法或kruscal算法中的一种最小生成树算法
Prim算法:
#include
#include
typedef int VRType;
typedef char InfoType;
#define MAX_NAME 3
/*顶点字符串的最大长度+1*/
#define MAX_INFO 20
/*相关信息字符串的最大长度+1*/
typedef char VertexType[MAX_NAME];
#define INFINITY 32767
/*用整型最大值代替无穷大*/
#define MAX_VERTEX_NUM 20
/*最大顶点个数*/
typedef enum{DG,DN,AG,AN} GraphKind;
/*{有向图,有向网,无向图,无向网}*/
typedef int PathMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef int ShortPathTable[MAX_VERTEX_NUM];
typedef struct
{
VRType adj;
/*顶点关系类型.对无权图,用1(是)或0(否)表示相邻否*/
/*对带全图,则为权值类型*/
InfoType *info;
/*该弧相关信息的指针(可无)*/
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
VertexType vexs[MAX_VERTEX_NUM];
/*顶点向量*/
AdjMatrix arcs;
/*邻接矩阵*/
int vexnum,arcnum;
/*图的当前顶点数和弧数*/
GraphKind kind;
/*图的种类标志*/
}MGraph;
int LocateVex(MGraph G,VertexType u)
{ /*初始条件:图G存在,u和G中顶点有相同特征*/
/*操作结果:若G中存在顶点u,则返回该顶点在图中位置;否则返回-1*/
int i;
for(i=0;i