Min Spanning tree (Minimum Spanning tree)-the smallest of the weights that connect the edges of all vertices
Prim algorithm
- Basic idea-Set the vertex set of the graph to V; the vertex set of the minimum spanning tree is U
- Place a vertex into u
- In one vertex belonging to u, the other vertex belongs to all the edges of the v-u, and the least weighted edge is found
- The vertex that will be found does not belong to u, put in U, repeat 2 until you include all vertices in u
- Specific implementation
- Introduce auxiliary array edge[], length equals the number of vertices
- Node structure-data field vertex (another vertex connected to the vertex) | weight cost (weight of this edge)
- For nodes in U i-edge[i].cost = 0
- For node J-edge[j in V-u] represents the least-weighted edge of the node J
- Time complexity O (n2) (independent of the number of edges)
- Suitable for edge-dense graphs
Kruskal algorithm
- Basic ideas
- Select the edge with the least weight in the diagram
- If this edge does not form a loop, select this edge; otherwise, find the edge of the next weighted value
- Time complexity (independent of vertex count)
- O (n2)-already sorted by the weight of the edge
- O (ELOG2E)-not sorted by the weight of the edge
- Graphs for edge coefficients
Minimum spanning tree for [Data Structure & Algrithom] without graphs