The spanning tree of a connected graph with n nodes is a minimal connected sub-graph of the original, and contains all n nodes in the original image, and has the least number of edges to keep the graph connected. The minimum spanning tree can be calculated using the Kruskal (Kruskal) algorithm or the prim (PRIMM) algorithm.
Kruskal (Kruskal) algorithm (only for edge correlation)
Algorithm Description: The Kruskal algorithm needs to access the edges of the graph, so the time complexity of the Kruskal algorithm is only related to the edge and can prove the time complexity of O (Eloge).
Algorithm process:
1. Sort the edges of the graph according to weights
2. Traverse the graph once to find the edge with the least weight, (condition: The edge of this finding cannot be a ring with the edges that have been added to the minimum spanning tree collection), and if so, the minimum spanning tree is added. The non-conforming condition continues to traverse the graph, looking for the next least-weighted edge.
3. Recursively repeat step 1 until you find the n-1 edge (the graph has n nodes, the minimum spanning tree should be the n-1 bar), the algorithm ends. The smallest spanning tree of this graph is obtained.
The Kruskal (Kruskal) algorithm is suitable for the smallest spanning tree of sparse graphs because it is only related to edges. The prime algorithm is suitable for finding the smallest spanning tree of dense graphs because it is only related to vertices.
#include <iostream> #include <cstring> #include <string> #include <cstdio> #include < algorithm>using namespace std; #define MAX 1000int Father[max], Son[max];int V, l;typedef struct Kruskal//storage edge Information {int A; int b;int value;}; BOOL CMP (const Kruskal & A, const Kruskal & B) {return a.value < B.value;} int unionsearch (int x)//Find root node + path compression {return x = = Father[x]? X:unionsearch (Father[x]);} BOOL Join (int x, int y)//merge {int root1, root2;root1 = Unionsearch (x); root2 = Unionsearch (y); if (root1 = = Root2)//For ring return False;else if (son[root1] >= Son[root2]) {Father[root2] = root1;son[root1] + = Son[root2];} ELSE{FATHER[ROOT1] = Root2;son[root2] + = son[root1];} return true;} int main () {int ncase, ltotal, SUM, flag; Kruskal edge[max];scanf ("%d", &ncase), while (ncase--) {scanf ("%d%d", &v, &l), ltotal = 0, sum = 0, flag = 0;for (i NT i = 1; I <= v; ++I)//Initialize {Father[i] = i;son[i] = 1;} for (int i = 1; I <= l; ++i) {scanf ("%d%d%d", &edge[i].a, &edge[i].b, &eDge[i].value);} Sort (Edge + 1, Edge + 1 + L, CMP); The weights are ordered from small to large for (int i = 1; I <= l; ++i) {if (Join (EDGE[I].A, edge[i].b)) {ltotal++;//number of edges plus 1sum + = Edge[i].value;//Record weight value and cou t<<edge[i].a<< "and" <<EDGE[I].B<<ENDL;} if (ltotal = = V-1)//minimum spanning tree condition: number of edges = number of vertices -1{flag = 1;break;}} if (flag) printf ("%d\n", sum), else printf ("Data error.\n");} return 0;}
Premium Manaus (Prime) algorithm (vertex-dependent only)
Algorithm Description:
Premium Manaus algorithm for the minimum spanning tree, and the number of side-independent, only and fixed-point amount of correlation, so suitable for dense network of the smallest spanning tree, the time complexity of O (n*n).
Algorithm process:
1. Divide the vertices of a graph into two parts, a node in the smallest spanning tree (a collection), and an unhandled node (collection B).
2. First select a node, add this node to a, and then, on the vertex in set a traversal, find the vertex associated with a in the Benquan value of the smallest (set to V), delete this vertex from B, added to the collection A.
3. Recursively repeat step 2 until the node in the B collection is empty, ending this process.
The nodes in the 4.A collection are the nodes of the smallest spanning tree obtained by the prime algorithm, and the vertices are connected according to the nodes in step 2, resulting in the smallest spanning tree of the graph.
The algorithm implements the specific process:
1. Place the first point into the collection of the smallest spanning tree (the tag visit[i]=1 means the minimum spanning tree collection).
2. Starting from the second point, initialize Lowcost[i] to the weighted value of the Edge (Lowcost[i) that is connected to the 1 point (only connected) is not the minimum weight of this point! will be updated incrementally later).
3. Find the edge of the minimum weight.
From the 2nd iteration, if it is not a point of the minimum spanning tree collection, find the minimum weight from 2 to N (Lowcost[j]).
4. Add the vertices of the least-weighted edge to the set of the smallest spanning tree (Mark visit[i] = 1), and the weights want to be added.
5. Update Lowcost[j] collection.
Assuming that the first time: Lowcost[2] represents the weighted value of a point connected to 1, the K point is now added. Compare the size of the K-point with the 2-point edge map[k][2] and lowcost[2], if LOWCOST[2] is large, then lowcost[2] = map[k][2]. (Key step: The essence is that each add a point to the minimum spanning tree collection, you need to compare this point to the point outside the set, constantly looking for the smallest edge between the two sets)
6. Cycle through the above steps to guide the addition of all vertices to the minimum spanning tree collection.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm>using namespace std; #define INF 0x3f3f3f3f#define maxn 110int map[maxn][maxn], Lowcost[maxn];bool visit[ Maxn];int Nodenum, Sum;void Prim () {int temp, k;sum = 0;memset (visit, false, sizeof (visit));//Initialize visitvisit[1] = true;for ( int i = 1; I <= nodenum; ++I)//Initialize lowcost[i]lowcost[i] = map[1][i];for (int i = 1; I <= nodenum; ++i)//Find the edge of the spanning tree collection point set connected to the minimum weight {temp = inf;for (int j = 1 ; J <= Nodenum; ++J) if (!visit[j] && temp > lowcost[j]) temp = Lowcost[k = j];if (temp = = INF) Break;visit[k] = true; Add minimum spanning tree set sum + = temp;//record weights sum for (int j = 1; j <= Nodenum; ++j)//update lowcost array if (!visit[j] && lowcost[j] > m AP[K][J]) lowcost[j] = Map[k][j];}} int main () {int A, B, cost, Edgenum;while (scanf ("%d", &nodenum) && nodenum) {memset (map, INF, sizeof (map)); Edgenum = Nodenum * (nodenum-1)/2;for (int i = 1; I <= edgenum; ++i)//input edge information {scanf ("%d%d%d", &a, &b, &cost), if (Cost < map[a][b]) map[a][b] = map[b][a] = Cost;} Prim ();p rintf ("%d\n", sum); The sum of the minimum spanning tree weights is}return 0;}
Minimum spanning tree algorithm