Minimum spanning Tree prim algorithm plain version
There are a few points to explain.
1, 2 for loops start with 2, because we usually start by default to add the first node to the build tree, so we don't need to look for it again.
2, Lowcost[i] records the minimum edge weights with node I as the endpoint. Because the first node is added to the spanning tree by default, lowcost[i] = Graph[1][i], that is, the minimum edge weight value is the Benquan value of nodes to node 1th.
3, Mst[i] record is lowcost[i] corresponding starting point, so there is a beginning, there is an end, you can only determine a side. Initialize Mst[i] = 1, that is, each edge is starting from node 1th.
Writing Program: For the following a weighted non-direction graph, given the number of nodes and all the edge weights, using prim algorithm to find the minimum spanning tree.
Enter data:
7 11
A B 7
A D 5
B C 8
B D 9
B E 7
C E 5
D E 15
D F 6
E F 8
E G 9
F G 11
Output:
A-d: 5
D-f: 6
A-b: 7
B-E: 7
E-C: 5
E-G: 9
total:39
Minimum spanning tree prim algorithm naive version C language implementation code is as follows
#include <stdio.h> #include <stdlib.h> #define MAX #define MAXCOST 0x7fffffff int Graph[max][max];
int Prim (int graph[][max], int n) {/* Lowcost[i] records the minimum weight of an edge at the end of I, when the lowcost[i]=0 indicates the endpoint I join the spanning tree/int Lowcost[max];
/* Mst[i] records The starting point of the corresponding lowcost[i], when mst[i]=0 indicates the start I join the spanning tree/int Mst[max];
int I, J, Min, minid, sum = 0;
/* Default selection of node 1th to join the spanning tree, starting from node 2nd to initialize/for (i = 2; I <= n; i++) {/* The distance from the shortest distance to the other node to the 1th node/lowcost[i] = Graph[1][i];
/* Mark the starting point of all nodes is the default 1th number node/mst[i] = 1;
}/* Tag 1th node added to the spanning tree/mst[1] = 0;
/* N nodes need at least n-1 to constitute the minimum spanning tree/for (i = 2; I <= n; i++) {min = Maxcost;
MiniD = 0; * * Find the minimum weighted edge of the node MiniD * * for (j = 2; J <= N; j +) {/* Edge weights are small and not in the spanning tree/if (lowcost[j) < min && Lowcos
T[J]!= 0) {min = lowcost[j];
MiniD = j;
}/* Output information to generate the tree edge: Start, end, weight/printf ("%c-%c:%d\n", Mst[minid] + ' A '-1, MiniD + ' a '-1, min);
/* Cumulative weight/sum = min;
/* Tag node MiniD Add the Spanning tree * * Lowcost[minid] = 0; /* Update the weights of the current node MiniD to other nodes * for (j = 2; J <= N; j +) {/* Find smaller weights */if (Graph[minid][j] < Lowcost[j]) {/* Update weight information/* LOWCOST[J]
= Graph[minid][j];
/* Update the minimum weight edge of the starting point */mst[j] = MiniD;
}}/* Returns the minimum weight and/or return sum;
int main () {int I, j, K, M, N;
int x, y, cost;
Char Chx, Chy;
/* Read the number of nodes and edges * * SCANF ("%d%d", &m, &n);
GetChar ();
/* initialization diagram, the distance between all nodes is infinity/for (i = 1; I <= m i++) {for (j = 1; j <= m; j) {graph[i][j] = Maxcost;
}/* Read Edge Info * for (k = 0; k < n; k++) {scanf ("%c%c%d", &chx, &chy, &cost);
GetChar ();
i = Chx-' A ' + 1;
j = chy-' A ' + 1;
GRAPH[I][J] = cost;
Graph[j][i] = cost;
/* Solve minimum spanning tree/cost = Prim (graph, M);
/* Output minimum weights and/or printf ("total:%d\n", cost);
System ("pause");
return 0;
}
Kruskal algorithm:
void Kruskal (Edge e[],int n,int E)
{
int i,j,m1,m2,sn1,sn2,k;
int vset[maxe];
for (i=0;i<n;i++) vset[i]=i; Initialize the auxiliary array
k=1; K represents the first few edges of the currently constructed minimum spanning tree, with an initial value of 1
j=0; The subscript of the middle edge of E, the initial value is 0
while (k<n) ///The generated number of edges is less than n when the loop
{
m1=e[j].u;m2=e[j].v; Take the head and tail vertex of an edge sn1=vset[m1];sn2=vset[m2]///////////////////// Two vertices belong to a different set, which is an edge of the minimum spanning tree (SN1!=SN2).
{
printf ("(%d,%d):%d/n", M1,M2,E[J].W);
k++; Generation Edge Number 1
for (i=0;i<n;i++) //Two set uniform number
if (VSET[I]==SN2) //Set number is SN2 to SN1
VSET[I]=SN1 ;
}
j + +; Scan next side
}
}