Adjacency List vs Matrix When to Choose Which in DSA Go - Complexity Comparison
We want to understand how the choice between adjacency list and adjacency matrix affects the time it takes to work with graphs.
Which one 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
func hasEdgeMatrix(matrix [][]bool, u, v int) bool {
return matrix[u][v]
}
// Using adjacency list
func hasEdgeList(list [][]int, u, v int) bool {
for _, node := range list[u] {
if node == v {
return true
}
}
return false
}
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, loop through neighbors.
- How many times: Matrix does 1 check; list loops up to the number of 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 | 2 |
| 100 | 1 | 5 |
| 1000 | 1 | 10 |
Matrix always does one check, list depends on neighbors, which grows with edges.
Time Complexity: O(1) for adjacency matrix, O(k) for adjacency list
This means matrix checks edges instantly, list checks take time based on neighbors count.
[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.
Knowing when to use adjacency list or matrix shows you understand trade-offs in data structures, a key skill in problem solving.
"What if the graph is dense with many edges? How would the time complexity for adjacency list and matrix change when checking edges?"
Practice
Solution
Step 1: Understand graph density
A graph with very few edges compared to nodes is called a sparse graph.Step 2: Choose data structure for sparse graphs
Adjacency lists store 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 declaration
An adjacency matrix is a 2D fixed-size array representing edges between nodes.Step 2: Check Go syntax for fixed 2D array
"var graph [4][4]int" declares a 4x4 integer matrix correctly.Final Answer:
var graph [4][4]int -> Option AQuick Check:
Fixed 2D array = var graph [4][4]int [OK]
- Using slice of slices without initialization for matrix
- Declaring 1D slice instead of 2D array
- Using map which is adjacency list style
graph := map[int][]int{
0: {1, 2},
1: {2},
2: {0},
3: {},
}
fmt.Println(graph[1])Solution
Step 1: Identify adjacency list entries for node 1
Node 1 has edges to node 2 only, so graph[1] = {2}.Step 2: Understand fmt.Println output
Printing graph[1] outputs the slice [2].Final Answer:
[2] -> Option BQuick Check:
graph[1] = [2] [OK]
- Confusing keys and values in map
- Assuming node 1 connects to node 0
- Expecting adjacency matrix output
graph := make([][]int, 3)
for i := range graph {
graph[i] = make([]int, 3)
}
graph[0][1] = 1
graph[1][2] = 1
fmt.Println(graph[0][2])Solution
Step 1: Check matrix initialization
Each row is initialized with make([]int, 3), so matrix is properly created.Step 2: Check assigned values and print
graph[0][1] and graph[1][2] are set to 1; graph[0][2] was never set, so prints 0.Step 3: Verify no runtime errors
All indices are within range; no errors occur.Final Answer:
No bug, code runs correctly -> Option DQuick Check:
Proper initialization and indexing = No bug [OK]
- Assuming unassigned entries print 1
- Thinking initialization is missing
- Expecting index errors without out-of-range access
Solution
Step 1: Understand graph density and operations
A dense graph has many edges, close to the maximum possible.Step 2: Choose data structure for fast edge existence checks
Adjacency matrix allows O(1) time to check if edge exists between any two nodes.Step 3: Consider memory tradeoff
Though adjacency matrix uses more memory, for dense graphs this is acceptable and beneficial.Final Answer:
Adjacency Matrix for constant-time edge checks -> Option CQuick Check:
Dense graph + fast edge check = Adjacency Matrix [OK]
- Choosing adjacency list for dense graphs slows edge checks
- Ignoring memory vs speed tradeoff
- Confusing edge list with adjacency matrix
Ready to prove it?
Pass the test and this pattern moves unknown
