图算法--最小生成树(1)prim

本文介绍使用Prim算法求解最小生成树的过程。通过初始化图结构并定义关键数据结构,如mst_parent、min_distance及known数组,实现Prim算法的核心逻辑。文章详细展示了算法流程,包括如何选择加入最小生成树的边。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
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)
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值