Adjacency List for Directed Graphs
Space: O(V + E)Add Edge: O(1)Dynamic Size
📋Understanding Adjacency Lists
An adjacency list is a collection of lists used to represent a graph. Each vertex has a list containing all vertices it is connected to. This representation is particularly efficient for sparse graphs where the number of edges is much smaller than the maximum possible number of edges.
The key advantage is that it only stores existing edges, making it space-efficient for graphs with few connections relative to the total number of possible edges.
Space Usage: O(V + E) instead of O(V²) for adjacency matrix
💻Implementation
📊Performance Analysis
Time Complexity
- • Add Edge: O(1)
- • Remove Edge: O(degree)
- • Check Edge: O(degree)
- • Get Neighbors: O(1)
Space Complexity
- • Total Space: O(V + E)
- • Per Vertex: O(degree)
- • Sparse Graphs: Much better than O(V²)
- • Dynamic: Grows with actual edges
✅ Advantages
- • Space efficient: Only stores existing edges
- • Fast neighbor iteration: Direct access to adjacency list
- • Dynamic size: Adapts to actual graph density
- • Memory locality: Better cache performance for sparse graphs
- • Easy to add vertices: Just add new list
⚠️ Disadvantages
- • Slower edge queries: O(degree) vs O(1) for matrix
- • Complex deletion: Need to search through lists
- • No direct indexing: Can't directly access edge weights
- • Memory fragmentation: Dynamic allocation overhead
- • Dense graphs: May use more space than matrix
🎯Best Use Cases
Sparse Graphs
When edges << V², adjacency lists save significant space
Graph Traversal
DFS, BFS benefit from fast neighbor iteration
Dynamic Graphs
When vertices/edges are frequently added/removed