-
Hehe, I've been working on this stuff for the past two days, and it just happens to be someone else that I read from the Internet, I think it's very good, the idea is relatively clear, and the data structure design is also very good, it's easy to understand the prim algorithm includeusing namespace std; #define maxvex 10#define max 65000typedef char vextype;typedef float adjtype;struct graphmatrix; struct edge; void edgecopy(edge *to,edge *from) void prim(graphmatrix *pgraph,edge *mst) for(i=0;in-1;++i)
-
Minimal spanning treePractical problems applied to graph theory knowledge. Spanning trees and minimal spanning trees have many important applications.
For example, if you want to lay fiber optic cables between n cities, the main goal is to make communication between any two of these n cities, but the cost of laying fiber optic cables is very high, and the cost of laying fiber optic cables varies between cities, so the other goal is to minimize the total cost of laying fiber optic cables. This requires finding the minimum weighted spanning tree.
How the spanning tree protocol works:If the root bridge is reached in any switch.
There are two or more links. The spanning tree protocol keeps only one switch and cuts off the others, so as to ensure that there is only a single active link between any two switches. Because of this topology that is generated.
It is much like a tree structure with the root switch as the trunk, so it is called the spanning tree protocol.
The above content refers to: Encyclopedia - Minimum Spanning Tree.
For the above content, please refer to: Encyclopedia-Spanning Tree Protocol.
-
Here are some solutions to the minimum spanning tree.
The basic idea of the Prime algorithm.
1.Clear the spanning tree, and take any vertex to join the Shengzhou Zhengcheng tree.
2.In those edges where one endpoint is in the spanning tree and the other endpoint is not in the living tree, select the edge with the least weight and add it and the other endpoint to the spanning tree.
3.Repeat step 2 until all vertices have entered the spanning tree, which is the smallest spanning tree.
Kruskal algorithm: Construct a subgraph with only n vertices and an empty edge set, if each vertex in the subgraph is regarded as the root node of each tree, it is a forest with n trees.
Then, an edge with the smallest weight is selected from the edge set of the net, and if the two vertices of the edge belong to different trees, it is added to the subgraph, that is, the two trees where the two vertices are located are combined into a tree. Conversely, if the two vertices of the edge have fallen on the same tree, it is not advisable to take the edge with the smallest weight. And so on until there is only one tree in the forest.
The Kruskal algorithm can be implemented very quickly on the basis of parallel sets. Combined with examples, the specific algorithm implementation is introduced (the part of the merging and querying can be found in the introduction of the merging and querying part).
The concept of spanning tree: a subgraph of a connected graph g If it is a tree that contains a vertex of the photographed trace of g, then the subgraph is called a spanning tree of g A spanning tree is a very small connected subgraph of a connected graph. The so-called minimum:
If you add an edge to the tree at random, a loop will appear; If you remove an edge, you will make it program a disconnected graph. The sum of the weights of the sides of the spanning tree is called the weights of the geneuloses. The spanning tree with the least weight is called the minimum spanning tree, and the commonly used algorithms are the prime algorithm and the kruskal algorithm.
-
Solve the minimum spanning tree.
The methods are as follows:
Connected graph: In an undirected graph, if any two vertices VI and VJ have paths that communicate with each other, the undirected graph is said to be a connected graph.
Strong connectivity diagrams. In a directed graph, if any two vertices VI and VJ have paths in common, the directed graph is said to be strongly connected.
Connected network: In a connected graph, if the edges of the graph have a certain meaning, each edge corresponds to a number, which is called a weight; The right represents the cost of connecting vertices, and this kind of connectivity graph is called a connectivity network.
Spanning Tree: A spanning tree of a connected graph is a connected subgraph that contains all n vertices in the graph, but only enough n-1 edges to form a tree. A spanning tree with n vertices has and only n-1 edges, and if another edge is added to the spanning tree, it must be looped.
Minimum spanning tree: The cost of all edges and the smallest spanning tree in all spanning trees of a connected network are called minimum spanning trees.
-
In a given undirected connectivity graph g = v, e), (u, v) represents the edge connecting vertex u to vertex v, and w(u, v) represents the weight of this edge; If there is a subset of t is e, g'=v , t) is the spanning tree of g, such that w(t) is the smallest, then t is the minimum spanning tree of g.
Minimum Spanning Tree is actually an abbreviation for Minimum Weight Spanning Tree.
To build a communication network in n cities, it is necessary to arrange n-1 communication lines in a cluster to connect these n cities, and consider how to build this communication network at the lowest cost?
Therefore, the connectivity graph can be introduced to solve the above problem, n cities are the n vertices on the graph, and then, the edge represents the communication lines of two cities, and the weight on each edge is the cost required to build this line, so now the connectivity network with n vertices can establish different spanning trees, each spanning tree can be used as a communication network, when we construct this connectivity network The cost is the smallest, and the spanning tree of the connected network is called the minimum spanning tree.
g (v,e) is a weighted connected undirected graph, u is a non-empty subset of the vertex set v, and if (u,v) is an edge with the least weight, where u u, v v-u, then there must be a minimum spanning tree containing edges (u,v).
Algorithm process:Weighted connectivity undirected concession diagram g (v,e).
Algorithm process:The weighted connected undirected graph g (v,e) and kruskal areIn ascending order of weightSelectAppropriate edgesto construct a minimum spanning tree.
Minimal spanning tree
PRIM algorithm.
Kruskal algorithm.
-
See Greedy Algorithm - Minimum Spanning Tree Algorithm.
Greedy Strategy: Understanding of the Algorithm:
Cyclic invariant: key mu.
Before each loop, a is a subset of a certain minimum spanning tree.
Safe edge: An edge that satisfies the following conditions is called a safe edge.
Add the edges (u, v) to the set a so that a does not violate the cyclic invariant, i.e. au is also a subset of the smallest spanning tree.
Rules for identifying safe edges in greedy strategies:
A corollary:If the optimal solution of a problem contains the optimal solution of the sub-problem, then the problem has an optimal substructure.
The minimum spanning tree satisfies the optimal substructure, which is proved below:
-
The so-called minimum spanning tree is a weighted connected graph g with n vertices, if there is a subgraph g', which contains all the vertices and a subset of the edges in graph g, does not form a loop, and is a subgraph g'The sum of the weights of each side is the smallest, and it is called g'is the minimum spanning tree for Figure g. From the definition, we can see three properties of the smallest spanning tree:
The minimum spanning tree cannot have loops.
The minimum spanning tree may be one, or it may be many.
The minimum number of spanning tree edges is equal to the number of vertices minus one. In this article, we will introduce two minimum spanning tree algorithms, namely the Kruskal algorithm and the Prim algorithm.
The core idea of Kruskal's algorithm is that in a weighted connected graph, the smallest edge is continuously found in the set of edges, and if the edge satisfies the conditions for obtaining a minimum spanning tree, it is constructed until a minimum spanning tree is finally obtained.
Execution steps of the Kruczcard algorithm:
Step 1: In the weighted connected graph, sort the weights of the edges.
Step 2: Determine whether you need to select this edge (at this time, the edges in the graph have been arranged in order of weights from smallest to largest). The basis for judging is whether the two vertices of the edge are connected or not, and if they are connected, they will continue to the next one; If it's not connected, then choose to make it connected.
Step 3: Loop the second step until all the vertices in the graph are in the same connected component, i.e., the minimum spanning tree is obtained.
Below I will use the diagram to illustrate the workflow of the Kruskal algorithm, as shown in the following figure:
-
Examples of the practical application of the minimum spanning tree are as follows:
Kruskal algorithm, process description: always take the edge as the dominant position, first select the edge with the smallest weight, always select the currently available minimum weight edge, and judge whether the two points have been indirectly connected each time, if they have been indirectly connected, then skip this edge. The time complexity is o(n*logn), which is suitable for finding the minimum spanning tree of edge sparsely connected networks.
PRIM algorithm, process description: The PRIM algorithm is always dominated by vertices, and the choice of starting point is arbitrary. Select the edge with the lowest weight from the starting point to the other points, and then find the edge with the minimum weight at each of the two vertices of this edge, and skip the edges that have also been indirectly connected.
The time complexity is o(n2), which is suitable for finding the minimum spanning tree of the edge-dense connected network.
To lay fiber optic cables between n cities, the main goal is to make communication between any two of these n cities, but the cost of laying fiber optic cables is high and the cost of laying fiber optic cables varies between cities, so another goal is to minimize the total cost of laying fiber optic cables. This requires finding the minimum weighted spanning tree.
Brief description of minimum spanning tree properties and algorithms:
Minimum spanning tree properties: Let g=(v,e) be a connected network and u be a non-null true subset of the vertex set v. If (u,v) is a "point in g" an endpoint bend in u (e.g. . .
u u), another edge whose endpoint is not in u (e.g., v v-u), and (u,v) has the smallest weight, then there must be a minimum spanning tree of g that includes this edge (u,v).
The vertices in the set u are considered red vertices, the vertices in v-u are considered blue vertices, the edge connecting the red and blue dots is considered the purple edge, and the purple edge with the least weight is called the light edge (i.e., the edge with the lightest weight). Thus, the edges (u,v) described in the MST properties can be shortened to light edges.
The general algorithm for finding MST can be described as follows: for graph g, starting from the empty tree t, selecting and adding n-1 safe edges (u, v) to the set t one by one, and finally generating an MST with n-1 edges.
Brief description of Kruskal algorithm: Assuming that wn=(v,) is a connected network with n vertices, then the process of constructing the minimum spanning tree and stirring according to the Kruskal algorithm is as follows: first, construct a subgraph with only n vertices and empty edge sets, if the vertices in the subgraph are regarded as the root nodes on each tree, then it is a forest containing n trees.
If you just go to the Internet, the speed requirements are not high (less than 54mbps), then 70 can also be used, the maximum speed is actually about 700kb s, if the router has a bridge function, and the received signal can transmit data normally, then a bridge is also possible, but it is recommended to use a pair, you can use 14 channels, and the interference is much smaller. The effect of a pair under the visual condition of 100 meters is very good.
Hello. Violated your home's right to light. You can ask him to remove the obstacles. >>>More