Graph representation
Adjacency matrix vs adjacency list — how to store a graph and when to choose which.
Syllabus says: “Representation of Graphs”
On this page · 10 sections
MATRIX = a big n × n attendance register (fast lookup, lots of zeros). LIST = each vertex keeps its own contact list (saves space).
A matrix is like the class register with every student's name on both axes; a list is like each student's phone saving only their own friends' numbers.
An adjacency matrix is an n × n array with a[i][j] = 1 if edge (i, j) exists; an adjacency list keeps, for each vertex, a linked list of its adjacent vertices.
What you will be able to do
- Explain two graph representations with examples (May 2026 Q6).
- Compare their space and time costs.
One graph, two representations
Undirected graph with vertices 0–4 and edges 0–1, 0–2, 1–2, 1–3, 2–4, 3–4.
Adjacency matrix
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 | 0 |
| 2 | 1 | 1 | 0 | 0 | 1 |
| 3 | 0 | 1 | 0 | 0 | 1 |
| 4 | 0 | 0 | 1 | 1 | 0 |
Adjacency list
| Vertex | Adjacent vertices (linked list) |
|---|---|
| 0 | 1 → 2 |
| 1 | 0 → 2 → 3 |
| 2 | 0 → 1 → 4 |
| 3 | 1 → 4 |
| 4 | 2 → 3 |
Now watch it run
Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶. Keys: Space play/pause, ← → step.
C implementation
graph_traversal.ctested ✓ 250/250 cases/* BFS and DFS on an adjacency matrix — KTU PCCST303, Module 3 (KTU Nov 2025 Q14a, May 2026 Q14a)
* Neighbours are taken in increasing vertex order, which makes the visit order unique —
* the convention KTU answer keys follow.
* Input : n m directed(0|1) then m edges "u v" (0..n-1) then the start vertex.
* Output: "BFS: ..." and "DFS: ..." (only vertices reachable from the start). */
#include <stdio.h>
#include <string.h>
#define MAXV 20
int adj[MAXV][MAXV], n, visited[MAXV];
void bfs(int s) {
int queue[MAXV], front = 0, rear = 0, u, v;
memset(visited, 0, sizeof visited);
visited[s] = 1;
queue[rear++] = s; /* mark when ENQUEUED, not when dequeued */
printf("BFS:");
while (front < rear) {
u = queue[front++];
printf(" %d", u);
for (v = 0; v < n; v++)
if (adj[u][v] && !visited[v]) { visited[v] = 1; queue[rear++] = v; }
}
printf("\n");
}
void dfs(int u) {
int v;
visited[u] = 1;
printf(" %d", u);
for (v = 0; v < n; v++)
if (adj[u][v] && !visited[v]) dfs(v); /* go deeper before trying the next neighbour */
}
int main(void) {
int m, directed, u, v, s, i;
if (scanf("%d %d %d", &n, &m, &directed) != 3 || n < 1 || n > MAXV) { printf("BAD INPUT\n"); return 1; }
for (i = 0; i < m; i++) {
if (scanf("%d %d", &u, &v) != 2 || u < 0 || v < 0 || u >= n || v >= n) { printf("BAD INPUT\n"); return 1; }
adj[u][v] = 1;
if (!directed) adj[v][u] = 1;
}
if (scanf("%d", &s) != 1 || s < 0 || s >= n) { printf("BAD INPUT\n"); return 1; }
bfs(s);
memset(visited, 0, sizeof visited);
printf("DFS:"); dfs(s); printf("\n");
return 0;
}
Sample run · lesson graph, start 0 · real output from gcc
Input (stdin)
5 6 0 0 1 0 2 1 2 1 3 2 4 3 4 0
Output (stdout)
BFS: 0 1 2 3 4 DFS: 0 1 2 4 3
Which one to choose
| Adjacency matrix | Adjacency list | |
|---|---|---|
| Space | O(n²) | O(n + e) |
| Edge (u, v) exists? | O(1) | O(degree(u)) |
| List all neighbours of u | O(n) | O(degree(u)) |
| BFS / DFS | O(n²) | O(n + e) |
| Best for | dense graphs | sparse graphs (most real graphs) |
Asked in KTU exams (1)
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
- Matrix: n × n, a[i][j] = 1 (or weight); symmetric if undirected; O(n²) space.
- List: array of linked lists; O(n + e) space.
- Dense → matrix; sparse → list.
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §6.1.3 (Graph representations)
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — §22.1
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)