Adjacency List vs Matrix When to Choose Which in DSA C# - Complexity Comparison
Choosing how to represent a graph affects how fast we can check connections and add edges.
We want to know how the time to do these tasks grows as the graph gets bigger.
Analyze the time complexity of checking if an edge exists between two nodes.
// Using adjacency matrix
bool HasEdgeMatrix(int[,] matrix, int u, int v) {
return matrix[u, v] == 1;
}
// Using adjacency list
bool HasEdgeList(List[] list, int u, int v) {
return list[u].Contains(v);
}
This code checks if there is a direct connection between node u and node v using two graph representations.
Look at what repeats when checking edges.
- Primary operation: Checking if an edge exists between two nodes.
- How many times: For matrix, direct access once; for list, search through neighbors of u.
As the graph grows, the time to check edges changes differently for each method.
| Input Size (n nodes) | Matrix Check | List Check (avg neighbors k) |
|---|---|---|
| 10 | 1 operation | about k operations |
| 100 | 1 operation | about k operations |
| 1000 | 1 operation | about k operations |
Matrix check time stays the same no matter how big the graph is.
List check time depends on how many neighbors node u has, which can grow with graph size.
Time Complexity: O(1) for matrix, O(k) for list
This means matrix lets you check edges instantly, while list depends on neighbors count.
[X] Wrong: "Adjacency list is always faster than matrix because it uses less space."
[OK] Correct: List uses less space but checking edges can be slower if nodes have many neighbors.
Understanding these differences helps you pick the right graph representation for fast operations in real projects.
"What if the graph is very dense with many edges? How would the time complexity of adjacency list change compared to matrix?"
Practice
Solution
Step 1: Understand graph density
A graph with many nodes but few edges is called a sparse graph.Step 2: Choose data structure for sparse graph
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 sparse with dense graphs
- Thinking arrays or stacks represent graphs directly
Solution
Step 1: Identify adjacency matrix type
An adjacency matrix is a 2D array representing edges between nodes.Step 2: Check C# syntax for 2D arrays
In C#, 2D arrays use syntax like int[,] array = new int[rows, columns];Final Answer:
int[,] adjacencyMatrix = new int[4,4]; -> Option DQuick Check:
2D array syntax = int[,] [OK]
- Using jagged arrays instead of 2D arrays for matrix
- Declaring adjacencyMatrix as a single int
- Using List<int>[] which is adjacency list, not matrix
var graph = new List<int>[] {
new List<int>{1, 2},
new List<int>{2},
new List<int>{0}
};
Console.WriteLine(graph[0][1]);
What will be the output?Solution
Step 1: Understand the adjacency list structure
graph[0] = List<int>{1, 2}, so graph[0][1] accesses the second element which is 2.Step 2: Identify the output of Console.WriteLine(graph[0][1])
It prints the value 2 from the list at index 1 of graph[0].Final Answer:
2 -> Option CQuick Check:
graph[0][1] = 2 [OK]
- Confusing indices and printing 1 instead of 2
- Expecting an error due to nested lists
- Thinking graph[0][1] accesses graph[1]
int[,] matrix = new int[3,3]; matrix[0,1] = 1; matrix[1,2] = 1; matrix[2,0] = 1; Console.WriteLine(matrix[1,0]);What is the output and why?
Solution
Step 1: Analyze matrix assignments
Only matrix[0,1], matrix[1,2], and matrix[2,0] are set to 1; matrix[1,0] remains 0.Step 2: Understand default values in int[] arrays
Unassigned int elements default to 0 in C#.Final Answer:
0 because matrix[1,0] was never set -> Option BQuick Check:
Unset matrix elements = 0 [OK]
- Assuming adjacency matrix is symmetric by default
- Expecting runtime error for valid indices
- Confusing default values with assigned values
Choose between adjacency list and adjacency matrix.Solution
Step 1: Calculate graph density
10,000 users with 10 friends each means about 100,000 edges, which is sparse compared to 100 million possible edges.Step 2: Choose data structure for sparse graph
Adjacency list stores only existing edges, saving huge memory compared to adjacency matrix.Final Answer:
Adjacency list, because it uses less memory for sparse graphs -> Option AQuick Check:
Sparse large graph = adjacency list [OK]
- Choosing adjacency matrix despite huge memory waste
- Thinking adjacency matrix is always simpler
- Misunderstanding adjacency list stores only existing edges
Ready to prove it?
Pass the test and this pattern moves unknown
