Adjacency List Representation in DSA Go - Time & Space Complexity
We want to understand how fast we can build and use an adjacency list for a graph.
How does the time needed grow as the graph gets bigger?
Analyze the time complexity of the following code snippet.
type Graph struct {
adjList map[int][]int
}
func (g *Graph) AddEdge(u, v int) {
g.adjList[u] = append(g.adjList[u], v)
g.adjList[v] = append(g.adjList[v], u) // for undirected graph
}
This code adds an edge between two nodes in an undirected graph using an adjacency list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Appending neighbors to adjacency lists.
- How many times: Once per edge addition.
Each edge causes two append operations, so the work grows with the number of edges.
| Input Size (edges) | Approx. Operations |
|---|---|
| 10 | 20 appends |
| 100 | 200 appends |
| 1000 | 2000 appends |
Pattern observation: Operations grow linearly with the number of edges.
Time Complexity: O(E)
This means the time to build the adjacency list grows directly with the number of edges.
[X] Wrong: "Adding an edge takes time proportional to the number of nodes."
[OK] Correct: Adding an edge only appends to lists, which is quick and does not depend on total nodes.
Knowing adjacency list time complexity helps you explain graph storage choices clearly and confidently.
"What if the graph was directed and we only added one direction per edge? How would the time complexity change?"
Practice
Solution
Step 1: Understand adjacency list concept and compare options
An adjacency list stores each node with a list of its neighbors, showing direct connections. Only the description "A list where each node stores its connected neighbors" correctly describes this structure; others describe different concepts.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 order
Solution
Step 1: Identify adjacency list type in Go and check options for correct syntax
An adjacency list maps each node (int) to a slice of connected nodes ([]int), so map[int][]int is correct. var graph map[int][]int = make(map[int][]int) uses map[int][]int with make, which is correct. Others use wrong types or slices without mapping.Final Answer:
var graph map[int][]int = make(map[int][]int) -> Option AQuick Check:
Adjacency list = map node to slice [OK]
- Using slice of ints instead of map for adjacency list
- Declaring map[int]int which stores single int, not neighbors
- Confusing slice of slices with map
graph := make(map[int][]int)
graph[1] = []int{2, 3}
graph[2] = []int{1}
graph[3] = []int{1, 4}
graph[4] = []int{3}
for node, neighbors := range graph {
fmt.Printf("%d -> %v\n", node, neighbors)
}Solution
Step 1: Analyze graph assignments and match printed output
Node 1 connects to 2 and 3, node 2 connects to 1, node 3 connects to 1 and 4, node 4 connects to 3. Output prints each node with its neighbors slice exactly as assigned, matching 1 -> [2 3]\n2 -> [1]\n3 -> [1 4]\n4 -> [3].Final Answer:
1 -> [2 3] 2 -> [1] 3 -> [1 4] 4 -> [3] -> Option AQuick Check:
Print adjacency list as assigned [OK]
- Mixing neighbors between nodes
- Confusing slice contents order
- Assuming empty neighbors incorrectly
func addEdge(graph map[int][]int, u int, v int) {
graph[u] = append(graph[u], v)
graph[v] = append(graph[u], v)
}Solution
Step 1: Review append calls for edges and identify correct usage
First append adds v to graph[u], correct. Second append should add u to graph[v], but uses graph[u] mistakenly. Second append must be graph[v] = append(graph[v], u) to add reverse edge for undirected graph.Final Answer:
Second append uses graph[u] instead of graph[v] -> Option DQuick Check:
Append neighbors to correct node slice [OK]
- Appending to wrong slice causing incorrect graph
- Thinking map cannot be modified inside function
- Misunderstanding append usage in Go
Solution
Step 1: Understand adjacency list lookup and optimize lookup with map
graph[u] is a slice, so direct indexing by v is invalid (Check if graph[u][v] exists directly using indexing wrong). Iteration (Iterate over graph[u] slice to find v; return true if found, else false) is linear time. Using map[int]bool inside graph[u] allows constant time check if v is neighbor (Use a map[int]bool inside graph[u] for constant time lookup). Sorting and binary search (Sort graph[u] slice and use binary search to find v) adds overhead.Final Answer:
Use a map[int]bool inside graph[u] for constant time lookup -> Option CQuick Check:
Map lookup faster than slice iteration [OK]
- Trying to index slice by node value directly
- Ignoring performance of linear search
- Overcomplicating with sorting when map is simpler
Ready to prove it?
Pass the test and this pattern moves unknown
