void main()
	{
	
	FILE *graph=fopen("graph.txt","r");
	if (graph==NULL) { printf("graph File not found :graph.txt ");exit(0);}
	int n_vertices,length;	
	fscanf(graph," %d ",&n_vertices);	
	length=n_vertices*n_vertices;
	int *adj_matrix=(int *) malloc(sizeof(int)*length),*track_index=(int *) malloc(sizeof(int)*length);
	int i,j,src,dst,weight,arr;	
	for (i=0;i<length;i++)
		{
		adj_matrix[i]=INFY;
		}
	while(fscanf(graph,"%d\t%d\t %d\n",&src,&dst,&weight) == 3) 
			{	
			adj_matrix[(src-1)*n_vertices+(dst-1)]=weight;
			track_index[(src-1)*n_vertices+(dst-1)]=(src-1)*n_vertices+(dst-1);
			if (src==dst)
				{
				adj_matrix[(src-1)*n_vertices+(dst-1)]=INFY;
				}
			//printf("%d \n",(src-1)*n_vertices+(dst-1));
			}
	//for (i=0;i<n_vertices;i++) printf("%d \n",adj_matrix[i]);
		
	Build_min_heap(adj_matrix,track_index,length);
	int min_edge,edge_src,edge_dst,parent[n_vertices],src_root,dst_root,forest_edges=1;
	for (i=0;i<n_vertices;i++) parent[i]=0;
	printf("src\tdst\tweight \n");
	 while (forest_edges<n_vertices)
			{
			min_edge=Find_Min(array1,track_index,length);
			edge_src=min_edge/n_vertices;
			edge_dst=min_edge%n_vertices;
			src_root=find(parent,edge_src);
			dst_root=find(parent,edge_dst);
			if (Union(parent,src_root,dst_root))
				{
				printf("%d\t%d\t%d\n",edge_src,edge_dst,global_min);
				total_sum+=global_min;
				}
			forest_edges++;
			}

	printf("\nMST SUM IS :%d \n\n",total_sum);
	

	}
示例#2
0
main()
{
	int list[SIZE] = {-3,25,0,14,55,-9,1,43};
	int current;
	int index;
	int temp;

	for (index = 0; index < SIZE; index++)
	{
	   current = Find_Min(list, index, SIZE);
	      if (index != current)
	      {
	      temp = list[index];
	      list[index] = list[current];
	      list[current] = temp;
    	      }
 	}
	printf("\n");
	
	for (index = 0; index < SIZE; index++)
		printf("%d, ", list[index]);
	printf("\n\n");
}