Graph
A graph is a set of vertices and a set of edges between them. In C you usually store it as adjacency lists — an array of neighbor lists — or as an adjacency matrix of booleans or weights.
Detailed Explanation
Trees are graphs with a restriction. Once a node can have two parents, or a back edge, you need a general graph. BFS, DFS, shortest paths, and topological sort all run on this representation.
Adjacency lists fit sparse graphs. You have n vertices numbered 0 .. n-1. adj[i] is a dynamic array or a linked list of vertices j such that the edge i -> j exists. An undirected edge is stored twice, once in each list, or stored once with an extra flag. Adding an edge is appending to a list. Asking "is i connected to j?" is a scan of adj[i] unless you also keep a set.
An adjacency matrix is an n * n array. m[i*n + j] is 1 or a weight. Edge query is O(1). Space is O(n^2), which is fine for n = 200 and impossible for n = 1e6. Directed versus undirected is whether you set both [i][j] and [j][i].
Weighted edges store a struct Edge { int to; int w; } in the list. Multiedges and self-loops are allowed by the structure and may or may not be allowed by the algorithm. Document them.
Traversal needs a visited array. Without it, a cycle sends DFS into infinite recursion and BFS into an unbounded queue. visited[i] = 1 before you push neighbors, or after you pop, is a choice that changes whether a node can sit in the queue twice. For BFS shortest unweighted path, mark when you enqueue.
Ownership of the lists is the graph object's job. graph_free walks every list and frees it, then frees the array of lists. If you stored pointers to caller objects as vertex payloads, say who frees those.
Code Examples
#include <stdio.h>
#include <stdlib.h>
struct Graph {
int **adj;
int *deg;
int n;
};
static int graph_init(struct Graph *g, int n)
{
g->n = n;
g->adj = calloc((size_t)n, sizeof *g->adj);
g->deg = calloc((size_t)n, sizeof *g->deg);
if (g->adj == NULL || g->deg == NULL) {
free(g->adj);
free(g->deg);
return -1;
}
return 0;
}
static int graph_add(struct Graph *g, int u, int v)
{
int *p = realloc(g->adj[u], (size_t)(g->deg[u] + 1) * sizeof *p);
if (p == NULL) {
return -1;
}
g->adj[u] = p;
g->adj[u][g->deg[u]++] = v;
return 0;
}
static void graph_free(struct Graph *g)
{
for (int i = 0; i < g->n; i++) {
free(g->adj[i]);
}
free(g->adj);
free(g->deg);
}
int main(void)
{
struct Graph g;
if (graph_init(&g, 3) != 0) {
return 1;
}
graph_add(&g, 0, 1);
graph_add(&g, 1, 2);
printf("%d\n", g.adj[0][0]); /* prints: 1 */
graph_free(&g);
return 0;
}Common Gotchas
DFS or BFS without `visited` loops forever on a cycle
allocate a `char visited[n]` and mark.
An undirected edge forgotten in one direction makes the graph directed by accident
add both, or store undirected edges once and walk a different API.
Vertex ids from input must be bounds-checked
`adj[u]` with `u == n` is a buffer overflow.
`O(n^2)` matrices are simpler and slower to allocate at scale
pick the representation from `n` and the number of edges, not from the textbook default.