Minimum Spanning Trees in C

Advanced
6 min read
Updated: Nov 2026
1 example

Connecting Everything Cheaply

A spanning tree of a connected undirected graph is a subset of edges that connects all V vertices without cycles, so it has exactly V - 1 edges. A minimum spanning tree (MST) is the spanning tree with the smallest total weight. Applications include cable and road networks and clustering. Prim and Kruskal find an MST efficiently.

Remember
spanning tree: V vertices, V - 1 edges, connected, acyclic
MST: spanning tree with minimum total weight

Cheat Sheet

Keep these facts handy while you work with Minimum Spanning Trees in C in C.

TopicDetail
EdgesV - 1
PropertyConnected and acyclic
Cycle propertyThe heaviest edge of any cycle is not needed
AlgorithmsPrim, Kruskal

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Prim's Algorithm in C.

Example 1 — Enumerate every spanning tree

Four vertices, five edges; try every 3-edge subset and keep the acyclic ones.

C
#include <stdio.h>

struct Edge { int u, v, w; };

static int find(int p[], int x) {
    while (p[x] != x) x = p[x];
    return x;
}

int main(void) {
    struct Edge e[] = {{0, 1, 1}, {1, 2, 2}, {0, 2, 3}, {2, 3, 4}, {1, 3, 6}};
    int count = 0, best = 1000;

    for (int i = 0; i < 5; i++)
        for (int j = i + 1; j < 5; j++)
            for (int k = j + 1; k < 5; k++) {
                int p[4] = {0, 1, 2, 3}, pick[3] = {i, j, k}, ok = 1, sum = 0;
                for (int t = 0; t < 3; t++) {
                    int ra = find(p, e[pick[t]].u), rb = find(p, e[pick[t]].v);
                    if (ra == rb) { ok = 0; break; }
                    p[ra] = rb;
                    sum += e[pick[t]].w;
                }
                if (ok) {
                    count++;
                    if (sum < best) best = sum;
                }
            }

    printf("spanning trees found: %d\n", count);
    printf("minimum weight: %d\n", best);
    return 0;
}

How It Works

  1. Ten 3-edge subsets exist; two contain a cycle (0-1-2 and 1-2-3).
  2. The other eight each connect all four vertices.
  3. The cheapest is edges 0-1, 1-2, and 2-3 with weight 1 + 2 + 4 = 7.

Edge Cases & Pitfalls

Check these before relying on Minimum Spanning Trees in C in real programs.

disc

Disconnected graphs

No spanning tree exists; you get a spanning forest instead.

uniq

Multiple MSTs

If edge weights tie, several MSTs with the same total weight may exist.

Key Takeaways

  • V - 1 edges.
  • Connected and acyclic.
  • Minimum total weight.

One line: A spanning tree of a connected undirected graph is a subset of edges that connects all V vertices without cycles, so it has exactly V - 1 edges.

Frequently Asked Questions

A spanning tree whose total edge weight is as small as possible.
Exactly V - 1 for a graph with V vertices.
Continue with "Prim's Algorithm in C" next. Grow an MST by repeatedly adding the cheapest edge leaving the tree. The C Graph Algorithms hub lists every topic in order.

Next: Prim's Algorithm in C

Grow an MST by repeatedly adding the cheapest edge leaving the tree.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful