Discover how a simple table can turn a confusing web of connections into a clear map you can read instantly!
Why Adjacency Matrix Representation in DSA Javascript?
Imagine you want to keep track of friendships in a group of people using a big table where each row and column represents a person. You try to remember who is friends with whom by writing 'yes' or 'no' in each cell manually.
This manual table is huge and confusing when the group grows. It's easy to make mistakes, forget connections, or waste time checking if two people are friends because you have to scan the whole table.
An adjacency matrix is a neat table where each cell automatically shows if two people (nodes) are connected (friends). It makes checking connections fast and clear, even for big groups.
const friends = [['Alice', 'Bob'], ['Bob', 'Charlie']]; // Manually check if Alice and Charlie are friends by scanning the list
const adjacencyMatrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ]; // Check if Alice and Charlie are friends by looking at adjacencyMatrix[0][2]
It enables quick and easy checks of connections between any two points in a network, making complex relationships simple to manage.
Social media platforms use adjacency matrices to quickly find if two users are connected or to suggest new friends.
Manual tracking of connections is slow and error-prone.
Adjacency matrix stores connections in a clear, fixed-size table.
It allows instant lookup of relationships between nodes.
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
