Adjacency Matrix Representation in DSA Java - 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 time to check or add connections?
Analyze the time complexity of the following code snippet.
public class Graph {
private int[][] matrix;
private int size;
public Graph(int n) {
size = n;
matrix = new int[n][n];
}
public void addEdge(int i, int j) {
matrix[i][j] = 1;
matrix[j][i] = 1; // for undirected graph
}
public boolean hasEdge(int i, int j) {
return matrix[i][j] == 1;
}
}
This code creates a graph using an adjacency matrix, adds edges, and checks if an edge exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing or updating a cell in the matrix (constant time).
- How many times: Each edge addition or check touches only one or two cells, but building or scanning the whole matrix involves n x n cells.
When adding or checking a single edge, time stays the same no matter the graph size.
But if we want to look at all connections, we must check every cell in the n by n matrix.
| Input Size (n) | Approx. Operations (checking all edges) |
|---|---|
| 10 | 100 |
| 100 | 10,000 |
| 1000 | 1,000,000 |
Pattern observation: The work grows by the square of the number of nodes.
Time Complexity: O(n²)
This means the time to process all connections grows quickly as the graph gets bigger.
[X] Wrong: "Checking all edges is just O(n) because we have n nodes."
[OK] Correct: Each node can connect to every other node, so we must check n times n cells, not just n.
Understanding adjacency matrix time helps you explain graph operations clearly and choose the right data structure for the problem.
"What if we used an adjacency list instead of a matrix? How would the time complexity for checking edges change?"
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 edge, 0 for no edge).Final Answer:
A 2D array showing edges between nodes -> Option AQuick Check:
Adjacency matrix = 2D array of edges [OK]
- Confusing adjacency matrix with adjacency list
- Thinking it stores node values instead of edges
- Mixing it up with traversal order
Solution
Step 1: Recall Java 2D array declaration syntax
In Java, a 2D array is declared as type[][] name = new type[rows][columns];Step 2: Match with graph nodes count
For 4 nodes, we need a 4x4 matrix, so new int[4][4] is correct.Final Answer:
int[][] matrix = new int[4][4]; -> Option CQuick Check:
2D array for 4 nodes = new int[4][4] [OK]
- Using single brackets for 2D array
- Declaring 1D array instead of 2D
- Incorrect array initialization syntax
int[][] matrix = {
{0, 1, 0},
{1, 0, 1},
{0, 1, 0}
};
What is the output of the code snippet?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
System.out.print(i + "->" + j + " ");
}
}
}Solution
Step 1: Analyze the matrix values
matrix[0][1] = 1, matrix[1][0] = 1, matrix[1][2] = 1, matrix[2][1] = 1; others are 0.Step 2: Trace the nested loops and print statements
For each 1, print "i->j ": 0->1, 1->0, 1->2, 2->1.Final Answer:
0->1 1->0 1->2 2->1 -> Option DQuick Check:
Edges printed where matrix[i][j] = 1 [OK]
- Printing zeros as edges
- Mixing i and j indices
- Skipping symmetric edges
int[][] matrix = new int[3]; matrix[0][1] = 1;
Solution
Step 1: Check array declaration syntax
int[][] matrix = new int[3]; declares a 1D array of int arrays but does not initialize inner arrays.Step 2: Understand why matrix[0][1] causes error
Inner arrays are null, so accessing matrix[0][1] causes NullPointerException or compile error.Final Answer:
2D array declared incorrectly with single dimension -> Option BQuick Check:
2D array needs both dimensions declared [OK]
- Declaring only one dimension for 2D array
- Not initializing inner arrays
- Confusing array types
Solution
Step 1: Map edges to matrix positions
Edges: 0->1 means matrix[0][1]=1, 0->2 means matrix[0][2]=1, 1->2 means matrix[1][2]=1, 2->3 means matrix[2][3]=1.Step 2: Check each option for these values
[[0,1,1,0],[0,0,1,0],[0,0,0,1],[0,0,0,0]] matches exactly these edges with 1s in correct places and 0s elsewhere.Final Answer:
[[0,1,1,0],[0,0,1,0],[0,0,0,1],[0,0,0,0]] -> Option AQuick Check:
Edges mapped as 1s in matrix [OK]
- Confusing directed edges with undirected
- Placing 1s symmetrically for directed graph
- Missing edges in matrix
Ready to prove it?
Pass the test and this pattern moves unknown
