Preface
Kruskal is one of the algorithms for generating a minimal spanning tree in a graph (graph theory). (There is also the prim algorithm, which will be involved later) This involves the concept of the smallest spanning tree, in fact, is the smallest total weight of a connected non-loop sub-graph. (combined with the following schematic diagram is not difficult to understand) the code here does not use the graph storage structure (such as: Matrix, adjacency linked list, etc.) to handle and use this algorithm, but the simplest ternary input, this will make this process a lot easier, as to how the graph is stored, in the later summary of the graph data structure will be discussed.
the thought and process of Kruskal algorithm
(1) Thought: In fact, the essence of this algorithm is still a greedy algorithm process. In fact, we can think of this, a figure, we want to make the generated sub-graph (more specifically, the tree) the total weight of the smallest, then as long as the lowest weight in the graph to select the edge, the weight of the small edge 、......, so naturally guaranteed to generate the lowest total weight value. But don't forget that the generated sub-graph we asked for is also a tree (the definition of a tree: a graph with no loops connected), which brings up a problem, when we choose the edge from small to large, it may cause the generated sub-graph to produce a loop, which does not conform to the concept, So we need not only the weights from small to large selection edge should also ensure that the sub-graph composed of these edges is not a loop . This process is not the greedy choice process.
Process (standard definition):
To a connected network with n vertices n = {V, E}, first constructs a graph T = {V, empty set} consisting of these n vertices, without any edges, where each vertex becomes a connected component. An edge with the smallest weight is removed from e continuously (if there are more than one), and if the two endpoints of the edge come from different connected components, the edge is added to T. This is repeated until all vertices are on the same connected component.
Why the Kruskal algorithm is implemented by using the set and the minimum heap.
The most fundamental solution to this problem is the implementation of the Kruskal algorithm, which involves two important steps in the process:
(1) The edge with the least weight per acquisition
(2) Determine whether the two end points of the joined edges are from the same connected component (essentially guaranteeing that no loops are generated after joining the edge)
and the smallest heap can be achieved every time a set of data in the key code (here can represent the weight of the size) of the smallest data; You can combine different sets (which can represent a collection of points) and you can tell whether different sets have intersections (it is possible to determine whether two points belong to a set, and then determine if the loop will occur after joining the edge.) )。 That's right. This is simply the Kruskal algorithm implementation of the standard, the following Kruskal algorithm implementation process diagram also embodies this point. (Note: The previous blog post has a summary of the minimum heap and the set, which can be seen if the two data structures are unclear)
The Kruskal algorithm generates an instance of the smallest spanning tree:
Program input data (ternary format):
7 9//The number of vertices in the graph, the number of edges of the graph
0 1 28//each row below stores information for one edge
0 5 10//indicates: End of Edge 1, end of Edge 2, weight of edge
1 2 16
1 6 14
2 3 12
3 4 22
3 6 18
4 5 25
4 6 24
Figure shows:
The number in the node indicates the ordinal or number of the node, and the number on the edge represents the weight of each edge, and you should not dwell on the weight of each edge and its ratio on the graph is not coordinated. Because there are many meanings of weights in real environment, such as cost, cost, importance and so on.
Kruskal implementation Process diagram:
Very clear and understandable illustrations.
the structural attention points during code implementation.
(1) Construction of the edge structure body:
This must be overloaded for this struct variable greater than, greater than, or less than operator (only through the weight of the edge), and it should be compared directly to this variable in the smallest heap.
(2) Kruskal algorithm implementation function:
In particular, it should be noted that when the set is set up, its constructors pass the number of vertices 1 more, otherwise this can lead to a serious error (or a hidden danger). This is explained in another article.
(3) About output:
This is just the output of the total weight of the smallest spanning tree, and other indicators can be obtained by the global array that holds all the edges of the most niche tree.
Kruskal.cpp Code
/* * Implement Kruskal algorithm using and checking and minimum heap */#include "UFSets.h" #include "heap.h" using namespace std; struct WEDGE {//weight edge structure definition int vertex1, VERTEX2; Determines the two vertices of an edge int weights;
The weight//constructor of the Edge WEDGE (int v1 =-1, int v2 =-1, int w = 0) {vertex1 = V1;
Vertex2 = v2;
weights = W; } bool operator > (wedge& WE); Heavy-duty edge greater than comparison bool operator < (wedge& WE); The overloaded edge is less than the comparison bool operator >= (wedge& WE);
Overload greater than equals comparison}; WEDGE MINTREEEDGES[20];
The edge set that stores the minimum spanning tree is void Kruskal (wedge* we, int edg_amou, int vex_amou); int main () {int vertexsamount{0}, edgesamount{0}; Enter the number of points and WEDGE wedge[20];
Initialize 20 sides of Cin >> Vertexsamount >> Edgesamount; for (int i = 0; i < edgesamount; i + +) {//Loop input edge vertex and weight cin >> wedge[i].vertex1 >> Wedge[i].verte X2 >> WEdge[i].weights;
} Kruskal (Wedge, Edgesamount, vertexsamount);
The total weight of the output minimum spanning tree int sumweights{0};
for (int j = 1; j < Vertexsamount; J + +) {sumweights + = mintreeedges[j].weights;
} cout << sumweights << Endl;
System ("pause");
return 0; } void Kruskal (wedge* we, int edg_amou, int vex_amou) {//kruskal algorithm implementation function, parameters are: A set of edge sets, number of edges, number of vertices minheap<wedge> m Inheap (We, Edg_amou); Establish minimum heap ufsets uf_set (Vex_amou + 1);
Set up and check the WEDGE edge;
int vex_1{0}, vex_2{0}; int count{1}; The minimum spanning tree joins the edge count while (Count < Vex_amou) {//Select the total number of edges-1 edges to minheap.removemin (edge); Delete and return the top element of the heap vex_1 = Uf_set.
Find (EDGE.VERTEX1); Vex_2 = Uf_set.
Find (EDGE.VERTEX2); if (vex_1! = vex_2) {//Two vertices are not in the same connected component mintreeedges[count] = edge; SELECT INTO Uf_set. Union (Vex_1, vex_2); //Connect two vertices to count++;
}}}//overloaded operator definition bool Wedge::operator > (wedge& WE) {if (Weights > We.weights) {return true;
} else {return false;
}} bool wedge::operator< (wedge& WE) {if (Weights < we.weights) {return true;
} else {return false;
}} bool Wedge::operator>= (wedge& WE) {if (weights >= we.weights) {return true;
} else {return false; }
}
The code in this file needs to rely on the header files of the two data structures-the minimum heap and the set. H. It is not posted here, you can use the link at the end of the article to download, you can also see the two data structures before the summary of the code posted. Let's test the example above with this code.
Well, he's doing well, in our process diagram, the final weights and: 10+12+14+16+22+25 = 99.
Alternative testing ...
But this is just a group of test data, we should find a group of completely unfamiliar data to try to see the following topic: (The topic is in the site to pick down ~)
The diagram of data structure experiment six: Village Road
Time limit:1000ms Memory limit:65536kb
Submit statistic
Problem Description
The current rural highway construction is in full swing, a township government decided to achieve village road, engineers existing villages between the original road statistics table,
The table lists the costs of several roads that can be built between villages, and your task is to make every village have the minimum cost required for road connectivity, based on the data tables given.
Input
Consecutive sets of data input, each group of data including the number of villages N (n <= 1000) and the number of roads to choose m (M <= 3000), followed by M-Line M Road,
Each line gives 3 positive integers, the number of the two villages directly connected to the road, and the budgeted cost of constructing the road, numbered from the 1~n village.
Output
The output allows each village to have the minimum cost of road connectivity, and if the input data does not allow all villages to flow, the output is 1, indicating that there is no road connection between some villages.
Example Input
5 8
1 2 12
1 3 9
1 4 11
1 5 3
3 2 6
2 4 9
3 4 4
5 4 6
Example Output
19
Author
Xam
This is a typical application problem, look at the following test results.
Oh! The no! problem seems to be very serious. The program crashed. Is this a problem with our program? or other bugs. Perhaps not, you can look at the subtle differences between the two sets of input data that might be inspired. (In fact, I have already made a hint above) I guarantee this Kruskal algorithm implementation is no problem, the next post will uncover the mystery of this hidden trouble.
header File Download link:
Link: https://pan.baidu.com/s/1eROKT3c Password: 9613