Adjacency List vs Matrix When to Choose Which in DSA C - Complexity Comparison
We want to understand how the time to check connections in a graph changes depending on how we store it.
Which storage method is faster for different tasks and graph sizes?
Analyze the time complexity of checking if an edge exists between two nodes.
// Using adjacency matrix
int hasEdgeMatrix(int n, int matrix[n][n], int u, int v) {
return matrix[u][v];
}
// Using adjacency list
int hasEdgeList(int u, int v, int* adj[], int sizes[]) {
for (int i = 0; i < sizes[u]; i++) {
if (adj[u][i] == v) return 1;
}
return 0;
}
This code checks if there is a direct connection (edge) between two nodes using two different graph storage methods.
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 node u.
Imagine the graph grows bigger with more nodes and edges.
| Input Size (n) | Matrix Checks | List Checks (avg neighbors) |
|---|---|---|
| 10 nodes | 1 check | about 2-3 checks |
| 100 nodes | 1 check | about 5-10 checks |
| 1000 nodes | 1 check | about 10-20 checks |
Matrix access stays the same no matter the size. List checks grow with how many neighbors a node has.
Time Complexity: O(1) for adjacency matrix, O(k) for adjacency list
This means matrix lets you check edges instantly, while list depends on how many neighbors the node has.
[X] Wrong: "Adjacency list is always faster because it uses less space."
[OK] Correct: Checking if an edge exists can be slower with lists if a node has many neighbors, because you must look through them one by one.
Knowing when to use adjacency lists or matrices shows you understand trade-offs in data structures, a key skill in problem solving.
What if the graph is very dense (almost every node connected to every other)? How would the time complexity of adjacency list checks compare to adjacency matrix?
Practice
Solution
Step 1: Understand graph density
A graph with very few edges compared to vertices is called a sparse graph.Step 2: Choose efficient representation for sparse graphs
Adjacency lists store only existing edges, saving space for sparse graphs, unlike adjacency matrices which use space for all possible edges.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
- Thinking adjacency matrix is always better
Solution
Step 1: Recall C array declaration syntax
In C, a 2D array for 4 vertices is declared as int graph[4][4];Step 2: Check options for correctness
int graph[4][4]; matches correct syntax; int graph[4]; is 1D array; int graph[][] = new int[4][4]; uses Java syntax; int graph[4][4][4]; is 3D array.Final Answer:
int graph[4][4]; -> Option DQuick Check:
C 2D array syntax = int graph[4][4]; [OK]
- Using Java or other language syntax in C
- Declaring wrong array dimensions
- Confusing 1D and 2D arrays
int graph[3][3] = {
{0, 1, 0},
{1, 0, 1},
{0, 1, 0}
};What is the output of the code snippet that prints all neighbors of vertex 1?
for (int i = 0; i < 3; i++) {
if (graph[1][i] == 1) {
printf("%d ", i);
}
}Solution
Step 1: Analyze adjacency matrix row for vertex 1
Row 1 is {1, 0, 1}, meaning edges to vertices 0 and 2.Step 2: Trace loop printing neighbors
Loop checks graph[1][i] == 1; prints i when true. i=0 and i=2 satisfy condition.Final Answer:
0 2 -> Option AQuick Check:
Neighbors of vertex 1 = 0 and 2 [OK]
- Confusing row and column indices
- Printing vertex itself as neighbor
- Misreading matrix values
int* graph[3];
for (int i = 0; i < 3; i++) {
graph[i] = malloc(sizeof(int));
graph[i][0] = i;
}What is the main issue?
Solution
Step 1: Check memory allocation size
malloc(sizeof(int)) allocates space for only one int, but adjacency list needs space for multiple neighbors.Step 2: Understand adjacency list needs dynamic size
Allocating only one int is insufficient; should allocate enough space or use dynamic structures like linked lists.Final Answer:
Memory allocated is too small for multiple neighbors -> Option CQuick Check:
Insufficient malloc size = Error [OK]
- Confusing malloc and calloc
- Wrong array declaration type
- Assuming fixed size adjacency list
Solution
Step 1: Analyze graph size and density
1000 vertices and 10,000 edges means the graph is moderately dense (max edges ~499,500).Step 2: Consider edge existence check speed
Adjacency matrix allows checking edge existence in O(1) time by direct index access.Step 3: Compare memory vs speed tradeoff
Though adjacency matrix uses more memory, fast edge check is priority here.Final Answer:
Adjacency Matrix, because it allows O(1) edge existence check -> Option BQuick Check:
Fast edge check = Adjacency Matrix [OK]
- Choosing adjacency list only for memory without speed need
- Ignoring graph density impact
- Confusing edge list with adjacency matrix
Ready to prove it?
Pass the test and this pattern moves unknown
