Introduction to algorithms--minimum spanning tree

Source: Internet
Author: User

For a connected graph, we can remove some of the edges that still maintain their connectivity, in which there is one or more graphs, and the sum of their paths is minimal, so the graph must be a tree. Because, if there is a ring in the diagram, the removal of an edge of the ring can still guarantee connectivity, which is contradictory to the total path and minimum. Such graphs are referred to as the bottom spanning tree. Inter-city laying circuits can be planned using the smallest spanning tree.

The black path forms the smallest spanning tree, removing the BC and adding AH is also a minimal spanning tree, and the smallest spanning tree of a graph is not necessarily unique.

The minimum spanning tree can be generated using a secure edge policy: Assuming that set A is a subset of the smallest spanning tree, we can find an edge to add to a, and still maintain a as a subset of the smallest spanning tree, so that the edge is called a secure edge. In order to find the safe side, we define the black section below the ab and de edges to form a set a, (a,v-a) called a cut of G. If an edge (v,u) One endpoint belongs to a and the other endpoint belongs to V-a, it is called a spanning cut (a,v-a). On the edge that spans the cut, you can find one or more of the least weighted edges, called lightweight edges, and a lightweight edge is a secure edge. Because a must be connected to the V-A part, this must be done by crossing the cutting edge, while the lightweight edge is the shortest, so must belong to the smallest spanning tree. The AH bj BC CD DF EF is shown across the cutting edge, where the CD is a lightweight edge. To find lightweight edges, there are two algorithms based on greedy strategies.

Kruskal algorithm

The idea of the Kruskal algorithm is to find the shortest side inside the edge of the two different trees in the forest to join the set a as a safe side. You can use disjoint collections to maintain such structures and establish a tree for each node. Returns the tree to which the node belongs by Find-set, merging the trees where you and V reside when an edge joins collection a. Time complexity can be expressed as O (ELGV).

As shown in the figure, the edges are traversed in length order to check if they want to join the collection a until all the edges have been traversed.

#include <stdio.h>#include<vector>#include<algorithm>using namespacestd;#defineSIZE 10classnode{ Public:    intrank; Node*p;};classroad{ Public:    intu; intv; intweight;};intG[size][size];//adjacency Matrix, parameter initialization slightlyNode nodes[size];voidMakeSet (Node x) {X.P= &x; X.rank=0;}voidUnion (Node x,node y) {if(X.rank >Y.rank) Y.P= &x; Else{X.P= &y; if(X.rank = =Y.rank) Y.rank++; }}node*Findset (Node x) {if(X.P! = &x) X.P= Findset (*X.P); returnX.P;}BOOLCOM (Road a,road b) {return(A.weight<b.weight);//Ascending order}vector<Road>Mstkruskal () {vector<Road>A; inti,j; Vector<Road>roads;  for(i =0; i < SIZE; i++) {Nodes[i]= *NewNode (); MakeSet (Nodes[i]);//Each tree contains a node.    }     for(i =0; i < SIZE; i++){         for(j = i +1; J < SIZE; J + +){            if(G[i][j]! =0) {Road Road= *NewRoad (); ROAD.U=i; ROAD.V=J; Road.weight=G[i][j];            Roads.push_back (road); }}} sort (Roads.begin (), roads.end (), COM);//to sort all paths in descending order     for(i =0; I < roads.size (); i++) {Road Road=Roads[i]; if(Findset (nodes[road.u])! =Findset (NODES[ROAD.V]))            {A.push_back (road);        Union (NODES[ROAD.U],NODES[ROAD.V]); }    }    returnA;}
Prim algorithm

The idea of the prim algorithm is to join the set a from the root node, and to find the shortest, that is, the lightweight side that spans (a,v-a) in A and v-a connected edge. The minimum value of v-a to a distance can be stored in the priority queue Q to reduce the time that each traversal looks for the shortest edge. The priority queue can be achieved by the minimum binary heap or Fibonacci heap, the former progressive Time O (ELGV) and the latter improved to O (E+VLGV)

#include <stdio.h>#include<vector>using namespacestd;#defineSIZE 10#defineINFI 10000classroad{ Public:    intu; intv; intweight;};intG[size][size];//adjacency Matrix, parameter initialization slightlyVector<Road> Mstprim (introot) {Vector<Road>A; inti; Road Roads[size];//record the shortest path to arrival a     for(i =0; i < SIZE; i++) {Roads[i]= *NewRoad (); if(G[root][i]! =0) {roads[i].u=Root; ROADS[I].V=i; Roads[i].weight=G[root][i]; }        ElseRoads[i].weight=infi; }     while(A.size ()! = Size-1){        intMin =0;  for(i =1; i < SIZE; i++){            if(Roads[i].weight < roads[min].weight && roads[i].weight >0) min= i;//find the shortest path} a.push_back (Roads[min]); Roads[min].weight= -1;//indicates that the point is already in a         for(i =0; i < SIZE; i++){            if(G[min][i] <roads[i].weight) Roads[i].weight= G[min][i];//update the shortest length of arrival a        }    }    returnA;}

Introduction to algorithms--minimum spanning tree

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.