FIG - Kruskal minimum spanning tree method implementation of FIG.

1, the minimum spanning tree features:

 

       1, the selected edge is smaller in FIG edge weights;

       2, all sides of the connection does not constitute a loop;

      

2, prim algorithm based on the apex at the core, at most spanning biggest feature is the edge, but insisted prim algorithm to vertex as the core to carry out, is somewhat complex and difficult to understand;

 

3, since the minimum spanning tree care about is how to choose the n - 1 edges, then take it to the side for the core direct algorithmic design?

 

4, simple try:

 

       1, the configuration of FIG four vertexes, three selected minimum weight side;

       2, but also to try to avoid loops;

      

 

5, need to be addressed:

 

       1, how to determine the newly selected edge with selected side constitutes the loop?

      

6. Tip: precursor array of tags (to avoid adding new edge loop caused problems)

       1, define the array: Array <int> p (vCount ());

       2. Define the array elements of significance:

              1, p [n] n vertices showing another side of the connecting passage in the end of the apex;

       3, the precursor array of tags exactly how come?

      

7, a key step in the minimum spanning tree algorithm (Kruskal):

       1, the definition of the precursor array of tags: Array <int> p (vCount ());

       2, acquires all of the edges of the current figure, and is stored in the array edges;

       3, the array deges sorted according to weight;

       4, with a p-n in the array before selecting the array edges - the one side does not constitute a loop;

      

8, Kruskal algorithm process:

 

      

9, find the key to the lookup function:

     

 

10, the Kruskal minimum spanning tree algorithm (Kruskal) implemented:

1     / * minimum, kruskal large Spanning Tree Algorithm * / 
2      SharedPointer <the Array <Edge <E>>> Kruskal ( const  BOOL minmum = to true )
 . 3      {
 . 4          LinkQueue <Edge <E>> RET;   // return queue 
5          SharedPointer <the array <edge <E>>> = getUndirectedEdges edges ();   // will not all of the edges have to get the phase diagram 
. 6          DynamicArray < int > P (VCount ());   // precursor tag array 
. 7  
. 8          / * precursor set flag value * / 
. 9          for ( int I = 0; I <p.length (); I ++ )
 10          {
 . 11              P [I] = - . 1 ;
 12 is          }
 13 is  
14          / * Sort the array of edge * / 
15          the Sort :: Shell (* Edges, minmum);   // second parameters on the side for sorting in descending order, to generate a maximum spanning tree 
16  
. 17          / * loop is entered selection edge * / 
18 is          for ( int I = 0 ; (I <edges-> length ()) && ( ret.length () <(VCount () - . 1 )); I ++)   // up to several times a loop side, and if the edge but has a lot of N - 1 edges is selected, then the end of the cycle                                            
19  {
 20             int B = Find (P, (* Edges) [I] .B);   // Find the precursor selected tag array two side vertices 
21 is              int E = Find (P, (* Edges) [I] .E );   // precursor tag array for determining the edge of the newly selected is whether the cause circuit 
22 is  
23 is              IF (B = E)!   // is equal to constitute a loop 
24              {
 25                  P [E] = B;   // modified precursor tag array 
26 is  
27                  ret.add ((* edges) [I]);   // these edges are added to the result set to 
28              }
 29          }
 30  
31 is          IF (! ret.length () = (VCount () - . 1 ))   / /Determining whether the edge enough, it is not enough to form the minimum spanning tree 
32          {
 33 is              throw_exception (an InvalidOperationException, " No Operation enough Edges for the Kruskal ... " );
 34 is          }
 35  
36          return toArray (RET);   // convert the results into an array of 
37      }

 

11, Kruskal algorithm test code:

 1 #include <iostream>
 2 #include "MatrixGraph.h"
 3 #include "ListGraph.h"
 4 
 5 using namespace std;
 6 using namespace DTLib;
 7 
 8 template< typename V, typename E >
 9 Graph<V, E>& GraphEasy()
10 {
11    static MatrixGraph<4, V, E> g;
12 
13     g.setEdge(0, 1, 1);
14     g.setEdge(1, 0, 1);
15     g.setEdge(0, 2, 3);
16     g.setEdge(2, 0, 3);
17     g.setEdge(1, 2, 1);
18     g.setEdge(2, 1, 1);
19     g.setEdge(1, 3, 4);
20     g.setEdge(3, 1, 4);
21     g.setEdge(2, 3, 1);
22    g.setEdge(3, 2, 1);
23 
24     return g;
25 }
26 
27 template< typename V, typename E >
28 Graph<V, E>& GraphComplex()
29 {
30    static ListGraph<V, E> g(9);
31 
32     g.setEdge(0, 1, 10);
33     g.setEdge(1, 0, 10);
34     g.setEdge(0, 5, 11);
35     g.setEdge(5, 0, 11);
36     g.setEdge(1, 2, 18);
37     g.setEdge(2, 1, 18);
38     g.setEdge(1, 8, 12);
39     g.setEdge(8, 1, 12);
40     g.setEdge(1, 6, 16);
41     g.setEdge(6, 1, 16);
42     g.setEdge(2, 3, 22);
43     g.setEdge(3, 2, 22);
44     g.setEdge(2, 8, 8);
45     g.setEdge(8, 2, 8);
46     g.setEdge(3, 8, 21);
47     g.setEdge(8, 3, 21);
48     g.setEdge(3, 6, 24);
49     g.setEdge(6, 3, 24);
50     g.setEdge(3, 7, 16);
51     g.setEdge(7, 3, 16);
52     g.setEdge(3, 4, 20);
53     g.setEdge(4, 3, 20);
54     g.setEdge(4, 5, 26);
55     g.setEdge(5, 4, 26);
56     g.setEdge(4, 7, 7);
57     g.setEdge(7, 4, 7);
58     g.setEdge(5, 6, 17);
59     g.setEdge(6, 5, 17);
60     g.setEdge(6, 7, 19);
61    g.setEdge(7, 6, 19);
62 
63     return g;
64 }
65 
66 int main()
67 {
68     Graph<int, int>& g = GraphEasy<int, int>();
69    SharedPointer< Array< Edge<int> > > sa = g.kruskal(65535);  
70 
71    int w = 0;
72 
73     for(int i=0; i<sa->length(); i++)
74     {
75         w += (*sa)[i].data;
76 
77         cout << (*sa)[i].b << " " << (*sa)[i].e << " " << (*sa)[i].data << endl;
78    }
79 
80    cout << "Weight: " << w << endl;
81 
82     return 0;
83 }

 

13 Summary:

       1, Prim algorithm to find the minimum spanning tree vertex as the core, not directly;

       2, Kruskal algorithm to find the minimum spanning tree as the core side, intuitive and simple;

       3, the key is to use the Kruskal algorithm precursor tag array;

       4, the precursor array flag used for determining whether the newly selected loop will cause the edges;

Guess you like

Origin www.cnblogs.com/dishengAndziyu/p/10926585.html