Prim algorithm and Kruskal algorithm of the minimal spanning tree (using HDU 1863 as an example)

Source: Internet
Author: User

Properties of the minimal spanning tree

MST properties: Set G = (V, E) to a connected weighted graph, and u to a true subset of v. If (u, v) ∈ E, and U, V, V-U, and in all such edges,

(U, v) the weight C [u] [v] is the smallest, so there must be a minimum spanning tree of G, (u, v) is one of the edges.

To construct the minimal spanning tree, you must solve the following two problems:
(1). Select an edge with a smaller weight as much as possible, but it cannot constitute a loop (that is, a loop ).
(2) Select n-1 appropriate edges to connect n vertices of the network.

The idea of the prim algorithm:

Set G = (V, E) to connect to the weighted graph, V = {1, 2 ,..., N }. First select a point (generally select the first point), first set S = {1}, then, as long as S is the true subset of V, select to meet the conditions I, J, V-S, and the smallest edge of C [I] [J], add vertex J to S. This process continues until s = v. All the edges selected in this process constitute a minimum spanning tree of G.

Prim algorithm code

Take HDU 1863 as an example (click to open the link)

<PRE name = "code" class = "CPP"> # include <stdio. h> # include <limits. h> # include <string. h> # define n 100int n, m, map [n + 5] [n + 5], V [n + 5], low [n + 5]; int prim () {int I, j, POs, Min, S = 0; memset (v, 0, sizeof (v); // v [I] indicates whether I is accessed, initialize to 0 first, indicating that none of them have accessed V [1] = 1; // You can select either of them as the first point Pos = 1; // POS is used to mark the subscript for (I = 2; I <= N; I ++) low [I] = map [1] [I]; // use the low array to store the weights of selected points to other points for (I = 1; I <n; I ++) {min = int_max; For (j = 1; j <= N; j ++) // obtain the edge if (! V [J] & low [J] <min) {min = low [J]; Pos = J;} If (min = int_max) break; S + = min; V [POS] = 1; for (j = 1; j <= N; j ++) // update the low array if (! V [J] & map [POS] [J] <low [J]) low [J] = map [POS] [J];} if (I! = N) S =-1; return s;} int main () {int I, j, S, A, B, C; while (scanf ("% d", & M, & N )! = EOF) {// M indicates the number of roads and N indicates the number of villages. If (M = 0) break; for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) map [I] [J] = int_max; // initialize the map array to a very large value (maximum int value) for (I = 1; I <= m; I ++) {scanf ("% d", & A, & B, & C); map [a] [B] = map [B] [a] = C; // map [a] [B] The stored weights from A to B} s = prim (); If (S =-1) printf ("? \ N "); else printf (" % d \ n ", S) ;}return 0 ;}

 

The concept of the Kruskal algorithm is given as follows: undirected and weighted graph G = (V, E), V = {1, 2,..., n }. (1) first, n vertices of G are considered as N isolated connected branches. Sort all edges by weight. (2) Check each edge in ascending order of edge weight from the first edge. Use the following method to connect two different connected branches: When the K side (V, W) is displayed, if the endpoints V and W are the endpoints of the current two different connection branches t1 and t2 respectively, use the edge (V, W) to connect t1 and t2 into a connection branch, then, view the k + 1 edge. If the endpoint V and W are in the same connected branch, view the k + 1 edge directly. This process ends with only one connected branch. At this time, a minimum spanning tree that has formed G. Kruskal algorithm code:

Take HDU 1863 as an example (click to open the link)

<PRE name = "code" class = "CPP"> # include <cstdio> # include <algorithm> using namespace STD; int f [105], n, m; struct Stu {int A, B, C;} t [1, 5500]; int CMP (struct Stu X, struct Stu y) {return X. c <Y. c;} int find (int x) // path compression, find the parent node {If (X! = F [x]) f [x] = find (F [x]); Return f [X];} int KRUS () {int I, K = 0, S = 0, x, y; for (I = 1; I <= N; I ++) {x = find (T [I]. a); y = find (T [I]. b); If (X! = Y) {// The minimum spanning tree cannot form a ring. Therefore, you must determine whether they belong to the same set S + = T [I]. c; k ++; If (k = m-1) // <span style = "font-family: kaiti_gb2312;"> the minimum generation tree forms the kernel-1 (vertex-1) edge. If it has been formed, the minimum spanning tree has formed </span> break; F [x] = y; // update the parent node} If (K! = M-1) S =-1; return s;} int main () {int I, S; while (scanf ("% d", & N, & M )! = EOF) {If (n = 0) break; for (I = 1; I <= N; I ++) scanf ("% d ", & T [I]. a, & T [I]. b, & T [I]. c); for (I = 1; I <= m; I ++) // F [I] the father of node I, first, initialize the father as f [I] = I; sort (t + 1, t + 1 + N, CMP ); // sort by weight from small to large S = KRUS (); If (S =-1) printf ("? \ N "); else printf (" % d \ n ", S) ;}return 0 ;}

 

Note: If the number of vertices is N, the edge is E.

The prim algorithm is suitable for dense graphs. Its time complexity is O (n ^ 2), and its time complexity is irrelevant to the number of edges,

The time complexity of the Kruskal algorithm is O (eloge), which is related to the number of edges. It is suitable for sparse graphs.



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.