/*
file: mst_prim.c
brief:最小生成树之prim算法
note:在prim中,集合仅形成单棵树,
添加入集合的安全边总是连接树与一个不在树中的顶点的最小权边
version:1.0
auther:yejing@14.03.24,updated@14.08.27
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_VEX 255
#define INFINITY 0x7fffffff;
typedef _graph_t{
int vex;
int arc[MAX_VEX][MAX_VEX];
int* mst_parent; //parent of node
int* min_distance;//min distance to current mst
int* known;// known or not
}graph_t;
graph_t creat_graph(void){
int i;
graph_t graph;
memset((char*)&graph, 0, sizeof(graph));
printf("please input the vex of the graph: ");
scanf("%d", &(graph.vex));
getchar();
printf("initializing the vectors\n");
graph.mst_parent = (int*)malloc(sizeof(int) * graph.vex);
graph.min_distance = (int*)malloc(sizeof(int) * graph.vex);
graph.known = (int*)malloc(sizeof(int) * graph.vex);
for(i = 0; i < graph.vex; ++i){
graph.mst_parent[i] = -1;
graph.min_distance[i] = INFINITY;
graph.known[i] = 0;
}
}
void mstprim_main(graph_t g, int s){
int i = 0, j = 0;
graph.min_distance[s] = 0;
graph.known[s] = 1;
//遍历所有的顶点,找到到已知顶点s的最近的点,s已经known了,这个循环是每次找到mst的一条边
for(i = 1; i < graph.vex; ++i){
int min = INFINITY;
for(k = 0; k = graph.vex; ++k){
if(graph.known[k])
for(j = 0; j < graph.vex; ++j){
if(!graph.known[j] && d[j] > graph.arc[s][j])
d[i] =
}
}
}
}
int main(int argc, char* argv[]){
graph_t graph;
graph = creat_graph();
mstprim_main(graph, 0)
}