Adjacency Matrix & List Visualizer

Compare adjacency matrices and adjacency lists as a graph changes, with synchronized views, interactive controls, and memory insights.

Current status: Click any matrix cell to add or remove its edge.
Balanced

Representation console

Create vertices, edit edges, then inspect how storage changes.

Editor ready

Vertices

Edge editor

4 vertices4 edges25% density

Click any matrix cell to add or remove its edge.

O(V²) storage · O(1) lookup

Adjacency Matrix

A
B
C
D
A
B
C
D

Click a cell to toggle its edge · diagonal cells represent self-loops

Visual graph

Drag nodes to rearrange

activevisited

O(V + E) storage

Adjacency List

A
B
C
D

Hover entries to locate their matrix cell and graph edge

Matrix or list?

Choose a matrix when

The graph is dense, constant-time edge checks matter, or a compact 2D representation makes an algorithm easier to reason about. Its cost is always V² cells, even when most contain zero.

Choose a list when

The graph is sparse or algorithms frequently iterate through neighbors. Storage grows with V + E, while checking one specific edge may require scanning a vertex’s neighbors.

Concept guide

Review the mental model, tradeoffs, and practical use cases after you experiment.

Graph Representations

Adjacency MatrixAdjacency List

Two fundamental ways to represent graphs with different trade-offs. Adjacency Matrix uses a 2D array for O(1) edge lookups but O(V²) space, while Adjacency List uses arrays of lists for O(V + E) space but slower edge queries. Choice depends on graph density and operation frequency.

Adjacency Matrix

Space Complexity

V × V matrix required

O(V²)

Edge Lookup

Constant time access

O(1)

Add/Remove Vertex

Resize matrix required

O(V²)

Add/Remove Edge

Update single cell

O(1)

Memory Usage

Stores all possible edges

High

Best For

Many edges per vertex

Dense Graphs

Adjacency List

Space Complexity

Vertices + edges storage

O(V + E)

Edge Lookup

Scan adjacency list

O(degree)

Add/Remove Vertex

Add to array/map

O(1)

Add/Remove Edge

Append to list

O(1)

Memory Usage

Only stores existing edges

Low

Best For

Few edges per vertex

Sparse Graphs

Operation Comparison

OperationAdjacency MatrixAdjacency List
Check Edge ExistenceO(1) - matrix[i][j]O(degree) - scan list
Get All NeighborsO(V) - scan rowO(degree) - direct access
Add VertexO(V²) - resize matrixO(1) - add to dictionary
Remove VertexO(V²) - resize matrixO(E) - remove from all lists
Add EdgeO(1) - set cell valueO(1) - append to list
Remove EdgeO(1) - clear cellO(degree) - search and remove

Implementation Examples

LanguageMatrixList
Python[[0]*V for _ in range(V)]{i: [] for i in range(V)}
Javaint[][] adj = new int[V][V]List<List<Integer>> adj = new ArrayList<>()
C++vector<vector<int>> adj(V, vector<int>(V))vector<vector<int>> adj(V)
JavaScriptArray(V).fill().map(() => Array(V).fill(0))Array(V).fill().map(() => [])

Use Case Recommendations

Social Networks

Sparse connections, many users

→ Adjacency List

Game Maps/Grids

Dense connections, fixed structure

→ Adjacency Matrix

Web Crawling

Billions of pages, few links

→ Adjacency List

Circuit Design

Many components, dense connections

→ Adjacency Matrix

Routing Algorithms

Focus on existing connections

→ Adjacency List

Variations & Extensions

TypeImplementationSpace Complexity
Weighted MatrixStore weights in cells, 0/-1 for no edgeO(V²) - same as unweighted
Weighted ListStore (neighbor, weight) pairsO(V + E) - efficient for sparse
Directed GraphsAsymmetric matrix

When to Choose Matrix

  • Dense graphs (E ≈ V²)
  • Frequent edge existence checks
  • Graph algorithms using matrix operations
  • Static graphs with fixed vertex set

When to Choose List

  • Sparse graphs
  • Memory-constrained environments
  • Dynamic graphs with frequent vertex additions
  • Algorithms that traverse all edges
Pro Tip: Use the density threshold: if E > V²/4, prefer adjacency matrix for better performance. For most real-world graphs (social networks, web graphs), adjacency list is superior due to sparsity. Consider hybrid approaches for very large graphs.
Space vs Time Trade-offGraph Density MattersOperation Frequency