Minimum spanning tree algorithm (unfinished)

Several definitions of the concept map:

  • FIG communication: if any two vertices are vjvj vivi path communicates with the figure in the absence of, the called communication undirected graph in FIG.
  • Strong graph: in there, and if two vertices vivi vjvj have any communication path in the figure, which is said to have a strongly connected graph in FIG.
  • Communication network: communicating in the figure, when the edge has a certain significance graph, each edge corresponds to a number, called weights; cost of connection weights representing vertices connected, which communicate with said communication network is called in FIG.
  • Spanning Tree: FIG spanning a communicating means communicating a subgraph, which contains all of the figures n vertices, but only enough to form a tree of the n-1 sides. A spanning tree with n vertices and only n-1 edges, add a spanning tree if the edge is bound to form a ring.
  • Minimum Spanning Tree: communicating all spanning trees in the network, the cost of all edges and minimal spanning tree, called the minimum spanning tree. 

 

Two minimum spanning tree (Minimum Spanning Tree, MST) algorithm is entirely equivalent.

If the input adjacency matrix, usually with Prim.

If the input edge list, usually by Kruskal.

1.Kruskal algorithm

The complicated and time complexity for all sides of the same sort as O (m \ log m) O ( m log m ).

If only the maximum required minimum spanning tree edge weights may be O (m) O ( m is obtained within) time.
(Half each lose half)

This algorithm may be called "Bordered Method", the initial number of edges is zero minimum spanning tree, for each iteration selects a minimum cost to meet the conditions of the edge, is added to the set of edges in the minimum spanning tree. 
1. all edges in the graph by the cost of small to large; 
2. FIG. N independent n vertices as a forest of trees; 
3. small to large weight selection edges, the selected edges connecting two vertices ui, viui, vi, should belong to two different trees, it becomes a minimum spanning tree edge, and combine these two trees as a tree. 
4. Repeat (3) until all vertices in the tree or there are n-1 until the edges.

 

2.Prim algorithm

It can be used to optimize the heap, similar to Dijkstra, but to optimize the use of the heap, not necessarily improve efficiency.

In the case of complete graphs:

Heap optimization is not used, the time complexity is O (^ n-2) O ( n- 2 );

Heap optimization, time complexity is O (n-2 ^ \ n-log) O ( n- 2 log n- ).

So adjacency matrix for the graph to enter, no need to use Kruskal.

Let G be the set of vertices of U, is first arbitrarily selected point in G as a starting point a, point of addition of the set V, to find another point b to point b so that the minimum point in V is a set of weights from the UV, At this point will be added to the set of V b; and so, now set V = {a, b}, c to find another point from the set such that the UV point V c to the minimum weight at any point in this case the c to the collection point V, have all been added until all vertices V, this time to build up a MST. Because there are N vertices, so that there are N-1 MST edges, each time point was added to the set V, the means to find an edge MST.

 

Initial state:

Providing two data structures :

lowcost [i]: it indicates the minimum weight to the edge ending at i, when Lowcost [i] i = 0 to be described as the end point of the edge of minimum weight = 0, i represents a point is added to the MST

mst [i]: indicates the starting point corresponds Lowcost [i], i.e., side description <mst [i], i> is a side MST, when mst [i] = 0 indicates the starting i added MST

 

We assume that V1 is the starting point, to initialize (* represents infinity, that is, no access):

 

lowcost[2]=6,lowcost[3]=1,lowcost[4]=5,lowcost[5]=*,lowcost[6]=*

mst [2] = 1, mst [3] = 1, mst [4] = 1, mst [5] = 1, mst [6] = 1, (all default starting point is V1)

 

As is apparent to V3 ending at a minimum edge weights = 1, the edge <mst [3], 3> = 1 were added MST

In this case, because the addition of point V3, the need to update the lowcost arrays and arrays mst:

 

lowcost[2]=5,lowcost[3]=0,lowcost[4]=5,lowcost[5]=6,lowcost[6]=4

mst[2]=3,mst[3]=0,mst[4]=1,mst[5]=3,mst[6]=3

 

As it is apparent to V6 as the end edge weights minimum = 4, so the sides <mst [6], 6> = 4 was added MST

 

In this case, because the addition of the V6 point needs to be updated lowcost arrays and arrays mst:

 

lowcost[2]=5,lowcost[3]=0,lowcost[4]=2,lowcost[5]=6,lowcost[6]=0

mst[2]=3,mst[3]=0,mst[4]=6,mst[5]=3,mst[6]=0

 

 

As is apparent to V4 edge ending at a minimum weight = 2, the side <mst [4], 4> = 4 was added MST

 

In this case, because the point V4 join, you need to update lowcost arrays and arrays mst:

 

lowcost[2]=5,lowcost[3]=0,lowcost[4]=0,lowcost[5]=6,lowcost[6]=0

mst[2]=3,mst[3]=0,mst[4]=0,mst[5]=3,mst[6]=0

 

明显看出,以V2为终点的边的权值最小=5,所以边<mst[2],2>=5加入MST

 

此时,因为点V2的加入,需要更新lowcost数组和mst数组:

 

lowcost[2]=0,lowcost[3]=0,lowcost[4]=0,lowcost[5]=3,lowcost[6]=0

mst[2]=0,mst[3]=0,mst[4]=0,mst[5]=2,mst[6]=0

 

很明显,以V5为终点的边的权值最小=3,所以边<mst[5],5>=3加入MST

 

lowcost[2]=0,lowcost[3]=0,lowcost[4]=0,lowcost[5]=0,lowcost[6]=0

mst[2]=0,mst[3]=0,mst[4]=0,mst[5]=0,mst[6]=0

 

至此,MST构建成功,如图所示:

代码:

 1 //存放地图
 2 int ma[100][100];
 3 //lowcost存放到起点的距离
 4 //mst表示i指向的点,
 5 int lowcost[100],mst[100];
 6 //n表示点数
 7 int n;
 8 //初始化地图
 9 void init()
10 {
11     for(int i=0;i<=n;i++)
12     {
13         for(int j=0;j<=n;j++)
14         {
15             //本地到本地的权值为0,到其他点的权值为无穷
16             if(i==j)
17             {
18                 ma[i][j]=0;
19             }
20             else
21             {
22                 ma[i][j]=INF;
23             }
24         }
25     }
26 }
27 //u表示起点
28 int prim(int u)
29 {
30     //ans表示树上所有边的权值和
31     int ans=0;
32     //初始化lowcost,mst
33     for(int i=1;i<=n;i++)
34     {
35         //将所有指向起点的边录入lowcost中
36         lowcost[i] = ma[u][i];
37         //所有的点都指向起点
38         mst[i]=u;
39     }
40     //起点已在树上,所以为-1
41     mst[u] = -1;
42     //遍历其他n-1个点
43     for(int i=1;i<n;i++)
44     {
45         //当前最小权值为INF
46         int mi=INF;
47         //当前最小值指向的点是-1
48         int v=-1;
49         //遍历所有的点
50         for(int j=1;j<=n;j++)
51         {
52             //如果该点不在树上,且该点到起点的距离小于当前最小权值
53             if(mst[j]!=-1&&lowcost[j]<mi)
54             {
55                 //记录这个较小的点和权值
56                 v=j;
57                 mi=lowcost[j];
58             }
59         }
60         //v!=-1表示在所有与树连接的最小权值
61         if(v!=-1)
62         {
63             //将这个点标记
64             mst[v]=-1;
65             //累计这个边的权值
66             ans+=lowcost[v];
67             //遍所有的点
68             for(int j=1;j<=n;j++)
69             {
70                 // big If j is not in the tree and the tree j j distance than the distance to v
 71                  // update the data, and MST [j] point v 
72                  IF (MST [j] = -! . 1 && Lowcost [j ]> mA [V] [J])
 73 is                  {
 74                      Lowcost [J] = mA [V] [J];
 75                      MST [J] = V;
 76                  }
 77              }
 78          }
 79      }
 80      // return all sides of the tree weights and 
81      return ANS;
 82 }

 

Guess you like

Origin www.cnblogs.com/mzchuan/p/11720280.html