Prim's Algorithm in C

Advanced
6 min read
Updated: Nov 2026
1 example

Growing a Tree from One Vertex

Prim's algorithm starts from any vertex and repeatedly adds the cheapest edge that connects the tree to a new vertex. Keep key[v] = cheapest known edge weight from the tree to v, and parent[v]. With a matrix it is O(V^2); with a heap, O(E log V). It works well on dense graphs.

Remember
key[start] = 0
repeat V times:
    u = vertex not in tree with smallest key
    add u; for each neighbor v not in tree:
        if w(u, v) < key[v]: key[v] = w(u, v), parent[v] = u

Cheat Sheet

Keep these facts handy while you work with Prim's Algorithm in C in C.

TopicDetail
Greedy ruleCheapest edge leaving the tree
Matrix versionO(V^2)
Heap versionO(E log V)
Good forDense graphs

C Examples

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

Example 1 — Prim on a five-vertex graph

Edges: 0-1:2, 0-3:6, 1-2:3, 1-3:8, 1-4:5, 2-4:7, 3-4:9.

C
#include <stdio.h>
#define V 5
#define INF 1000000

int main(void) {
    int w[V][V] = {{0}};
    int e[][3] = {{0, 1, 2}, {0, 3, 6}, {1, 2, 3}, {1, 3, 8}, {1, 4, 5}, {2, 4, 7}, {3, 4, 9}};
    for (int i = 0; i < 7; i++) {
        w[e[i][0]][e[i][1]] = e[i][2];
        w[e[i][1]][e[i][0]] = e[i][2];
    }

    int key[V], parent[V], in[V] = {0}, total = 0;
    for (int i = 0; i < V; i++) { key[i] = INF; parent[i] = -1; }
    key[0] = 0;

    for (int round = 0; round < V; round++) {
        int u = -1;
        for (int i = 0; i < V; i++)
            if (!in[i] && (u < 0 || key[i] < key[u])) u = i;
        in[u] = 1;
        if (parent[u] >= 0) {
            printf("%d - %d (%d)\n", parent[u], u, key[u]);
            total += key[u];
        }
        for (int v = 0; v < V; v++)
            if (w[u][v] && !in[v] && w[u][v] < key[v]) {
                key[v] = w[u][v];
                parent[v] = u;
            }
    }
    printf("total weight: %d\n", total);
    return 0;
}

How It Works

  1. The tree starts at 0; the cheapest connection is 0-1 (2).
  2. Then 1-2 (3) and 1-4 (5) are cheapest, in that order.
  3. The last vertex, 3, joins through 0-3 (6). Total: 2 + 3 + 5 + 6 = 16.

Edge Cases & Pitfalls

Check these before relying on Prim's Algorithm in C in real programs.

dir

Directed edges

Prim's algorithm is for undirected graphs.

zero

Zero as "no edge"

If a real edge can have weight 0, use a separate marker.

Key Takeaways

  • Grow one tree.
  • Cheapest crossing edge.
  • Matrix O(V^2).

One line: Prim's algorithm starts from any vertex and repeatedly adds the cheapest edge that connects the tree to a new vertex.

Frequently Asked Questions

It grows a single tree from a start vertex by repeatedly adding the cheapest edge that connects the tree to a vertex outside it.
Prim grows one tree vertex by vertex; Kruskal sorts all edges and adds the cheapest ones that do not form a cycle.
Continue with "Kruskal's Algorithm in C" next. Sort edges by weight and add each one that does not create a cycle. The C Graph Algorithms hub lists every topic in order.

Next: Kruskal's Algorithm in C

Sort edges by weight and add each one that does not create a cycle.

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.

9 people found this page helpful