# Spanning trees of a graph in C

A spanning tree of a graph is a subgraph that includes all of the vertices, but only enough edges to connect them. A connected graph will have more than one spanning tree unless the graph is a tree, in which case it has just one spanning tree which is the graph itself. A spanning tree of a graph of order $$v$$ will contain $$v – 1$$ edges.

In order to list all of the spanning trees of a graph, we could just construct all of the subsets of the edges of size $$v – 1$$, and then check if they form a spanning tree, but that wouldn’t be very efficient. A more efficient method is to use backtracking, which is what the algorithm presented here does.

In order to decide whether or not to add an edge to a partially constructed spanning tree, the algorithm checks that the candidate edge:

1. Has a higher numeric index than its predecessor
2. Doesn’t have both edges in the same connected component of the tree

The first condition prevents duplicates because the edges are always listed in ascending order. The second one ensures that the tree doesn’t contain too many edges, because an edge is superfluous if both of its endpoints are in the same connected component. In order to check this condition, I used the connected components of a graph algorithm I described previously.

The C code is below. The function spanning_trees() takes a graph in edge list representation, the number of edges and vertices, and a user callback that is called for every spanning tree found.

#include <stdlib.h>

typedef struct {
unsigned int first;
unsigned int second;
} edge;

typedef void (*treefn)(const edge *, unsigned int);

/* Check if vertices v1 and v2 are in different components in the tree */
static unsigned int different_components(const edge *tree, unsigned int t, unsigned int order,
unsigned int v1, unsigned int v2)
{
int *components;
unsigned int different;
connected_components(tree, t, order, &components);
different = components[v1] != components[v2];
free(components);
return different;
}

static void spanning_trees_recursive(const edge *edges, unsigned int n, unsigned int order,
edge *tree, unsigned int t, int predecessor, treefn fun)
{
if (t == order - 1) {
/* Found a tree */
fun(tree, order - 1);
}
else {
unsigned int e;
for (e = predecessor + 1; e < n; e++) {
if (t == 0 /* First edge */
|| different_components(tree, t, order,
edges[e].first, edges[e].second))
{
tree[t] = edges[e];
spanning_trees_recursive(edges, n, order, tree, t + 1, e, fun);
}
}
}
}

void spanning_trees(const edge *edges, unsigned int n, unsigned int order, treefn fun)
{
edge *tree;
tree = malloc((n - 1) * sizeof(edge));
if (tree == NULL) {
return;
}
spanning_trees_recursive(edges, n, order, tree, 0, -1, fun);
free(tree);
}


Here is an example that prints all of the spanning trees of the complete graph on 5 vertices:

#include <stdio.h>
#include <stdlib.h>

/* Calculate the nth triangular number T(n) */
unsigned int triangular_number(unsigned int n)
{
return (n * (n + 1)) / 2;
}

/* Construct a complete graph on v vertices */
edge *complete_graph(unsigned int v)
{
const unsigned int n = triangular_number(v - 1);
unsigned int i, j, k;
edge *edges = malloc(n * sizeof(edge));
if (edges != NULL) {
for (i = 0, k = 0; i < v - 1; i++) {
for (j = i + 1; j < v; j++) {
edges[k].first = i;
edges[k].second = j;
k++;
}
}
}
return edges;
}

/* Print the edges in a tree */
static void print_tree(const edge *tree, unsigned int n)
{
unsigned int e;
for (e = 0; e < n; e++) {
printf("(%u, %u) ", tree[e].first, tree[e].second);
}
putchar('\n');
}

int main(void)
{
const unsigned int v = 5;
const unsigned int n = triangular_number(v - 1);
edge *edges;

edges = complete_graph(v);
if (edges == NULL) {
return 1;
}
spanning_trees(edges, n, v, print_tree);
free(edges);

return 0;
}


Here is the output. There are 125 spanning trees of the complete graph on 5 vertices:

(0, 1) (0, 2) (0, 3) (0, 4)
(0, 1) (0, 2) (0, 3) (1, 4)
(0, 1) (0, 2) (0, 3) (2, 4)
(0, 1) (0, 2) (0, 3) (3, 4)
(0, 1) (0, 2) (0, 4) (1, 3)
(0, 1) (0, 2) (0, 4) (2, 3)
(0, 1) (0, 2) (0, 4) (3, 4)
(0, 1) (0, 2) (1, 3) (1, 4)
(0, 1) (0, 2) (1, 3) (2, 4)
(0, 1) (0, 2) (1, 3) (3, 4)
(0, 1) (0, 2) (1, 4) (2, 3)
(0, 1) (0, 2) (1, 4) (3, 4)
(0, 1) (0, 2) (2, 3) (2, 4)
(0, 1) (0, 2) (2, 3) (3, 4)
(0, 1) (0, 2) (2, 4) (3, 4)
(0, 1) (0, 3) (0, 4) (1, 2)
(0, 1) (0, 3) (0, 4) (2, 3)
(0, 1) (0, 3) (0, 4) (2, 4)
(0, 1) (0, 3) (1, 2) (1, 4)
(0, 1) (0, 3) (1, 2) (2, 4)
(0, 1) (0, 3) (1, 2) (3, 4)
(0, 1) (0, 3) (1, 4) (2, 3)
(0, 1) (0, 3) (1, 4) (2, 4)
(0, 1) (0, 3) (2, 3) (2, 4)
(0, 1) (0, 3) (2, 3) (3, 4)
(0, 1) (0, 3) (2, 4) (3, 4)
(0, 1) (0, 4) (1, 2) (1, 3)
(0, 1) (0, 4) (1, 2) (2, 3)
(0, 1) (0, 4) (1, 2) (3, 4)
(0, 1) (0, 4) (1, 3) (2, 3)
(0, 1) (0, 4) (1, 3) (2, 4)
(0, 1) (0, 4) (2, 3) (2, 4)
(0, 1) (0, 4) (2, 3) (3, 4)
(0, 1) (0, 4) (2, 4) (3, 4)
(0, 1) (1, 2) (1, 3) (1, 4)
(0, 1) (1, 2) (1, 3) (2, 4)
(0, 1) (1, 2) (1, 3) (3, 4)
(0, 1) (1, 2) (1, 4) (2, 3)
(0, 1) (1, 2) (1, 4) (3, 4)
(0, 1) (1, 2) (2, 3) (2, 4)
(0, 1) (1, 2) (2, 3) (3, 4)
(0, 1) (1, 2) (2, 4) (3, 4)
(0, 1) (1, 3) (1, 4) (2, 3)
(0, 1) (1, 3) (1, 4) (2, 4)
(0, 1) (1, 3) (2, 3) (2, 4)
(0, 1) (1, 3) (2, 3) (3, 4)
(0, 1) (1, 3) (2, 4) (3, 4)
(0, 1) (1, 4) (2, 3) (2, 4)
(0, 1) (1, 4) (2, 3) (3, 4)
(0, 1) (1, 4) (2, 4) (3, 4)
(0, 2) (0, 3) (0, 4) (1, 2)
(0, 2) (0, 3) (0, 4) (1, 3)
(0, 2) (0, 3) (0, 4) (1, 4)
(0, 2) (0, 3) (1, 2) (1, 4)
(0, 2) (0, 3) (1, 2) (2, 4)
(0, 2) (0, 3) (1, 2) (3, 4)
(0, 2) (0, 3) (1, 3) (1, 4)
(0, 2) (0, 3) (1, 3) (2, 4)
(0, 2) (0, 3) (1, 3) (3, 4)
(0, 2) (0, 3) (1, 4) (2, 4)
(0, 2) (0, 3) (1, 4) (3, 4)
(0, 2) (0, 4) (1, 2) (1, 3)
(0, 2) (0, 4) (1, 2) (2, 3)
(0, 2) (0, 4) (1, 2) (3, 4)
(0, 2) (0, 4) (1, 3) (1, 4)
(0, 2) (0, 4) (1, 3) (2, 3)
(0, 2) (0, 4) (1, 3) (3, 4)
(0, 2) (0, 4) (1, 4) (2, 3)
(0, 2) (0, 4) (1, 4) (3, 4)
(0, 2) (1, 2) (1, 3) (1, 4)
(0, 2) (1, 2) (1, 3) (2, 4)
(0, 2) (1, 2) (1, 3) (3, 4)
(0, 2) (1, 2) (1, 4) (2, 3)
(0, 2) (1, 2) (1, 4) (3, 4)
(0, 2) (1, 2) (2, 3) (2, 4)
(0, 2) (1, 2) (2, 3) (3, 4)
(0, 2) (1, 2) (2, 4) (3, 4)
(0, 2) (1, 3) (1, 4) (2, 3)
(0, 2) (1, 3) (1, 4) (2, 4)
(0, 2) (1, 3) (2, 3) (2, 4)
(0, 2) (1, 3) (2, 3) (3, 4)
(0, 2) (1, 3) (2, 4) (3, 4)
(0, 2) (1, 4) (2, 3) (2, 4)
(0, 2) (1, 4) (2, 3) (3, 4)
(0, 2) (1, 4) (2, 4) (3, 4)
(0, 3) (0, 4) (1, 2) (1, 3)
(0, 3) (0, 4) (1, 2) (1, 4)
(0, 3) (0, 4) (1, 2) (2, 3)
(0, 3) (0, 4) (1, 2) (2, 4)
(0, 3) (0, 4) (1, 3) (2, 3)
(0, 3) (0, 4) (1, 3) (2, 4)
(0, 3) (0, 4) (1, 4) (2, 3)
(0, 3) (0, 4) (1, 4) (2, 4)
(0, 3) (1, 2) (1, 3) (1, 4)
(0, 3) (1, 2) (1, 3) (2, 4)
(0, 3) (1, 2) (1, 3) (3, 4)
(0, 3) (1, 2) (1, 4) (2, 3)
(0, 3) (1, 2) (1, 4) (3, 4)
(0, 3) (1, 2) (2, 3) (2, 4)
(0, 3) (1, 2) (2, 3) (3, 4)
(0, 3) (1, 2) (2, 4) (3, 4)
(0, 3) (1, 3) (1, 4) (2, 3)
(0, 3) (1, 3) (1, 4) (2, 4)
(0, 3) (1, 3) (2, 3) (2, 4)
(0, 3) (1, 3) (2, 3) (3, 4)
(0, 3) (1, 3) (2, 4) (3, 4)
(0, 3) (1, 4) (2, 3) (2, 4)
(0, 3) (1, 4) (2, 3) (3, 4)
(0, 3) (1, 4) (2, 4) (3, 4)
(0, 4) (1, 2) (1, 3) (1, 4)
(0, 4) (1, 2) (1, 3) (2, 4)
(0, 4) (1, 2) (1, 3) (3, 4)
(0, 4) (1, 2) (1, 4) (2, 3)
(0, 4) (1, 2) (1, 4) (3, 4)
(0, 4) (1, 2) (2, 3) (2, 4)
(0, 4) (1, 2) (2, 3) (3, 4)
(0, 4) (1, 2) (2, 4) (3, 4)
(0, 4) (1, 3) (1, 4) (2, 3)
(0, 4) (1, 3) (1, 4) (2, 4)
(0, 4) (1, 3) (2, 3) (2, 4)
(0, 4) (1, 3) (2, 3) (3, 4)
(0, 4) (1, 3) (2, 4) (3, 4)
(0, 4) (1, 4) (2, 3) (2, 4)
(0, 4) (1, 4) (2, 3) (3, 4)
(0, 4) (1, 4) (2, 4) (3, 4)