Adjacency List Representation in DSA C# - Time & Space Complexity
We want to understand how the time to build and use an adjacency list changes as the graph grows.
How does the number of nodes and edges affect the work done?
Analyze the time complexity of the following code snippet.
// Create adjacency list for a graph
var adjacencyList = new Dictionary<int, List<int>>();
void AddEdge(int u, int v) {
if (!adjacencyList.ContainsKey(u)) {
adjacencyList[u] = new List<int>();
}
adjacencyList[u].Add(v);
}
This code adds a directed edge from node u to node v in the adjacency list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding edges to the adjacency list.
- How many times: Once per edge added.
Each edge causes one addition to a list. The number of nodes affects dictionary checks.
| Input Size (n = nodes, m = edges) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 15 insertions and up to 10 dictionary checks |
| 100 nodes, 200 edges | About 200 insertions and up to 100 dictionary checks |
| 1000 nodes, 5000 edges | About 5000 insertions and up to 1000 dictionary checks |
Pattern observation: Operations grow roughly with the number of edges and nodes.
Time Complexity: O(n + m)
This means the time grows linearly with the number of nodes plus the number of edges.
[X] Wrong: "Adding edges takes O(m²) time because each insertion searches the whole list."
[OK] Correct: Each edge is added directly to a list without searching, so insertion is fast and does not depend on the number of edges already added.
Understanding adjacency list time complexity helps you explain graph storage and traversal efficiency clearly in interviews.
"What if we used an adjacency matrix instead of a list? How would the time complexity for adding edges change?"
Practice
Solution
Step 1: Understand adjacency list concept
An adjacency list stores each node with a list of its neighbors, showing direct connections.Step 2: Compare with other options
Other options describe different structures: matrix, isolated nodes, or traversal tools, not adjacency lists.Final Answer:
A list where each node stores its connected neighbors -> Option BQuick Check:
Adjacency list = neighbors list [OK]
- Confusing adjacency list with adjacency matrix
- Thinking adjacency list stores distances
- Mixing adjacency list with traversal queues
Solution
Step 1: Identify adjacency list structure
An adjacency list maps each node (int) to a list of connected nodes (List<int>), so Dictionary fits.Step 2: Check other options
List<int> adjacencyList = new List<int>(); is a simple list, not mapping nodes to neighbors. int[][] adjacencyList = new int[5][]; is an array of arrays, possible but less flexible. Dictionary adjacencyList = new Dictionary(); maps int to int, not to a list.Final Answer:
Dictionary<int, List<int>> adjacencyList = new Dictionary<int, List<int>>(); -> Option CQuick Check:
Use Dictionary with List for adjacency list [OK]
- Using List<int> alone without mapping nodes
- Using Dictionary instead of list of neighbors
- Confusing jagged arrays with adjacency lists
var graph = new Dictionary();
graph[1] = new List<int> {2, 3};
graph[2] = new List<int> {1};
graph[3] = new List<int> {1};
foreach(var node in graph)
{
Console.Write(node.Key + ": ");
foreach(var neighbor in node.Value)
{
Console.Write(neighbor + " ");
}
Console.WriteLine();
}Solution
Step 1: Analyze graph initialization
Node 1 connects to 2 and 3; node 2 connects to 1; node 3 connects to 1.Step 2: Trace output loop
For each node, print key and neighbors separated by spaces. So output lines are: "1: 2 3 " "2: 1 " "3: 1 "Final Answer:
1: 2 3 2: 1 3: 1 -> Option AQuick Check:
Print node keys with their neighbors [OK]
- Printing node itself as neighbor
- Missing spaces between neighbors
- Printing empty neighbor lists
var graph = new Dictionary(); graph[1].Add(2); graph[1].Add(3);
Solution
Step 1: Understand dictionary initialization
graph[1] is not initialized with a list, so calling Add on null causes error.Step 2: Fix by initializing list
Assign graph[1] = new List<int>() before adding neighbors fixes the null reference error.Final Answer:
Initialize graph[1] with a new List<int> before adding neighbors -> Option AQuick Check:
Initialize list before adding elements [OK]
- Trying to add to null list
- Changing dictionary type incorrectly
- Using Add method wrongly on dictionary
Dictionary. How would you add an undirected edge between nodes 4 and 5?Solution
Step 1: Understand undirected edge meaning
Undirected edge means both nodes connect to each other, so both lists must include the other node.Step 2: Add neighbors to both nodes
Add 5 to node 4's neighbor list and 4 to node 5's neighbor list to represent the edge correctly.Final Answer:
Add 5 to node 4's list and 4 to node 5's list -> Option DQuick Check:
Undirected edge = mutual neighbor entries [OK]
- Adding edge only one way
- Adding wrong node to list
- Using dictionary entry incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
