No phase weighted graph, minimum spanning tree algorithm

Original link: https://www.cnblogs.com/guweiwei/p/7083368.html

FIG weights assigned the edges of the web or referred weighted graph, weighted graph is a spanning tree of the weighted, sum of the weights of each side of the spanning tree T values ​​referred to the weight of the tree.

   Minimum spanning tree (MST): minimum weight spanning tree.

   And the minimum spanning tree spanning applications: n cities need to communicate with the n-1 sides of the line. The weights can be interpreted as the cost side of the line. The minimum spanning tree represents the minimum spanning tree it cost.

   Minimum spanning tree structure network must address the following two questions:

    1, as much as possible to select a small weight side, but can not form a loop;

    2, select the appropriate side to the n-1 communication n vertices;

    MST properties: Suppose G = (V, E) is a communication network, U is a non-empty set of vertices V. If the (u, v) is an edge having a minimum weight value, wherein u∈U, v∈V-U, certainly there is a minimum spanning tree comprises an edge (u, v) of. 

 

1.prim algorithm

  The basic idea: Suppose G = (V, E) is in communication, TE is the minimum spanning tree of G in the set of edges. Algorithm starts U = {u0} (u0∈V), TE = {}. Repeat the following operations:

   Find a weight value for all side u∈U, v∈V-U of the (u, v) ∈E smallest edge (U0, v0) set TE is incorporated while incorporated v0 U, up until V = U .

   In this case, TE there must be n-1 edges, T = (V, TE) of the G minimum spanning tree.

   The core Prim algorithm: Always keep the edge set TE constitutes a spanning tree.

Note: prim algorithm for dense FIG its time complexity is O (n ^ 2), which is independent of time and the complexity of the number of sides too.

Read large blocks of text above is not feeling a little dizzy ah, in order to better understand I can give an example, the following example:

(1) in FIG. 6 vertices v1-v6, the edge weights of each edge in the figure; during prim algorithm, I will randomly select a vertex as a starting point, of course, as a starting point we generally select v1, well, now we have set up a collection for the minimum spanning tree U inside the vertex currently found, TE collection is found side, now state the following:

U v1 = {}; TE = {};

(2) Find a vertex U is now set, the minimum weight of the other set of vertices in the VU, as shown below, to find the minimum value in the red line intersect.

We can see through FIG edge weights v1-v3 minimum of 1, then the U v3 added to the set, (V1, v3) was added to the TE, the following state:

U = {v1, v3}; TE = {(v1, v3)};

(3) continue to search, the current state is U = {v1, v3}; TE = {(v1, v3)}; and to find a minimum value at the edge of red line intersect.

We can find the minimum value of the weight (v3, v6) = 4, we will set v6 is added to the U, and the minimum edge TE was added to the collection, then the state after the addition of the following:

U = {v1, v3, v6}; TE = {(v1, v3), (v3, v6)}; at this cycle until we find all vertices.

(4) next picture shows us the entire discovery process:

Kruskal (Kruskal) algorithm (only relevant side)

 

Algorithm Description: Kruskal algorithm requires access to the edges of the graph, so the time complexity of the algorithm of Kruskal and only edge and relationships, can prove its time complexity is O (eloge).

Algorithmic process:

1. FIG sorted according to each side of the weight

2. FIG traversal time, find the smallest edge weights: set (on this side can not identify the minimum spanning tree is added to the set of edges form a ring), if they meet the conditions, the addition of the minimum spanning tree . Does not meet the conditions continued to traverse the map, find the next edge of minimum weight.

3. Repeat steps 1 recursively, till finding the n-1 sides (FIG provided with n nodes, the number of sides of the minimum spanning tree should be the n-1), the algorithm ends. This minimum spanning tree is obtained in FIG.

 

Kruskal (the Kruskal) algorithm because only relevant side, the demand for thinning the minimum spanning tree of FIG. The prime algorithm because only with the vertex, so dense minimum spanning tree for FIG.

The time complexity of the algorithm is kruskal O (eloge) related with the number of edges, for sparse graphs.

Guess you like

Origin www.cnblogs.com/xysun/p/11201759.html