Discover how choosing the right way to store connections can save you from a mountain of confusion and wasted effort!
Adjacency List vs Matrix When to Choose Which in DSA Javascript - Why the Distinction Matters
Imagine you want to keep track of friendships in a large school. You try to write down every possible pair of students and whether they are friends or not on a big sheet of paper.
This method quickly becomes overwhelming and confusing because the sheet grows huge, and most pairs are not friends, so you waste a lot of space and time checking every pair.
Using an adjacency list or matrix helps organize this information smartly. An adjacency list only notes actual friendships, saving space when there are few connections. An adjacency matrix uses a grid to quickly check if two students are friends, which is faster when many friendships exist.
const friendships = new Array(1000).fill(null).map(() => new Array(1000).fill(false)); // Checking friendship needs scanning for(let i=0; i<1000; i++) { for(let j=0; j<1000; j++) { if(friendships[i][j]) console.log(i, 'is friends with', j); } }
const adjacencyList = Array.from({length: 1000}, () => []); adjacencyList[0].push(1); // 0 is friends with 1 console.log(adjacencyList[0]); // Quickly see friends of 0
This choice lets you store and find connections efficiently, saving time and memory depending on how many connections exist.
Social media platforms use adjacency lists to store friends when users have few connections, and adjacency matrices when groups are tightly connected, to quickly suggest new friends.
Manual tracking of connections wastes space and time.
Adjacency lists save space for few connections.
Adjacency matrices speed up checks for many connections.
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
