19. Adjacency List and Matrix
Description
Graph representation is the foundation for most graph processing algorithms, such as Breadth-First Search (BFS), Depth-First Search (DFS), Dijkstra's algorithm, and others. Depending on the task and data characteristics, a graph can be represented using various data structures, including an adjacency list and an adjacency matrix. These structures allow for efficient storage and manipulation of graphs to execute various operations.
1. Adjacency List
An adjacency list is a data structure that represents a graph as an array of lists, where each vertex stores a list of its neighbors. An adjacency list is one of the most efficient ways to represent a graph, especially for sparse graphs where the number of edges is significantly smaller than the maximum possible.
Advantages of Adjacency List
- Memory Efficiency: For graphs with a small number of edges, an adjacency list consumes less memory than an adjacency matrix.
- Dynamic Flexibility: Adding and removing edges in an adjacency list is simpler than in an adjacency matrix because elements can simply be appended or removed from the list of neighbors.
Adjacency List Structure
An adjacency list can be implemented using an array or list (or a dictionary/hash map in languages like Python), where the index (or key) represents a vertex, and the corresponding value is a list of its adjacent vertices.
Example
Suppose we have the following graph with 5 vertices (0, 1, 2, 3, 4):
0 -> 1
| ^
v |
2 -> 3 -> 4
For this graph, the adjacency list is:
0: [1, 2]
1: [0, 3]
2: [0, 3]
3: [1, 2, 4]
4: [3]
Here:
- Vertex 0 is connected to vertices 1 and 2.
- Vertex 1 is connected to vertices 0 and 3, and so on.
How It Works
To find all vertices adjacent to a given vertex, we simply look up the vertex index in the adjacency list to retrieve its set of neighbors.
Time Complexity
- Finding all neighbors of vertex v: O(k), where k is the number of edges outgoing from vertex v.
- Checking for an edge between vertices u and v: O(k), where k is the number of edges in the adjacency list of vertex u.
- Adding an edge: O(1) when using a dynamic list.
2. Adjacency Matrix
An adjacency matrix is a two-dimensional grid (matrix) where rows and columns correspond to vertices in the graph, and cell values indicate whether an edge exists between the respective vertices. This representation is useful when a graph is dense (has many edges) or when edge existence checks must be fast.
Advantages of Adjacency Matrix
- Fast Edge Lookup: Checking whether an edge exists between two vertices takes O(1) time, making the adjacency matrix an ideal choice for dense graphs.
- Implementation Simplicity: A matrix is straightforward to implement and maintains a fixed size, which can be advantageous in certain applications.
Adjacency Matrix Structure
For a directed graph, an adjacency matrix is a square matrix where element A[i][j] equals 1 if an edge exists from vertex i to vertex j, and 0 otherwise.
Example
Using the same graph structure from the previous example:
0 -> 1
| ^
v |
2 -> 3 -> 4
The adjacency matrix is represented as follows:
0 1 2 3 4
0 [0, 1, 1, 0, 0]
1 [1, 0, 0, 1, 0]
2 [0, 0, 0, 1, 0]
3 [0, 0, 0, 0, 1]
4 [0, 0, 0, 0, 0]
Here:
- Row 0 indicates that vertex 0 has directed edges to vertices 1 and 2.
- Row 1 indicates that vertex 1 has directed edges to vertices 0 and 3, and so on.
How It Works
To verify whether an edge exists between vertex u and vertex v, we check element A[u][v]. If it equals 1, the edge exists; otherwise, it does not.
Time Complexity
- Finding all neighbors of vertex v: O(n), where n is the total number of vertices in the graph (since the entire row or column must be scanned).
- Checking for an edge between vertices u and v: O(1), as it involves direct matrix array lookup.
- Adding an edge: O(1).
Comparison of Adjacency List and Adjacency Matrix
| Feature | Adjacency List | Adjacency Matrix |
|---|---|---|
| Memory | O(n + e), where n = vertices, e = edges | O(n^2) |
| Edge Lookup | O(k), where k = degree of vertex u | O(1) |
| Find All Neighbors | O(k) | O(n) |
| Add Edge | O(1) | O(1) |
| Remove Edge | O(k) | O(1) |
When to Use an Adjacency List
- For sparse graphs (where the number of edges is much less than
n^2). - When memory conservation is important while primarily operating on a vertex's immediate neighbors.
- For dynamic graphs where edges are frequently added or removed.
When to Use an Adjacency Matrix
- For dense graphs where the number of edges is close to
n^2(e.g., complete graphs). - When frequent, fast edge-existence queries between arbitrary pairs of vertices are required.
- For applications requiring matrix operations or rapid lookup times.
Conclusion
Choosing between an adjacency list and an adjacency matrix depends on the specific problem, graph density, and required operations. Adjacency lists are preferred for sparse graphs and dynamic graph modifications, whereas adjacency matrices excel in dense graphs and scenarios requiring constant-time edge lookups.
All rights remain with the original author (Archiveskk). Publishing here does not grant a license to copy or reuse this text. Copyright & license. If you noticed a mistake or copyright issues, please contact: lebroskiofficial@proton.me