데이터 구조 - 그림 - 최소 생 성 트 리Prim

2478 단어
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXEDGE 20
#define MAXVEX 20
#define INFINITY 65535

typedef int Status;	/* Status      ,           , OK  */

typedef struct
{
	int arc[MAXVEX][MAXVEX];
	int numVertexes, numEdges;
}MGraph;

void CreateMGraph(MGraph *G)/*     */
{
	int i, j;
    
	/* printf("         :"); */
	G->numEdges=15;
	G->numVertexes=9;
    
	for (i = 0; i < G->numVertexes; i++)/*      */
	{
		for ( j = 0; j < G->numVertexes; j++)
		{
			if (i==j)
				G->arc[i][j]=0;
			else
				G->arc[i][j] = G->arc[j][i] = INFINITY;
		}
	}
    
	G->arc[0][1]=10;
	G->arc[0][5]=11;
	G->arc[1][2]=18;
	G->arc[1][8]=12;
	G->arc[1][6]=16;
	G->arc[2][8]=8;
	G->arc[2][3]=22;
	G->arc[3][8]=21;
	G->arc[3][6]=24;
	G->arc[3][7]=16;
	G->arc[3][4]=20;
	G->arc[4][7]=7;
	G->arc[4][5]=26;
	G->arc[5][6]=17;
	G->arc[6][7]=19;
    
	for(i = 0; i < G->numVertexes; i++)
	{
		for(j = i; j < G->numVertexes; j++)
		{
			G->arc[j][i] =G->arc[i][j];
		}
	}
    
}

/* Prim           */
void MiniSpanTree_Prim(MGraph G)
{
	int min, i, j, k;
	int adjvex[MAXVEX];		/*          */
	int lowcost[MAXVEX];	/*             */
	lowcost[0] = 0;/*          0, v0      */
    /* lowcost   0,                   */
	adjvex[0] = 0;			/*            0 */
	for(i = 1; i < G.numVertexes; i++)	/*       0       */
	{
		lowcost[i] = G.arc[0][i];	/*  v0              */
		adjvex[i] = 0;					/*      v0    */
	}
	for(i = 1; i < G.numVertexes; i++)
	{
		min = INFINITY;	/*         ∞, */
        /*              32767、65535  */
		j = 1;k = 0;
		while(j < G.numVertexes)	/*        */
		{
			if(lowcost[j]!=0 && lowcost[j] < min)/*       0     min */
			{
				min = lowcost[j];	/*             */
				k = j;			/*            k */
			}
			j++;
		}
		printf("(%d, %d)
", adjvex[k], k);/* */ lowcost[k] = 0;/* 0, */ for(j = 1; j < G.numVertexes; j++) /* */ { if(lowcost[j]!=0 && G.arc[k][j] < lowcost[j]) {/* k */ lowcost[j] = G.arc[k][j];/* lowcost */ adjvex[j] = k; /* k adjvex */ } } } } int main(void) { MGraph G; CreateMGraph(&G); MiniSpanTree_Prim(G); return 0; }

좋은 웹페이지 즐겨찾기