Adjacency Matrix Representation in DSA Javascript - Time & Space Complexity
We want to understand how the time to work with an adjacency matrix changes as the graph grows.
How does the number of nodes affect the steps needed to check or update connections?
Analyze the time complexity of the following code snippet.
// Create adjacency matrix for n nodes
function createMatrix(n) {
const matrix = [];
for (let i = 0; i < n; i++) {
matrix[i] = [];
for (let j = 0; j < n; j++) {
matrix[i][j] = 0;
}
}
return matrix;
}
This code builds an n by n matrix filled with zeros to represent no edges between nodes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops filling each cell of the matrix.
- How many times: Outer loop runs n times, inner loop runs n times for each outer loop, total n x n = n² times.
As the number of nodes increases, the steps to fill the matrix grow by the square of that number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 |
| 100 | 10,000 |
| 1000 | 1,000,000 |
Pattern observation: Doubling the nodes roughly quadruples the work needed.
Time Complexity: O(n²)
This means the time needed grows quickly as the graph gets bigger, because we check every possible pair of nodes.
[X] Wrong: "Creating the matrix takes only O(n) time because we just loop once."
[OK] Correct: Because we have two loops, one inside the other, the total steps multiply, making it O(n²), not just O(n).
Understanding this helps you explain how graph data structures work and why some operations can be slower with certain representations.
"What if we used an adjacency list instead of a matrix? How would the time complexity for creating the graph change?"
Practice
Solution
Step 1: Understand adjacency matrix structure
An adjacency matrix is a 2D array where rows and columns represent nodes.Step 2: Identify what the matrix stores
Each cell shows if there is a connection (edge) between two nodes.Final Answer:
A 2D array showing connections between nodes -> Option AQuick Check:
Adjacency matrix = 2D array of connections [OK]
- Confusing adjacency matrix with adjacency list
- Thinking it stores node values instead of connections
- Assuming it is a sequence or tree
Solution
Step 1: Understand adjacency matrix as 2D array
It must be an array of arrays, each inner array representing a row.Step 2: Check each option's structure
const matrix = [[0,0,0],[0,0,0],[0,0,0]]; is a 3x3 array filled with zeros, correct for adjacency matrix initialization.Final Answer:
const matrix = [[0,0,0],[0,0,0],[0,0,0]]; -> Option BQuick Check:
2D array syntax = const matrix = [[0,0,0],[0,0,0],[0,0,0]]; [OK]
- Using a single array instead of 2D array
- Using object instead of array
- Creating array without inner arrays
const matrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ]; console.log(matrix[1][2]);
Solution
Step 1: Identify indices in matrix
matrix[1] is the second row: [1, 0, 1].Step 2: Access element at column 2
matrix[1][2] is the third element in second row, which is 1.Final Answer:
1 -> Option AQuick Check:
matrix[1][2] = 1 [OK]
- Confusing row and column indices
- Assuming 1-based indexing
- Expecting undefined for valid indices
const matrix = [ [0, 0], [0, 0] ]; matrix[0][1] = 1; matrix[1][2] = 1;
Solution
Step 1: Check matrix dimensions
Matrix has 2 rows and 2 columns (indices 0 and 1 only).Step 2: Identify invalid index access
matrix[1][2] tries to access third element in second row, which does not exist.Final Answer:
Index 2 is out of bounds for matrix[1] -> Option CQuick Check:
Accessing invalid column index = Error [OK]
- Ignoring array size limits
- Assuming dynamic resizing on assignment
- Confusing row and column indices
Solution
Step 1: Understand undirected edge representation
Undirected edges are symmetric, so both directions must be marked.Step 2: Mark both connections in matrix
Set matrix[1][3] = 1 and matrix[3][1] = 1 to show connection both ways.Final Answer:
Set matrix[1][3] = 1 and matrix[3][1] = 1 -> Option DQuick Check:
Undirected edge = symmetric matrix entries [OK]
- Setting only one direction for undirected edge
- Setting diagonal elements instead of edge
- Confusing node indices
Ready to prove it?
Pass the test and this pattern moves unknown
