Adjacency List vs Matrix When to Choose Which in DSA Typescript - Complexity Comparison
Choosing between adjacency list and adjacency matrix affects how fast graph operations run.
We want to know how the time to check or add edges changes as the graph grows.
Analyze the time complexity of checking if an edge exists in two graph representations.
// Using adjacency matrix
function hasEdgeMatrix(matrix: number[][], u: number, v: number): boolean {
return matrix[u][v] === 1;
}
// Using adjacency list
function hasEdgeList(list: number[][], u: number, v: number): boolean {
return list[u].includes(v);
}
This code checks if there is an edge from node u to node v using two different graph storage methods.
Look at what repeats when checking edges.
- Primary operation: For matrix, direct access; for list, searching through neighbors.
- How many times: Matrix access is once; list search depends on number of neighbors of u.
How does checking edges grow as graph size grows?
| Input Size (n nodes) | Matrix Accesses | List Searches (neighbors) |
|---|---|---|
| 10 | 1 | Up to 9 |
| 100 | 1 | Up to 99 |
| 1000 | 1 | Up to 999 |
Matrix access stays constant; list search grows with number of neighbors, which depends on graph density.
Time Complexity: O(1) for adjacency matrix, O(k) for adjacency list
This means matrix checks edges instantly, while list checks take time proportional to neighbors count.
[X] Wrong: "Adjacency list is always faster than matrix because it uses less space."
[OK] Correct: List uses less space but edge checks can be slower if many neighbors exist, unlike constant-time matrix access.
Understanding these differences helps you pick the right graph storage for fast operations in real problems.
"What if the graph is very sparse? How does that affect the choice between adjacency list and matrix for edge checks?"
Practice
Solution
Step 1: Understand graph density
Sparse graphs have few edges compared to vertices, so storing all possible edges wastes space.Step 2: Compare space usage of adjacency list and matrix
Adjacency list stores only existing edges, using less space for sparse graphs. Adjacency matrix always uses space for all possible edges.Final Answer:
Adjacency List -> Option AQuick Check:
Sparse graph = Adjacency List [OK]
- Choosing adjacency matrix for sparse graphs
- Confusing edge count with vertex count
- Ignoring space complexity differences
Solution
Step 1: Understand adjacency matrix type
An adjacency matrix is a 2D array (array of arrays) of numbers representing edges.Step 2: Check initialization correctness
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; explicitly creates a 3x3 matrix with zeros. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0)); uses fill with the same inner array reference, causing bugs. const matrix: number[][] = Array(3).map(() => Array(3).fill(0)); uses map on empty array, which won't work as expected. const matrix: number[] = [0,0,0]; is 1D array, not matrix.Final Answer:
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; -> Option CQuick Check:
Explicit 2D array with zeros = const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; [OK]
- Using fill with nested arrays causing shared references
- Declaring 1D array instead of 2D
- Using map on empty array without initialization
const graph = {
0: [1, 2],
1: [2],
2: [0, 3],
3: []
};
console.log(graph[2]);What will be the output?
Solution
Step 1: Identify the adjacency list for vertex 2
In the graph object, key '2' maps to the array [0, 3].Step 2: Understand console.log output
Logging graph[2] prints the array [0, 3] representing edges from vertex 2.Final Answer:
[0, 3] -> Option DQuick Check:
graph[2] = [0, 3] [OK]
- Confusing keys and values in adjacency list
- Expecting adjacency matrix output
- Assuming undefined for existing keys
const size = 3; const matrix = new Array(size).fill(new Array(size).fill(0)); matrix[0][1] = 1; console.log(matrix);
What is the problem with this code?
Solution
Step 1: Analyze the fill method usage
Using fill with new Array(size).fill(0) creates one inner array and fills outer array with references to the same inner array.Step 2: Understand effect of modifying one element
Changing matrix[0][1] changes that element in all rows because they share the same inner array reference.Final Answer:
All rows refer to the same inner array, so all rows change when one element changes -> Option AQuick Check:
Shared inner array reference causes unexpected changes [OK]
- Thinking fill creates separate inner arrays
- Ignoring shared reference problem
- Assuming matrix is 1D array
Solution
Step 1: Understand graph density and operation needs
Dense graph means many edges; checking edge existence frequently requires fast lookup.Step 2: Compare adjacency list and matrix for edge checks
Adjacency matrix allows direct O(1) access to check if edge exists between two vertices. Adjacency list requires searching neighbors, which can be slower in dense graphs.Step 3: Consider space tradeoff
Though adjacency matrix uses more space, for dense graphs this is acceptable and beneficial for speed.Final Answer:
Adjacency Matrix, because it allows O(1) edge existence checks -> Option BQuick Check:
Dense graph + frequent edge check = Adjacency Matrix [OK]
- Choosing adjacency list for dense graphs needing fast edge checks
- Ignoring time complexity of edge existence queries
- Confusing edge list with adjacency matrix benefits
Ready to prove it?
Pass the test and this pattern moves unknown
