Adjacency List vs Matrix When to Choose Which in DSA Rust - Complexity Comparison
We want to understand how the choice between adjacency list and adjacency matrix affects the speed of graph operations.
Which method is faster depends on the graph size and connections.
Analyze the time complexity of checking if an edge exists between two nodes.
// Using adjacency matrix
fn has_edge_matrix(matrix: &Vec>, u: usize, v: usize) -> bool {
matrix[u][v]
}
// Using adjacency list
fn has_edge_list(list: &Vec>, u: usize, v: usize) -> bool {
list[u].iter().any(|&node| node == v)
}
This code checks if there is a direct connection from node u to node v using two graph representations.
Look at what repeats when checking edges.
- Primary operation: For matrix, direct access; for list, searching neighbors.
- How many times: Matrix does one check; list may check many neighbors.
As the graph grows, the cost changes differently for each method.
| Input Size (n) | Matrix Checks | List Checks (avg neighbors) |
|---|---|---|
| 10 | 1 | 2 |
| 100 | 1 | 5 |
| 1000 | 1 | 10 |
Matrix always does one check, list checks grow with neighbors count.
Time Complexity: O(1) for adjacency matrix, O(k) for adjacency list
This means matrix is constant time, list depends on how many neighbors node u has.
[X] Wrong: "Adjacency list is always faster because it uses less space."
[OK] Correct: While adjacency list uses less space for sparse graphs, checking edges can be slower if nodes have many neighbors.
Understanding these differences helps you choose the right graph representation for your problem, a key skill in coding interviews.
"What if the graph is dense with many edges? How would the time complexity for adjacency list and matrix change?"
Practice
Solution
Step 1: Understand graph density
A graph with very few edges compared to vertices is called sparse.Step 2: Choose data structure 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 edge list with adjacency matrix
- Assuming adjacency matrix is always better
Solution
Step 1: Understand adjacency matrix structure
It is a 2D structure with size n x n, here 3 x 3.Step 2: Check Rust syntax for 2D vector initialization
vec![vec![0; 3]; 3] creates a 3x3 matrix filled with zeros.Final Answer:
let matrix = vec![vec![0; 3]; 3]; -> Option DQuick Check:
3x3 zero matrix = vec![vec![0; 3]; 3] [OK]
- Using single vector for 2D matrix
- Wrong inner vector size
- Using fixed-size array without mutability
let graph = vec![
vec![1, 2],
vec![0],
vec![0]
];
println!("{:?}", graph[0]);Solution
Step 1: Understand adjacency list structure
graph[0] contains neighbors of vertex 0, which are 1 and 2.Step 2: Check printed output
Printing graph[0] shows the vector [1, 2].Final Answer:
[1, 2] -> Option AQuick Check:
graph[0] = [1, 2] [OK]
- Confusing indices and values
- Expecting sorted output
- Mixing adjacency matrix with list
let mut matrix = vec![vec![0; 3]; 3]; matrix[0][1] = 1; matrix[1][3] = 1;
Solution
Step 1: Check matrix size and indices
Matrix is 3x3, valid indices are 0,1,2 for both rows and columns.Step 2: Identify invalid index usage
matrix[1][3] accesses column 3 which is out of bounds.Step 3: Fix the index
Change matrix[1][3] to matrix[1][2] to stay within bounds.Final Answer:
Index 3 is out of bounds; change to matrix[1][2] = 1; -> Option BQuick Check:
3x3 matrix max index = 2 [OK]
- Using index equal to size (out of bounds)
- Assuming 1-based indexing
- Ignoring Rust's strict bounds checking
Solution
Step 1: Analyze graph density and operations
Dense graph means many edges, close to maximum possible.Step 2: Choose data structure for fast edge checks
Adjacency matrix allows O(1) time to check if edge exists by direct index access.Step 3: Consider memory tradeoff
Though adjacency matrix uses more memory, for dense graphs this is acceptable and efficient.Final Answer:
Adjacency Matrix, because it allows O(1) edge existence checks -> Option CQuick Check:
Dense graph + fast edge check = Adjacency Matrix [OK]
- Choosing adjacency list for dense graphs wastes time
- Confusing edge list speed with adjacency matrix
- Incorrectly thinking adjacency matrix saves memory for sparse graphs
Ready to prove it?
Pass the test and this pattern moves unknown
