Data Structures and Algorithms of interview questions: minimum spanning tree algorithm

Minimum spanning tree algorithm: Kruskal algorithm and Prim algorithms.

Several concept of map:

FIG communication : if any two vertices vi and vj are the communication path without drawing, the called communication undirected graph in FIG.

Strong graph : in there, if any two vertices vi and vj are communicating 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; right represents the cost of connecting two vertices of said communication network which communication 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.

A, Kruskal Kruskal algorithm

  The core idea of ​​Kruskal algorithm is: constantly find the minimum set of edges in the edge in communication with the right figure, if the obtained satisfies the edge conditions of the minimum spanning tree, will be configured, until a minimum a generation tree.

  Kruskal algorithm to perform the steps of:

  Step 1: communicating FIG weighted, the weight of the edge values ​​are sorted;

  Step two: determine whether to select this edge (in the case of FIG side by weight have been sorted from small to large). Based on the determination whether a vertex has two communication sides, if the next communication continues; if it does not communicate then select communication.

  The third step: The second step cycle, until all of the figures are in the same vertices in a connected component, i.e., to obtain the minimum spanning tree.

Two, Prim algorithm Puli Mu

  The core idea of ​​Puli Mu algorithm is: a starting vertex the vertex addition of a set of vertices U and unlocked from any adjacent sides and find satisfying result in the smallest side piece of the minimum spanning tree, the other side of the vertex added and continue to unlock the set of vertices U adjacent sides, was added until all the vertices to generate a vertex set U minimum spanning tree.

  Puli Mu to step algorithm:

  Step: Select a starting vertex in FIG added vertex set U, and unlock all adjacent edges

  Step: select the smallest one edge weights satisfy the conditions from the unlocked side, the newly added vertex vertex set U;

  The third step: The second step loop until all vertices added set of vertices.

Guess you like

Origin www.cnblogs.com/yichengming/p/11469454.html