minimum spanning tree

1. Definition

  • Connected graph: In an undirected graph, if any two vertices vi and vj have a path connected, the undirected graph is called a connected graph.
  • Strongly connected graph: In a directed graph, if any two vertices vi and vj have paths connected, the directed graph is called a strongly connected graph.
  • Connected graph: In a connected graph, if the edge of the graph has a certain meaning, each variable has a corresponding number, called a weight, and the weight represents the cost of connecting two vertices. This connected graph is called a connected network.
  • Spanning tree: The spanning tree of a connected graph refers to a connected subgraph, which contains all n vertices in the graph, but only has enough n-1 edges to form a tree. A spanning tree with n vertices has and only n-1 edges. If an edge is added to the spanning tree, it must be a cycle.
  • Minimum spanning tree: (minimum cost) The spanning tree of a connected graph with n nodes is a minimally connected subgraph of the original graph, and contains all n nodes in the original graph, and has the least number of nodes that keep the graph connected. side.

The minimum spanning tree can be calculated by kruskal algorithm or prim algorithm.

 

2. Kruskal algorithm

This algorithm can be called "edge addition method". The initial minimum spanning tree edge number is 0, and each iteration selects a minimum cost edge that satisfies the condition and adds it to the edge set of the minimum spanning tree.

1. Sort all the edges in the graph in ascending order of cost;

2. Treat the n vertices in the graph as a forest consisting of n independent trees;

3. Select the edge according to the weight from small to large. The two vertices ui and vi connected by the selected edge should belong to two different trees, so they become an edge of the minimum spanning tree, and the two trees are combined as one. tree.

4. Repeat (3) until all vertices are within a tree or there are n-1 edges.

 

3. Prim algorithm

This algorithm can be called "point addition method". Each iteration selects the point corresponding to the edge with the least cost and adds it to the minimum spanning tree.

The algorithm starts from a certain vertex s and gradually reaches all vertices covering the entire connected network.

1. The set of all vertices of the graph is V; the initial set u = { s } , v = V u ;

2. Among the edges that can be composed of two sets u and v, select an edge (u0, v0) with the least cost, add it to the minimum spanning tree, and merge v0 into the set u.

3. Repeat the above steps until the minimum spanning tree has n-1 edges or n vertices.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325069972&siteId=291194637