Graph representations (adjacency matrix vs list) in Data Structures Theory - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When working with graphs, how we store connections affects how fast we can check or find neighbors.
We want to know how the time to do these tasks grows as the graph gets bigger.
Analyze the time complexity of checking all neighbors of a node using two common graph storage methods.
// Adjacency Matrix
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 1) {
// process neighbor j
}
}
// Adjacency List
for (int neighbor : list[i]) {
// process neighbor
}
This code checks all neighbors of node i using either a matrix or a list.
Look at what repeats when finding neighbors of one node.
- Primary operation: Looping through possible neighbors.
- How many times: For matrix, loops n times; for list, loops degree(i) times (number of actual neighbors).
As the graph grows, the matrix always checks all nodes, but the list only checks actual neighbors.
| Input Size (n) | Adjacency Matrix Ops | Adjacency List Ops |
|---|---|---|
| 10 | 10 checks | degree(i) checks (e.g., 3) |
| 100 | 100 checks | degree(i) checks (e.g., 5) |
| 1000 | 1000 checks | degree(i) checks (e.g., 10) |
Pattern observation: Matrix cost grows with total nodes, list cost grows with actual neighbors.
Time Complexity: O(n) for adjacency matrix, O(degree(i)) for adjacency list
This means matrix always checks all nodes, while list only checks real neighbors, making it faster for sparse graphs.
[X] Wrong: "Adjacency matrix is always faster because it uses a simple 2D array."
[OK] Correct: Matrix checks every node even if no connection exists, so it can be slower when many nodes have few neighbors.
Understanding these differences helps you choose the right graph storage for your problem and explain your choice clearly.
"What if the graph is dense, meaning most nodes connect to many others? How would the time complexity for adjacency list compare to adjacency matrix?"
Practice
Solution
Step 1: Understand adjacency matrix structure
An adjacency matrix is a 2D grid where rows and columns represent nodes, and cells show if an edge exists.Step 2: Compare with other representations
Adjacency lists store neighbors in lists, not grids. Edge lists and incidence matrices differ in format.Final Answer:
Adjacency matrix -> Option BQuick Check:
2D grid = adjacency matrix [OK]
- Confusing adjacency list with matrix
- Thinking edge list is a grid
- Mixing incidence matrix with adjacency matrix
Solution
Step 1: Identify adjacency list format
An adjacency list maps each node to a list of its neighbors, often using a dictionary in Python.Step 2: Check each option
graph = {0: [1, 2], 1: [0], 2: [0, 1]} uses a dictionary with keys as nodes and values as neighbor lists, which is correct. graph = [[0,1,0],[1,0,1],[0,1,0]] is a matrix, C is an edge list, D incorrectly uses an integer 0 for node 1 instead of a list.Final Answer:
graph = {0: [1, 2], 1: [0], 2: [0, 1]} -> Option DQuick Check:
Dict with neighbors = adjacency list [OK]
- Choosing matrix format as list
- Confusing edge list with adjacency list
- Using integer instead of list for neighbors
graph = [[0, 1, 0], [1, 0, 1], [0, 1, 0]]Solution
Step 1: Locate row for node 1
Row 1 in the matrix is [1, 0, 1], representing edges from node 1 to nodes 0, 1, and 2.Step 2: Identify connected nodes
Values 1 indicate connection. Here, positions 0 and 2 have 1, so node 1 connects to nodes 0 and 2.Final Answer:
Nodes 0 and 2 -> Option AQuick Check:
Row 1 has 1s at 0 and 2 [OK]
- Confusing row and column indices
- Including node itself as connected
- Misreading zeros as edges
graph = {0: [1, 2], 1: [0, 3], 2: [0], 3: 1}Solution
Step 1: Check format of neighbors for each node
Nodes 0, 1, and 2 have neighbor lists. Node 3 has a single integer instead of a list.Step 2: Identify correct adjacency list format
Neighbors must always be in a list, even if only one neighbor exists, to keep consistent structure.Final Answer:
Node 3's neighbors should be in a list -> Option AQuick Check:
Neighbors must be lists [OK]
- Ignoring single neighbor format
- Thinking adjacency list must be matrix
- Assuming neighbors can be integers
Solution
Step 1: Calculate memory use for adjacency matrix
An adjacency matrix for 1000 nodes uses 1000x1000 = 1,000,000 cells, regardless of edges.Step 2: Calculate memory use for adjacency list
An adjacency list stores only the 10,000 edges, so memory use is proportional to edges, much less than matrix.Step 3: Compare efficiency
Since edges are sparse compared to possible connections, adjacency list is more memory efficient.Final Answer:
Adjacency list, because it stores only existing edges -> Option CQuick Check:
Sparse graph = adjacency list efficient [OK]
- Choosing matrix for sparse graphs
- Confusing speed with memory use
- Thinking adjacency list stores all edges
