Skip to main content

Kruskal's MST on graph A to G

Short answer

Kruskal's algorithm sorts edges ascending and uses union-ind. Sorted: A-G2, A-F3, B-G3, C-D3, B-C5, G-E5 accepted. Remaining rejected. MST edges: A-G, A-F, B-G, C-D, B-C, G-E; cost = 21.

Explain Kruskal algorithm for finding minimum spanning tree in a graph. [Figure: undirected weighted graph on vertices A, B, C, D, E, F, G with edges A-B 10, A-F 3, A-G 2, B-G 3, B-C 5, F-G 8, F-E 8, G-E 5, G-C 9, G-D 8, E-D 7, C-D 3]

Minimum spanning tree (MST): a spanning tree of a connected, weighted, undirected graph (a tree containing all V vertices and V−1 edges) whose total edge weight is the smallest possible.

Kruskal's algorithm is a greedy method: it considers the edges in increasing order of weight and adds an edge to the forest only if it does not form a cycle.

Algorithm

KRUSKAL(G)
1. T = empty set
2. make each vertex a separate set (tree)
3. sort all edges in non-decreasing order of weight
4. for each edge (u, v) in sorted order
5.     if FIND(u) != FIND(v)        // different components, no cycle
6.         add (u, v) to T
7.         UNION(u, v)
8.     if T has V-1 edges, stop
9. return T
struct edge { int u, v, w; } e[100];
int parent[50];
int find(int x) { while (parent[x] != x) x = parent[x]; return x; }

int kruskal(int V, int E) {
    int total = 0, count = 0;
    sortEdgesByWeight(e, E);                 /* ascending */
    for (int i = 0; i < V; i++) parent[i] = i;
    for (int i = 0; i < E && count < V - 1; i++) {
        int a = find(e[i].u), b = find(e[i].v);
        if (a != b) {                         /* no cycle */
            parent[a] = b;                    /* union */
            total += e[i].w; count++;
            printf("%d-%d (%d)\n", e[i].u, e[i].v, e[i].w);
        }
    }
    return total;
}

Complexity: O(E log E), dominated by sorting the edges.

Application to the given graph

Edges: A-B 10, A-F 3, A-G 2, B-G 3, B-C 5, F-G 8, F-E 8, G-E 5, G-C 9, G-D 8, E-D 7, C-D 3 (7 vertices, so the MST has 6 edges).

Sorted: A-G 2, A-F 3, B-G 3, C-D 3, B-C 5, G-E 5, E-D 7, F-G 8, F-E 8, G-D 8, G-C 9, A-B 10.

StepEdgeWeightDecisionTotal
1A-G2accept2
2A-F3accept5
3B-G3accept8
4C-D3accept11
5B-C5accept (joins {A,B,F,G} and {C,D})16
6G-E5accept (adds E; 6th edge, so stop)21
7 to 12remaining7, 8, 8, 8, 9, 10not needed (each would form a cycle)21

MST edges: A-G, A-F, B-G, C-D, B-C, G-E. Minimum cost = 21.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 10 questions →