Skip to main content
← Advanced CS Study Guide

Code Scholars Tutoring · Advanced CS

8.1 Representation

How it works

An adjacency list uses space proportional to vertices plus edges; an adjacency matrix uses the square of the vertices regardless of how few edges exist.

Choose the list for sparse graphs and the matrix when edge lookup between two named vertices must be constant.

Common error

Quoting a traversal cost without saying which representation it assumes. The answer differs.

Worked example

Example

Undirected edges: A-B, A-C, B-D, C-D
Adjacency of A: B, C
Adjacency of D: B, C
Each undirected edge appears at both endpoints

An adjacency list stores neighbors and is compact for sparse graphs. An adjacency matrix gives direct edge-membership checks but uses space proportional to the square of the vertex count.

Edges are A-B, A-C, B-D, and C-D.
Edges A-B, A-C, B-D, and C-D; visit order depends on traversal strategy and neighbor order.

Exercises

8.1.1 · Explain

For 5 vertices and 4 undirected edges, how many neighbor entries does a standard adjacency list store?

Show solution

There are 8 neighbor entries, two per undirected edge, in addition to the vertex-list structure.

8.1.2 · Explain

Compare adjacency-list and adjacency-matrix space in terms of V and E.

Show solution

An adjacency list uses O(V + E) space. An adjacency matrix uses O(V squared) space.

Open Java compiler

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic