Minimum consumption Spanning Tree

Source: Internet
Author: User


1. Kruskal Algorithm

(1) algorithm ideas

K r u s k a l algorithm selects n-1 edge each time. The greedy criterion used is: select an edge that does not generate a loop from the remaining edge and add it to the selected edge set. Note that if the selected edge generates a loop, it is impossible to form a spanning tree. The K r u s k a l algorithm is divided into steps e, where e is the number of edges in the network. Consider the e-edge in the ascending order of consumption, and consider one edge each time. When you consider an edge, if it is added to the selected edge set, a loop will appear, then it will be discarded; otherwise, it will be selected.

Test the network in Figure 1-12a. At the beginning, no edge is selected. Figure 13-12b shows the current status of each node. Edge (1, 6) is the first selected edge. It is added to the build tree to be constructed. See figure 1 3-1 2 c. Next, select an edge (3, 4) and add it to the tree (1 3-1 2 d ). Then consider the side () and add it to the tree without generating a loop, so we can see Figure 1 3-1 2 e. Next, consider the edge (2, 3) and add it to the tree (1 3-1 2 f ). Among the remaining edges that are not yet considered, () has the minimum cost. Therefore, first consider it. adding it to the tree being created will generate a loop and discard it. Add the edge () to the tree, as shown in the tree 13-12g. The next step is to consider the side (7, 5) and discard it because it will generate a loop. Finally, consider the edge (6, 5) and add it to the tree to generate a spanning tree, which consumes 9. Figure 1-1 3 shows the pseudo code of the K r u s k a l algorithm.

// Find a minimal spanning tree in a network with n vertices

Set T as the set of selected edges and initialize T =

Set E as the edge set in the Network

W h I l e (E =) & (| T | =n-1 ){

Make (u, v) the edge with the smallest cost in E = E-{(u, v)} // Delete the edge from E

When I f (u, v) is added to T, no loop is generated.) (u, v) is added to T.

}

I f (| T | = N-1) T is the minimum consumption spanning tree.

The e l s e network is not interconnected and cannot find the Spanning Tree.

Figure 13-13 pseudocode of the Kruskao Algorithm

(2) Proof of correctness

The conversion technology used in the above loading problem proves that the greedy algorithm in Figure 1 3-1 3 can always create a minimum cost Spanning Tree. The following two points must be proved: 1) as long as a spanning tree exists, the K r u s k a l algorithm can always generate a spanning tree; 2) the generated tree has the minimum cost. Make G an arbitrary weighted undirected graph (that is, G is an undirected network ). We can see from section. 3 that a tree is generated when and only when an undirected graph is connected. In addition, the edges that are rejected (discarded) in the Kruskal algorithm are those that will generate loops. The graph formed by deleting an edge in the connected graph loop is still a connected graph. Therefore, if G is connected at the beginning, T and E can always form a connected graph. That is, if G is connected at the beginning, the algorithm will not end with E = and | T | <n-1.

It is now proved that the created spanning tree T has the minimum cost. Since G has a finite spanning tree, it has at least one smallest spanning tree. So that U is such a minimal spanning tree, T and U both have exactly n-1 edge. If T = U, T has the minimum cost, so it does not have to be proved. Therefore, suppose T = U, so that k (k> 0) is the number of edges in T but not in U. k is also the number of edges in U but not in T. By converting U to T, it is proved that U and T have the same consumption. This conversion can be completed in k steps. Each step reduces the number of edges in T rather than U by 1. In addition, the cost of U will not change because of conversion. After k step conversion, the U will have the same consumption as the original U, and the edges in the transformed U will be the edges in T. T has the minimum cost. Each step of conversion includes moving an edge e from T to U and removing an edge f from U. Select edge e and f as follows:

1) Make e have the smallest edge in T, not in U. Because k> 0, this edge must exist.

2) When e is added to U, a unique loop is formed. Make f an edge that is not in T on this loop.

Because T does not contain loops, at least one side of the formed loop is not in T.

From the selection method of e and f, we can see that V = U + {e}-{f} is a spanning tree, and exactly k-1 edge in T does not appear in V. It is now proved that the cost of V is the same as that of U. Obviously, the cost of V is equal to the cost of U plus the cost of edge e minus the cost of edge f. If the cost of e is smaller than that of f, the cost of generating tree V is smaller than that of U, which is impossible. If the cost of e is higher than that of f, f will be considered before e in the K r u s k a l algorithm. Because f is not in T, the Kruskal algorithm has discarded f when considering whether f can be added to T. Therefore, f and T consume less than or equal to f to form a loop. By selecting e, all these edges are in U, so U must contain loops, but in fact this is impossible because U is a spanning tree. The assumption that the cost of e is higher than that of f will lead to conflicts. The only difference between e and f is that the cost of V and U is the same.

(3) Data Structure Selection and Complexity Analysis

To select an edge in a non-decreasing order of consumption, you can create a minimum heap and retrieve each edge from the heap one by one as needed. When an e edge exists in the graph, it takes (e) Time to initialize the heap and O (l o ge) to select each edge.

<

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.