What if you could instantly know if two people are connected without searching through endless lists?
Why Adjacency Matrix Representation in DSA C?
Imagine you want to keep track of friendships in a group of 100 people by writing down who is friends with whom on a big sheet of paper.
You try to list every pair manually to see if they are friends or not.
Writing down every pair manually is slow and confusing.
It is easy to miss some pairs or write wrong information.
Finding if two people are friends takes searching through a long list.
An adjacency matrix is like a neat table where rows and columns represent people.
Each cell tells if two people are friends with a simple yes or no.
This makes checking friendships quick and organized.
int isFriend(int personA, int personB, int friendships[][2], int size) { for (int i = 0; i < size; i++) { if ((friendships[i][0] == personA && friendships[i][1] == personB) || (friendships[i][0] == personB && friendships[i][1] == personA)) { return 1; } } return 0; }
int adjacencyMatrix[MAX][MAX];
int isFriend(int personA, int personB) {
return adjacencyMatrix[personA][personB];
}It enables instant answers to connection questions between any two points in a network.
Social media platforms use adjacency matrices to quickly find if two users are connected or to suggest new friends.
Manual listing of connections is slow and error-prone.
Adjacency matrix stores connections in a clear, fast-access table.
It makes checking relationships between nodes very quick.
Practice
Solution
Step 1: Understand adjacency matrix concept
An adjacency matrix is a 2D array where rows and columns represent nodes.Step 2: Identify what the matrix stores
Each cell shows if an edge exists between two nodes (1 for connected, 0 for not).Final Answer:
A 2D array showing connections between nodes -> Option AQuick Check:
Adjacency matrix = 2D array [OK]
- Confusing adjacency matrix with adjacency list
- Thinking it stores node values instead of edges
- Assuming it is a sequence or tree
Solution
Step 1: Recall C array declaration syntax
In C, a 2D array is declared as type name[rows][columns].Step 2: Match syntax for 4x4 matrix
For 4 rows and 4 columns, it is int matrix[4][4];Final Answer:
int matrix[4][4]; -> Option BQuick Check:
2D array syntax = int matrix[4][4]; [OK]
- Using single dimension array for matrix
- Using comma inside brackets (invalid syntax)
- Not specifying both dimensions
int matrix[3][3] = {
{0, 1, 0},
{1, 0, 1},
{0, 1, 0}
};
Which nodes are directly connected to node 1 (index 1)?Solution
Step 1: Identify row for node 1
Node 1 corresponds to index 1, so look at matrix[1]: {1, 0, 1}.Step 2: Find indices with value 1
Positions 0 and 2 have 1, meaning node 1 connects to nodes 0 and 2.Final Answer:
[0, 2] -> Option CQuick Check:
matrix[1][0] and matrix[1][2] = 1 [OK]
- Confusing row and column indices
- Including the node itself as connected
- Reading zeros as connections
int matrix[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = 0;
}
}
matrix[1][3] = 1;
What is the error in this code?Solution
Step 1: Check loop ranges and matrix size
Matrix is 3x3, so valid indices are 0 to 2 for both rows and columns.Step 2: Identify invalid index usage
matrix[1][3] tries to access column index 3, which is out of bounds.Final Answer:
Accessing out-of-bounds index matrix[1][3] -> Option AQuick Check:
Index 3 invalid for size 3 array [OK]
- Ignoring array bounds and using invalid indices
- Assuming loops fix out-of-range errors
- Confusing row and column indices
int matrix[4][4] = {
{0, 1, 0, 0},
{0, 0, 1, 0},
{1, 0, 0, 0},
{0, 0, 0, 0}
};
Which nodes have no incoming edges?Solution
Step 1: Understand incoming edges in adjacency matrix
Incoming edges to a node are shown by 1s in that node's column.Step 2: Check each column for 1s
Column 0 has 1 at row 2 (node 2 -> node 0).
Column 1 has 1 at row 0 (node 0 -> node 1).
Column 2 has 1 at row 1 (node 1 -> node 2).
Column 3 has no 1s (all 0s).Step 3: Identify nodes with no incoming edges
Nodes 1 and 3 have no incoming edges because column 1 has 1 at row 0 (so node 1 has incoming edge), so this is incorrect. Actually, column 1 has 1 at row 0, so node 1 has incoming edge. Column 3 has no 1s, so node 3 has no incoming edges. Node 0 has incoming edge from node 2. Node 2 has incoming edge from node 1. So only node 3 has no incoming edges.Final Answer:
[3] -> Option DQuick Check:
Column 3 all 0s [OK]
- Checking rows instead of columns for incoming edges
- Confusing directed and undirected edges
- Missing edges from nodes to themselves
Ready to prove it?
Pass the test and this pattern moves unknown
