C Language algorithm

Source: Internet
Author: User

This problem has been examined in cases 1-2 and 1-3. Because each spanning tree of the N-vertex-free network g has exactly the n-1 edge, the problem is to select the n-1 edge in some way so that they form the minimum spanning tree of G. At least three different greedy strategies can be used to select this n-1 edge. The three greedy algorithm strategies for minimizing spanning trees are: K r u S k a l algorithm, P r i m algorithm and s o l l n algorithm.

1.Kruskal algorithm

(1) Algorithm idea

K r u S k a l algorithm to select the n-1 edge each time, the greedy rule is: from the remaining side of the selection of a loop does not produce a minimum cost of the edge to join the selected edge of the collection. Note that if the selected edge produces a loop, it is impossible to form a spanning tree. K r u S k a l algorithm is divided into e-step, where E is the number of edges in the network. Consider this e-edge in an incremental order, considering one side at a time. When you consider an edge, if you add it to the collection of the selected edges, the loop appears, discard it, or select it.

Explore the network in Figure 1-12a. No edges are selected at the beginning. Figure 13-12b shows the current state of each node. The Edge (1,6) is the first selected edge, which is added to the build tree to be built, with figure 1 3-1 2 C. Next select the Edge (3,4) and add it to the tree (as shown in Figure 1 3-1 2 D). Then consider the Edge (2,7), add it to the tree does not produce a loop, so you get Figure 1 3-1 2 E. Next consider the edge (2,3) and add it to the tree (as shown in Figure 1 3-1 2 f). On the other side that has not yet been considered, (7,4) has a minimum cost, so consider it first, add it to the tree you are creating and create a loop, so discard it. The Edge (5,4) is then added to the tree, and the resulting tree is shown in Figure 13-12g. The next consideration is the Edge (7,5), which is discarded because it produces a loop. Finally, consider the Edge (6,5) and add it to the tree to produce a spanning tree, which consumes 9 9. Fig. 1-1 3 shows the pseudo code of K r u S k a l algorithm.

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

Make T a collection of the selected edges, initialize the t=

Make e a collection of edges in the network

W h i l e (e≠) && (| T |≠n-1) {

Order (U,V) is the least expensive edge in E e=e-{(u,v)}//Remove Edge from E

I f ((u,v) does not produce loops in T) will (U,V) be added T

}

I f (| T |==n-1) T is the minimum cost spanning tree

E L S E Networks are not interconnected and cannot find the spanning tree

Graph 13-13 Kruskao algorithm pseudo-code

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.