#include
#include
#include
using namespace std;
#define MAXVEX 100
#define INF 100000000
#define MAXE 100
typedef char VertexType[10];
typedef struct vertex
{
int adjvex;
VertexType data;
} VType;
typedef struct graph
{
int n, e;
VType vexs[MAXVEX];
int edges[MAXVEX][MAXVEX];
}MatGraph;
typedef struct
{
int u;
int v;
int w;
}Edge;
void CreateGraph(MatGraph& g, int A[][MAXVEX], int n, int e)
{
int i, j;
g.n = n;
g.e = e;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
g.edges[i][j] = A[i][j];
}
}
}
void DestroyGraph(MatGraph g)
{
}
void DispGraph(MatGraph g)
{
int i, j;
for (i = 0; i < g.n; i++)
{
for (j = 0; j < g.n; j++)
{
if (g.edges[i][j] < INF)
{
printf("%4d", g.edges[i][j]);
}
else
{
printf("%4s", "∞");
}
}
printf("n");
}
}
void Prim(MatGraph g, int v)
{
int lowcost[MAXVEX];
int mincost;
int closest[MAXVEX],i,j,k;
for(j=0;j= 0 && temp.w < E[j].w)
{
E[j + 1] = E[j];
j--;
}
E[j + 1] = temp;
}
}
int main()
{
MatGraph g;
int n = 7, e = 7;
int A[MAXVEX][MAXVEX] = { {0,2,5,3,INF,INF,INF},{INF,0,2,INF,INF,8,INF},{INF,INF,0,1,3,5,INF},
{INF,INF,INF,0,5,INF,INF},{INF,INF,INF,INF,0,3,9},{INF,INF,INF,INF,INF,0,5},{INF,INF,INF,INF,INF,INF,0} };
CreateGraph(g, A, n, e);
printf("图形n");
DispGraph(g);
printf("Prim:得到的结果n");
Prim(g, 0);
DestroyGraph(g);
}