Introduction
Graphs can be represented in different ways depending on the application. Each representation has trade-offs in terms of space complexity, time complexity, and ease of implementation.
Methods of Representation
- Adjacency Matrix
- Adjacency List
- Edge List
- Incidence Matrix
1. Adjacency Matrix
Definition
An adjacency matrix is a 2D array A of size n×n, where n is the number of vertices.
For undirected graph:
- A[i][j] = 1 if there is an edge between vertices i and j
- A[i][j] = 0 otherwise
For weighted graph:
- A[i][j] = weight of edge (i,j)
- A[i][j] = ∞ or 0 if no edge
Example 1: Undirected Graph
Graph:
1 --- 2
| |
3 --- 4
Adjacency Matrix:
1 2 3 4
1 [0 1 1 0]
2 [1 0 0 1]
3 [1 0 0 1]
4 [0 1 1 0]
Properties:
- Symmetric matrix (A[i][j] = A[j][i])
- Diagonal is 0 (no self-loops)
- Row sum = degree of vertex
Example 2: Directed Graph
Graph: 1 → 2 → 3 → 1
Adjacency Matrix:
1 2 3
1 [0 1 0]
2 [0 0 1]
3 [1 0 0]
Example 3: Weighted Graph
Graph: 1 —5— 2 —7— 4, 1 —3— 3 —2— 4
Adjacency Matrix:
1 2 3 4
1 [0 5 3 0]
2 [5 0 0 7]
3 [3 0 0 2]
4 [0 7 2 0]
Operations with Adjacency Matrix
Check if edge (i,j) exists: O(1) Find all neighbors of vertex i: O(n) Add edge (i,j): O(1) Remove edge (i,j): O(1)
Space Complexity
Space: O(n²) - always, regardless of number of edges
Advantages
- Fast edge lookup: O(1)
- Easy to implement
- Edge addition/removal: O(1)
- Good for dense graphs
Disadvantages
- Space inefficient: O(n²) even for sparse graphs
- Finding neighbors: O(n) time
- Not scalable for very large graphs
When to Use
- Dense graphs (many edges)
- Frequent edge queries
- Small graphs (hundreds of vertices)
2. Adjacency List
Definition
An adjacency list represents a graph as an array of lists. For each vertex, store a list of its adjacent vertices.
Example 1: Undirected Graph
Graph:
1 --- 2
| |
3 --- 4
Adjacency List:
1: [2, 3]
2: [1, 4]
3: [1, 4]
4: [2, 3]
Example 2: Directed Graph
Graph: 1 → 2 → 3 → 1
Adjacency List:
1: [2]
2: [3]
3: [1]
Example 3: Weighted Graph
Adjacency List (with weights):
1: [(2,5), (3,3)]
2: [(1,5), (4,7)]
3: [(1,3), (4,2)]
4: [(2,7), (3,2)]
Operations with Adjacency List
Check if edge (i,j) exists: O(deg(i)) Find all neighbors of vertex i: O(1) to access, O(deg(i)) to iterate Add edge (i,j): O(1) Remove edge (i,j): O(deg(i)) Find degree of vertex i: O(1)
Space Complexity
For undirected graph: O(V + E) For directed graph: O(V + E)
Where V = vertices, E = edges
Advantages
- Space efficient: O(V + E) - great for sparse graphs
- Fast neighbor iteration: O(deg(v))
- Easy to add edges
- Scalable to large graphs
Disadvantages
- Edge lookup slower: O(deg(v)) vs O(1) for matrix
- Removing edges slower
- More complex implementation
When to Use
- Sparse graphs (few edges)
- Large graphs (thousands/millions of vertices)
- Frequent neighbor iterations (BFS, DFS)
- Real-world networks (social, web, roads)
3. Edge List
Definition
Simply store all edges as a list of pairs.
Example
Graph: 1 --- 2, 1 --- 3, 2 --- 4, 3 --- 4
Edge List:
[(1,2), (1,3), (2,4), (3,4)]
For weighted:
[(1,2,5), (1,3,3), (2,4,7), (3,4,2)]
Space Complexity
Space: O(E) - most compact!
Advantages
- Most compact: O(E) space
- Simple to implement
- Efficient for edge iterations
- Good for algorithms that process all edges
Disadvantages
- Very slow for vertex operations
- Edge lookup: O(E)
- Finding neighbors: O(E)
When to Use
- Algorithms that process all edges (Kruskal’s MST)
- Very sparse graphs
- Simple storage
Comparison Summary
| Representation | Space | Edge Check | Find Neighbors | Add Edge | Best For |
|---|---|---|---|---|---|
| Adjacency Matrix | O(V²) | O(1) | O(V) | O(1) | Dense graphs |
| Adjacency List | O(V+E) | O(deg) | O(deg) | O(1) | Sparse graphs |
| Edge List | O(E) | O(E) | O(E) | O(1) | Edge processing |
Dense vs. Sparse Graphs
Dense graph: E ≈ V²
- Use: Adjacency Matrix
Sparse graph: E ≈ V
- Use: Adjacency List
Key Points for Exams
- Adjacency matrix: O(V²) space, O(1) edge check
- Adjacency list: O(V+E) space, O(deg) edge check
- Matrix is symmetric for undirected graphs
- List stores each edge twice in undirected graphs
- Dense graph: matrix better
- Sparse graph: list better
- Edge list: simplest, O(E) space
- Matrix good for: small, dense graphs
- List good for: large, sparse graphs
- Row sum in matrix: vertex degree
Practice Problems
-
Draw adjacency matrix for K₄
-
Draw adjacency list for graph with edges: {1,2}, {2,3}, {3,4}, {4,1}, {1,3}
-
Graph has 100 vertices and 200 edges. Compare space usage of matrix vs list.
-
Convert adjacency matrix to list: [[0,1,1,0], [1,0,1,1], [1,1,0,1], [0,1,1,0]]
-
What is time complexity to find all edges in adjacency list?
-
For directed graph with edge (1→2), what does A[1][2] contain?
-
Why is adjacency list better for BFS/DFS?
-
Graph has edge list [(1,2), (2,3), (3,1)]. How to check if (1,3) exists?
-
In adjacency matrix of undirected graph, what is relationship between A[i][j] and A[j][i]?
-
Calculate space ratio: (adjacency list) / (adjacency matrix) for V=1000, E=5000