Data Structures Β· Visualization

Graph: Representations (Adjacency Matrix vs List)

Graph

A graph can be stored as an adjacency matrix (an nΓ—n grid marking which vertex pairs are connected) or an adjacency list (each vertex keeps a list of its neighbors). Time: Matrix: O(1) edge check, O(V) to list neighbors; List: O(V) edge check worst case, O(degree) to list neighbors. Space: Matrix: O(V^2); List: O(V + E).

Big-O: BFS/DFS visit each vertex and edge once: O(V + E).

Graph: Representations (Adjacency Matrix vs List)

Graph

A graph can be stored as an adjacency matrix (an nΓ—n grid marking which vertex pairs are connected) or an adjacency list (each vertex keeps a list of its neighbors). The choice trades memory for query speed depending on how dense the graph is.

How Representations (Adjacency Matrix vs List) Works

  1. Adjacency matrix: create an nΓ—n 2D array; set matrix[u][v] = 1 (or weight) whenever an edge exists between u and v.
  2. Adjacency matrix edge lookup 'does edge (u,v) exist?' is a single O(1) array access.
  3. Adjacency list: create an array of n lists; for each edge (u,v), append v to list[u] (and u to list[v] if undirected).
  4. Adjacency list traversal of a vertex's neighbors only visits its actual neighbors, not all n vertices.

Representations (Adjacency Matrix vs List) Worked Example

Input: Represent a 4-node graph with edges (0,1), (0,2), (1,2)

  1. Matrix (4x4): row0=[0,1,1,0], row1=[1,0,1,0], row2=[1,1,0,0], row3=[0,0,0,0]
  2. List: 0β†’[1,2], 1β†’[0,2], 2β†’[0,1], 3β†’[]
  3. Checking edge (0,3): matrix lookup is O(1) β†’ 0 (no edge); list scan of list[0] is O(degree) β†’ not found

Output: Same graph, two representations with different space/speed trade-offs

Graph: Introduction
A set of nodes connected by edges β€” edges can be one-way or two-way, and optionally carry a weight (cost/distance).
A
B
C
D
E
Unvisited Visited / discovered Current Start
Graph: Introduction
Press β–Ά Run to watch it step by step.
Python Β· Graphs in Code
vertices = ["A", "B", "C", "D"]
edges = [("A", "B", 4), ("A", "C", 2)]

adj_list = {v: [] for v in vertices}
for a, b, w in edges:            # adjacency list
    adj_list[a].append((b, w))

adj_matrix = [[0] * len(vertices) for _ in vertices]
for a, b, w in edges:            # adjacency matrix
    adj_matrix[vertices.index(a)][vertices.index(b)] = w

print(adj_list)
print(adj_matrix)                # O(V^2) space, O(1) edge check
# example: repr
Input: Represent a 4-node graph with edges (0,1), (0,2), (1,2)

Behind the Scenes Execution

Press β–Ά Run in the visualizer β€” the call stack, registers and memory will replay here in sync with the code, line by line.

Representations (Adjacency Matrix vs List) Time & Space Complexity

TimeMatrix: O(1) edge check, O(V) to list neighbors; List: O(V) edge check worst case, O(degree) to list neighbors
SpaceMatrix: O(V^2); List: O(V + E)

When to Use Representations (Adjacency Matrix vs List)

  • Dense graphs (many edges) favor matrices
  • Sparse graphs (few edges) favor lists β€” most real-world graphs
  • Algorithm choice depends on representation (Floyd-Warshall wants a matrix, BFS/DFS prefer a list)

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired β€” free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action β€” visualize the algorithms that operate on these structures, or take a guided course.