Adjacency List vs Matrix When to Choose Which in DSA Javascript - Complexity Comparison
Choosing how to represent a graph affects how fast we can check connections and add edges.
We want to know how the time to do these tasks grows as the graph gets bigger.
Analyze the time complexity of these two graph representations.
// Adjacency Matrix
const matrix = Array(n).fill(null).map(() => Array(n).fill(0));
// Add edge
matrix[u][v] = 1;
// Adjacency List
const list = Array(n).fill(null).map(() => []);
// Add edge
list[u].push(v);
This code shows how to add edges using adjacency matrix and adjacency list.
Look at what repeats when we check or add edges.
- Primary operation: Accessing or updating connections between nodes.
- How many times: For matrix, checking all pairs is n×n; for list, checking neighbors depends on edges.
As nodes (n) grow, matrix operations involve n² checks, list operations depend on edges (e).
| Input Size (n) | Approx. Operations (Matrix) | Approx. Operations (List) |
|---|---|---|
| 10 | 100 (10x10) | Depends on edges, e.g. 20 |
| 100 | 10,000 (100x100) | Depends on edges, e.g. 200 |
| 1000 | 1,000,000 (1000x1000) | Depends on edges, e.g. 2000 |
Matrix grows with square of nodes; list grows with number of edges, often less than n².
Time Complexity: O(n²) for adjacency matrix, O(n + e) for adjacency list
Matrix is slower for large graphs because it checks all pairs; list is faster when edges are fewer.
[X] Wrong: "Adjacency matrix is always better because it uses simple arrays."
[OK] Correct: Matrix wastes time and space when graph has few edges; list is more efficient then.
Understanding these differences helps you pick the right graph style for problems, showing good problem-solving skills.
"What if the graph is dense with many edges? How would the time complexity of adjacency list compare to adjacency matrix?"
Practice
Solution
Step 1: Understand graph density
A graph with many nodes but few edges is called a sparse graph.Step 2: Choose efficient representation for sparse graphs
Adjacency lists store only existing edges, saving space for sparse graphs.Final Answer:
Adjacency List -> Option BQuick Check:
Sparse graph = Adjacency List [OK]
- Choosing adjacency matrix for sparse graphs wastes space
- Confusing adjacency list with adjacency matrix
- Thinking arrays or stacks represent graphs directly
Solution
Step 1: Understand adjacency list structure
An adjacency list is an array where each element is an array of neighbors.Step 2: Check each option
const graph = [[], [], []]; creates separate empty arrays for each node. const graph = new Array(3).fill([]); fills with the same array reference, causing bugs.Final Answer:
const graph = [[], [], []]; -> Option AQuick Check:
Separate empty arrays = const graph = [[], [], []]; [OK]
- Using fill([]) which shares the same array reference
- Using adjacency matrix syntax for adjacency list
- Using an object instead of array for adjacency list
const graph = [ [1, 2], [0], [0] ]; console.log(graph[0]);
Solution
Step 1: Understand the adjacency list structure
graph[0] contains the neighbors of node 0, which are nodes 1 and 2.Step 2: Check the console.log output
Logging graph[0] prints the array [1, 2].Final Answer:
[1, 2] -> Option CQuick Check:
graph[0] = [1, 2] [OK]
- Confusing adjacency list with adjacency matrix output
- Expecting all nodes instead of neighbors
- Mistaking graph[0] as undefined
const graph = new Array(3).fill(new Array(3).fill(0)); graph[0][1] = 1; console.log(graph);
Solution
Step 1: Analyze array initialization
Using fill with new Array(3).fill(0) creates one inner array shared by all rows.Step 2: Effect of modifying graph[0][1]
Changing graph[0][1] changes the same element in all rows because they reference the same array.Final Answer:
The inner arrays are shared references causing all rows to change -> Option AQuick Check:
Shared inner arrays cause unexpected changes [OK]
- Assuming fill creates independent arrays
- Ignoring shared reference problem
- Thinking syntax is wrong instead of logic
Solution
Step 1: Calculate graph density
Maximum edges for 1000 nodes is 1,000,000 (1000*1000). 10,000 edges is sparse.Step 2: Compare memory usage
Adjacency matrix uses memory for all possible edges (1,000,000). Adjacency list uses memory proportional to actual edges (10,000).Step 3: Choose efficient representation
Adjacency list is more memory efficient for sparse graphs.Final Answer:
Adjacency List, because it uses memory proportional to edges -> Option DQuick Check:
Sparse graph = Adjacency List memory efficient [OK]
- Choosing adjacency matrix for sparse graphs wastes memory
- Confusing simplicity with efficiency
- Thinking adjacency list stores all possible edges
Ready to prove it?
Pass the test and this pattern moves unknown
