# 图的最小生成树

#include <stdio.h>
#include <stdlib.h>

#define QUEUE_MAXSIZE 30
#define VERTEX_MAX 26
#define MAXVALUE 99999

typedef struct
{
char Vertex[VERTEX_MAX];//保存顶点信息，序号或字母
int Edges[VERTEX_MAX][VERTEX_MAX];//保存边的权
int isTrav[VERTEX_MAX];//遍历标志
int VertexNum;//顶点的数量
int EdgeNum;//边的数量
int GraphType;//图的类型，0：无向图；1：有向图
}MatrixGraph;

void CreateMatrixGraph(MatrixGraph *G);//创建邻接矩阵图
void OutMatrix(MatrixGraph *G);//输出邻接矩阵

/************************************************************************

************************************************************************/
void CreateMatrixGraph(MatrixGraph *G)
{
int i, j, k, weight;
char start, end;

printf("输入各个顶点的信息\n");
for (i = 0; i < G->VertexNum; i++)
{
getchar();
printf("第%d个顶点：", i+1);
scanf("%c", &(G->Vertex[i]));
}

printf("输入构成各边的两个顶点及权值（用逗号分隔）:\n");
for (k = 0; k < G->EdgeNum; k++)
{
getchar();
printf("第%d条边:", k+1);
scanf("%c,%c,%d", &start, &end, &weight);
for (i = 0; start != G->Vertex[i]; i++);//在已有顶点中查找起始点
for (j = 0; end != G->Vertex[j]; j++);//在已有顶点中查找终点
G->Edges[i][j] = weight;
if (G->GraphType == 0)
G->Edges[j][i] = weight;
}
}
/************************************************************************

************************************************************************/
void OutMatrix(MatrixGraph *G)
{
int i, j;
for (j = 0; j < G->VertexNum; j++)
{
printf("\t%c", G->Vertex[j]);
}
printf("\n");
printf("      ------------------------------------\n");
for (i = 0; i < G->VertexNum; i++)
{
printf("    %c |", G->Vertex[i]);
for (j = 0; j < G->VertexNum; j++)
{
if (G->Edges[i][j] == MAXVALUE)
printf("\t∞");
else
printf("\t%d", G->Edges[i][j]);
}
printf("\n");
}
printf("\n");
}
/**********************************************************
*******上面是图的代码
*******下面是生成最小树的程序
***********************************************************/
/************************************************************************

************************************************************************/
#define USED 0
void Prim(MatrixGraph G)
{
int i, j, k, min, sum = 0;
int weight[VERTEX_MAX];
char tmpvertex[VERTEX_MAX];//临时顶点信息

for (i = 1; i < G.VertexNum; i++)
{
weight[i] = G.Edges[0][i];
if (weight[i] == MAXVALUE)
else
tmpvertex[i] = G.Vertex[0];
}
tmpvertex[0] = USED;
weight[0] = MAXVALUE;
for (i = 1; i < G.VertexNum; i++)
{
min = weight[0];
k = i;
for (j = 1; j < G.VertexNum; j++)
{
if (weight[j] < min && tmpvertex[j] != 0)
{
min = weight[j];
k = j;
}
}
sum += min;
printf("(%c,%c)", tmpvertex[k], G.Vertex[k]);
tmpvertex[k] = USED;
weight[k] = MAXVALUE;
for (j = 0; j < G.VertexNum; j++)
{
if (G.Edges[k][j] < weight[j] && tmpvertex[j] != 0)
{
weight[j] = G.Edges[k][j];
tmpvertex[j] = G.Vertex[k];
}
}
}
printf("\n最小生成树的总权值为:%d\n", sum);
}

/************************************************************************

************************************************************************/
int main()
{
MatrixGraph G;
int i, j;
char select;

do
{
printf("输入生成图的类型(0:无向图,1:有向图):");
scanf("%d", &G.GraphType);

printf("输入图的顶点数量和边数量:");
scanf("%d,%d", &G.VertexNum, &G.EdgeNum);

//清空矩阵
for (i = 0; i < G.VertexNum; i++)
for (j = 0; j < G.VertexNum; j++)
G.Edges[i][j] = MAXVALUE;

CreateMatrixGraph(&G);
printf("邻接矩阵数据如下:\n");
OutMatrix(&G);

printf("最小生成树的边如下：\n");
Prim(G);
printf("继续进行吗？(Y/N)");
scanf("%c", &select);
getchar();
}while (select != 'N' && select != 'n');

getchar();
getchar();
return 0;
}  

#### 最小生成树

2014-12-01 22:48:00

#### GraphMatrix：：图的定义

2015-06-02 14:02:58

#### Graph-Slam(一)

2016-09-11 14:13:29

#### 算法浅谈：图（下）

2018-01-29 08:24:24

#### <em>Graph</em>Maker V1.5

2018年03月21日 0B 下载

#### <em>Graph+</em>曲线图类

2017年08月02日 0B 下载

2017-07-11 07:43:39

#### algebraic <em>graph</em> theory

2018年05月04日 0B 下载

#### 图Graph

2017-03-06 15:56:24

#### 算法系列15天速成——第十五天 图【下】（大结局）

2017-03-22 17:00:42