Graph Representation (Adjacency Matrix and Adjacency List)
Introduction
Graph representation is a fundamental concept in Data Structures and Algorithms (DSA) and is crucial for implementing graph-based problems efficiently. If you are enrolled in a DSA course in Jaipur, understanding how graphs are stored in memory will help you optimize performance in coding interviews.
Graphs can be represented in two main ways:
- Adjacency Matrix
- Adjacency List
Why Graph Representation is Important
Choosing the right representation helps:
- Optimize space complexity
- Improve algorithm performance
- Simplify implementation
- Handle large datasets efficiently
1. Adjacency Matrix Representation
An adjacency matrix is a 2D array used to represent a graph.
If there is an edge between node i and j:
- Value = 1 (or weight)
Else: - Value = 0
Example (Undirected Graph):
Nodes: A, B, C
Matrix:
A B C
A [ 0, 1, 1 ]
B [ 1, 0, 1 ]
C [ 1, 1, 0 ]
Features of Adjacency Matrix
- Easy to implement
- Quick edge lookup
- Suitable for dense graphs
Time and Space Complexity
O(V^2)
- Space Complexity → O(V²)
- Edge Check → O(1)
Advantages
- Simple representation
- Fast edge checking
- Works well for dense graphs
Disadvantages
- High memory usage
- Inefficient for sparse graphs
2. Adjacency List Representation
An adjacency list stores a list of neighbors for each node.
Example:
A → B, C
B → A, C
C → A, B
Features of Adjacency List
- Memory efficient
- Suitable for sparse graphs
- Flexible structure
Time and Space Complexity
O(V+E)
- Space Complexity → O(V + E)
- Edge traversal → Efficient
Advantages
- Saves memory
- Efficient for large graphs
- Faster traversal
Disadvantages
- Slower edge lookup compared to matrix
- Slightly complex implementation
Adjacency Matrix vs Adjacency List
- Matrix uses more memory
- List uses less memory
- Matrix is faster for edge lookup
- List is better for traversal
When to Use Which
Use Adjacency Matrix when:
- Graph is dense
- Frequent edge checks are required
Use Adjacency List when:
- Graph is sparse
- Memory optimization is needed
Real-World Applications
- Social networks
- Navigation systems
- Network routing
- Recommendation systems
Common Interview Questions
- Convert matrix to list
- Implement graph using adjacency list
- Check if edge exists
- Count edges
Best Practices
- Choose representation based on problem
- Optimize space and time complexity
- Practice both implementations
Summary
- Graphs can be represented using matrix or list
- Adjacency matrix uses O(V²) space
- Adjacency list uses O(V + E) space
- Choice depends on problem requirements
FAQs
Q1. What is adjacency matrix?
A 2D array representing connections between nodes.
Q2. What is adjacency list?
A list of neighbors for each node.
Q3. Which representation is better?
Adjacency list is better for sparse graphs.
Q4. What is the space complexity of adjacency matrix?
O(V²).
Q5. Is this topic important for interviews?
Yes, it is a fundamental concept.
Internal Link
To explore more programming and development courses, click here for more free courses.



