A graph is a data structure made of vertices (nodes) connected by edges, used to model networks such as roads, social connections and dependencies. C has no built-in graph type, so you build one yourself, most often as an adjacency list (an array of linked lists) or an adjacency matrix (a two-dimensional array).
The program below implements an undirected graph with an adjacency list, then traverses it with breadth-first search (BFS) and depth-first search (DFS). It is written in C17 with explicit memory management and error checks.
Choose the representation by how dense the graph is and which operations you need most.
| Property | Adjacency list | Adjacency matrix |
|---|---|---|
| Memory | O(V + E) | O(V^2) |
| Check if edge (u, v) exists | O(degree of u) | O(1) |
| Iterate neighbours of u | O(degree of u) | O(V) |
| Best for | Sparse graphs | Dense graphs, fast edge lookup |
Most real graphs are sparse, so the adjacency list is the default. A matrix is simple when the vertex count is small and fixed:
For a weighted graph, store the weight instead of 1 and use a sentinel (such as 0 or a large value) for "no edge".
Each vertex owns a singly linked list of its neighbours. For an undirected graph, adding an edge inserts each endpoint into the other's list.
For a directed graph, call add_arc once. graph_free walks every list and frees each node, then the arrays; forgetting this is the most common source of leaks in C graph code. Note that neighbours appear in reverse insertion order, so traversal output depends on the order you add edges.
BFS visits vertices in order of their distance (in edges) from the start, using a queue. It finds shortest paths in unweighted graphs. The queue below is a plain array, which is enough because each vertex is enqueued at most once.
Marking a vertex visited when it is enqueued, not when it is dequeued, prevents duplicates in the queue. Time is O(V + E) and extra space is O(V).
DFS follows one path as deep as it can before backtracking. The recursive version is short; for very deep graphs, recursion can overflow the stack, so use an explicit stack instead.
Put all the snippets of the adjacency-list sections in one file and compile with gcc -std=c17 -O2 -Wall -Wextra graph.c -o graph. Because neighbours are inserted at the front, the expected output is:
To visit all components of a disconnected graph, loop over every vertex and start a traversal from each one that is not yet visited. DFS also underlies cycle detection, topological sorting and finding connected components.
Use a matrix for small or dense graphs, or when you need constant-time edge lookups. For large sparse graphs the list saves memory and speeds up neighbour iteration.
Add a weight field to the list node, or store weights in the matrix. Algorithms such as Dijkstra's shortest path then use those weights with a priority queue.
BFS explores level by level with a queue and finds shortest paths in unweighted graphs. DFS goes deep first with recursion or a stack and suits cycle detection and topological sorting.
Free every node in every list, then the head array, then the graph struct, and check each malloc. Run the program under AddressSanitizer or Valgrind to confirm.
A graph in C is usually an array of linked lists, one per vertex, giving O(V + E) memory and efficient neighbour traversal. Use an adjacency matrix only for small or dense graphs. BFS with a queue and DFS with recursion or a stack both run in O(V + E), and careful allocation checks plus a matching free function keep the code safe.