1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
void MiniSpanTree_Prim(MGraph G) { int min, lowcost[MAXVEX], adjvex[MAXVEX]; lowcost[0] = 0; for (int i = 1; i < G.numVertexes; i++) { lowcost[i] = G.arc[0][i]; adjvex[i] = 0; }
for (int i = 1; i < G.numVertexes; i++) { int j = 1, k = 0; min = 0x3f3f3f3f; while (j < G.numVertexes) { if (lowcost[j] != 0 && min > lowcost[j]) { min = lowcost[j]; k = j; } j++; } printf("(%d, %d), %d", adjvex[k], k, min); lowcost[k] = 0; for (int j = 1; j < G.numVertexes; j++) { if (lowcost[j] != 0 && lowcost[j] > G.arc[k][j]) { adjvex[j] = k; lowcost[j] = G.arc[k][j]; } } } }
|