Adjacency List vs Matrix When to Choose Which in DSA Java - Complexity Comparison
Choosing between adjacency list and adjacency matrix affects how fast graph operations run.
We want to know how the time to check or list connections grows as the graph gets bigger.
Analyze the time complexity of checking if an edge exists in two graph representations.
// Using adjacency matrix
boolean hasEdgeMatrix(int u, int v, int[][] matrix) {
return matrix[u][v] == 1;
}
// Using adjacency list
boolean hasEdgeList(int u, int v, List<List<Integer>> list) {
for (int node : list.get(u)) {
if (node == v) return true;
}
return false;
}
This code checks if there is a connection from node u to node v using two ways to store graphs.
Look at what repeats when checking edges.
- Primary operation: Accessing or searching neighbors of node u.
- How many times: Matrix checks once directly; list may check all neighbors of u.
As the graph grows, the time to check edges changes differently for each method.
| Input Size (n) | Matrix Checks | List Checks (avg neighbors) |
|---|---|---|
| 10 | 1 operation | about 2-3 operations |
| 100 | 1 operation | about 5-10 operations |
| 1000 | 1 operation | about 10-20 operations |
Matrix check stays the same; list check grows with how many neighbors node u has.
Time Complexity: O(1) for adjacency matrix, O(k) for adjacency list
This means matrix checks edges instantly, while list checks depend on number of neighbors k.
[X] Wrong: "Adjacency list always faster because it uses less space."
[OK] Correct: List can be slower to check edges if node has many neighbors, while matrix is always quick to check.
Understanding these differences helps you pick the right graph storage for fast operations in real problems.
What if the graph is very dense? How would the time complexity of adjacency list edge checks compare to adjacency matrix?
Practice
Solution
Step 1: Understand graph density
A graph with many vertices but few edges is called a sparse graph.Step 2: Choose efficient representation for sparse graphs
Adjacency List stores only existing edges, saving space for sparse graphs.Final Answer:
Adjacency List -> Option AQuick Check:
Sparse graph = Adjacency List [OK]
- Choosing adjacency matrix for sparse graphs wastes space
- Confusing adjacency list with linked list
- Ignoring graph density when choosing representation
Solution
Step 1: Understand adjacency matrix structure
An adjacency matrix for 4 vertices is a 4x4 2D array.Step 2: Check Java syntax for 2D arrays
Correct declaration uses int[][] with new int[4][4].Final Answer:
int[][] adjMatrix = new int[4][4]; -> Option AQuick Check:
2D array syntax = int[][] adjMatrix = new int[4][4]; [OK]
- Using single brackets for 2D array
- Missing array type brackets
- Declaring as single dimension array
int[][] matrix = new int[3][3]; matrix[0][1] = 1; matrix[1][0] = 1; matrix[1][2] = 1; matrix[2][1] = 1; System.out.println(matrix[0][2]);
What is the output?
Solution
Step 1: Analyze matrix initialization and assignments
Matrix is 3x3 initialized with zeros. Edges set between 0-1 and 1-2.Step 2: Check value at matrix[0][2]
No edge assigned between 0 and 2, so matrix[0][2] remains 0.Final Answer:
0 -> Option DQuick Check:
matrix[0][2] = 0 (no edge) [OK]
- Assuming symmetric edges without assignment
- Expecting 1 for indirect connections
- Confusing output with exceptions
List<Integer>[] adjList = new ArrayList[3]; adjList[0].add(1);
What is the error and how to fix it?
Solution
Step 1: Identify cause of NullPointerException
Array created but each element is null; no ArrayList objects initialized.Step 2: Fix by initializing each list
Use a loop to assign new ArrayList() to each adjList[i] before adding edges.Final Answer:
NullPointerException; initialize each list before adding -> Option BQuick Check:
NullPointerException = uninitialized lists [OK]
- Forgetting to create ArrayList objects
- Assuming array creation initializes lists
- Ignoring runtime exceptions
Solution
Step 1: Analyze graph density
1000 vertices and 10,000 edges means graph is sparse (max edges ~ 499,500).Step 2: Consider edge existence check speed
Adjacency matrix allows O(1) edge existence check by direct index access.Step 3: Balance space vs speed
Though matrix uses more space, frequent edge checks favor adjacency matrix for speed.Final Answer:
Adjacency Matrix, because it allows O(1) edge checks despite higher space -> Option CQuick Check:
Frequent edge checks = adjacency matrix [OK]
- Choosing adjacency list for O(1) edge checks (it's O(k))
- Ignoring space vs time tradeoff
- Confusing sparse and dense graph definitions
Ready to prove it?
Pass the test and this pattern moves unknown
