Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 3 · syllabusAsked 1× in KTU papersLesson live

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
ormakkurippu ✎
Never-forget card

MATRIX = a big n × n attendance register (fast lookup, lots of zeros). LIST = each vertex keeps its own contact list (saves space).

Picture it

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.

Write this in the exam

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.

01342
The example graph (5 vertices, 6 edges).

Adjacency matrix

01234
001100
110110
211001
301001
400110
Symmetric. Row sum = degree (vertex 1 has degree 3). For a weighted graph, store the weight instead of 1.

Adjacency list

Each undirected edge appears twice (once in each endpoint's list): 2e = 12 nodes.
VertexAdjacent vertices (linked list)
01 → 2
10 → 2 → 3
20 → 1 → 4
31 → 4
42 → 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 ◀ ▶.

Loading visualizer…

C implementation

graph_traversal.ctested ✓ 250/250 cases
C
/* 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

Open in the Code Lab →

Which one to choose

Adjacency matrixAdjacency list
SpaceO(n²)O(n + e)
Edge (u, v) exists?O(1)O(degree(u))
List all neighbours of uO(n)O(degree(u))
BFS / DFSO(n²)O(n + e)
Best fordense graphssparse graphs (most real graphs)

Asked in KTU exams (1)

Quick check — can you get them all?

Question 1

Q1.Space for an adjacency list of a graph with n vertices and e edges?

Question 2

Q2.Is there an edge between u and v? Fastest with…

Question 3

Q3.The adjacency matrix of an undirected graph is always…

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)