LibraryGraph Representation: Adjacency Matrix, Adjacency List

Graph Representation: Adjacency Matrix, Adjacency List

Learn about Graph Representation: Adjacency Matrix, Adjacency List as part of GATE Computer Science - Algorithms and Data Structures

Graph Representation: Adjacency Matrix vs. Adjacency List

Graphs are fundamental data structures used to model relationships between objects. Representing these relationships efficiently is crucial for algorithm performance. Two primary methods for representing graphs are the Adjacency Matrix and the Adjacency List.

Adjacency Matrix

An Adjacency Matrix is a square matrix used to represent a finite graph. The elements of the matrix indicate whether pairs of vertices are adjacent or not in the graph. For a graph with V vertices, the matrix will be V x V.

A V x V matrix where matrix[i][j] = 1 if there's an edge from vertex i to vertex j, and 0 otherwise.

For an unweighted graph, the adjacency matrix is a 2D array where matrix[i][j] is 1 if an edge exists between vertex i and vertex j, and 0 if no edge exists. For weighted graphs, matrix[i][j] stores the weight of the edge, or infinity/0 if no edge exists.

In an adjacency matrix representation, we use a 2D array, say adjMatrix[V][V], where V is the number of vertices. If there is an edge from vertex i to vertex j, then adjMatrix[i][j] is set to 1 (or the weight of the edge if it's a weighted graph). Otherwise, it's set to 0 (or infinity/a sentinel value for weighted graphs). For undirected graphs, the matrix is symmetric (adjMatrix[i][j] == adjMatrix[j][i]). For directed graphs, it's not necessarily symmetric.

Adjacency Matrix: Pros and Cons

FeatureAdjacency Matrix
Space ComplexityO(V^2)
Checking for Edge (u, v)O(1)
Adding an EdgeO(1)
Removing an EdgeO(1)
Finding all neighbors of a vertexO(V)
Suitable forDense graphs (E is close to V^2)

Adjacency List

An Adjacency List represents a graph using an array of linked lists. Each index in the array corresponds to a vertex, and the linked list at that index stores all the vertices adjacent to it.

An array of lists, where each list contains the neighbors of a vertex.

The adjacency list uses an array where each element adjList[i] is a list (e.g., linked list, vector) containing all vertices j such that there is an edge from vertex i to vertex j. This is often implemented as an array of vectors or an array of linked lists.

The adjacency list representation typically involves an array adjList of size V, where V is the number of vertices. Each adjList[i] is a data structure (like a linked list or a dynamic array/vector) that stores the indices of all vertices directly connected to vertex i. For weighted graphs, each entry in the list would be a pair: (neighbor_vertex, edge_weight).

Visualizing the difference between Adjacency Matrix and Adjacency List for a sample graph. The Adjacency Matrix shows a grid where '1' indicates an edge. The Adjacency List shows each vertex with a list of its direct neighbors. This helps understand how space is utilized differently based on graph density.

📚

Text-based content

Library pages focus on text content

Adjacency List: Pros and Cons

FeatureAdjacency List
Space ComplexityO(V + E)
Checking for Edge (u, v)O(degree(u)) or O(V) in worst case (if using simple linked list)
Adding an EdgeO(1) (amortized for dynamic arrays)
Removing an EdgeO(degree(u)) or O(V) in worst case
Finding all neighbors of a vertexO(degree(u))
Suitable forSparse graphs (E is much smaller than V^2)

Choosing the Right Representation

The choice between Adjacency Matrix and Adjacency List depends heavily on the characteristics of the graph and the operations you intend to perform. For dense graphs where most pairs of vertices are connected, an Adjacency Matrix is often more efficient for checking edge existence. For sparse graphs, where connections are few, an Adjacency List is preferred due to its lower space complexity and faster iteration over neighbors.

For competitive exams like GATE, understanding the time and space complexity trade-offs for both representations is crucial for optimizing algorithm performance.

What is the space complexity of an Adjacency Matrix for a graph with V vertices?

O(V^2)

What is the space complexity of an Adjacency List for a graph with V vertices and E edges?

O(V + E)

Which representation is generally better for sparse graphs?

Adjacency List

Which representation offers O(1) time complexity for checking if an edge exists between two vertices?

Adjacency Matrix

Learning Resources

GeeksforGeeks: Graph Representation(documentation)

A comprehensive explanation of graph representations, including Adjacency Matrix and Adjacency List, with C++ and Java examples.

TutorialsPoint: Graph Representation(documentation)

Explains the concepts of Adjacency Matrix and Adjacency List with clear diagrams and descriptions.

Coursera: Algorithms Specialization - Graph Representation(video)

A video lecture from a reputable specialization covering graph representations and their implications.

Programiz: Adjacency Matrix and Adjacency List(documentation)

Provides a clear breakdown of Adjacency Matrix and Adjacency List with Python code examples.

Wikipedia: Adjacency Matrix(wikipedia)

Detailed information on the mathematical definition and properties of adjacency matrices.

Wikipedia: Adjacency List(wikipedia)

Comprehensive details about adjacency lists, their variations, and applications.

Scaler Topics: Adjacency Matrix vs Adjacency List(blog)

A comparative analysis of Adjacency Matrix and Adjacency List, highlighting their differences and use cases.

HackerEarth: Graph Representation(documentation)

An introductory tutorial on graph representations, covering both matrix and list methods.

YouTube: Adjacency Matrix vs Adjacency List by Abdul Bari(video)

A highly-rated video explanation by a popular data structures educator, clarifying the concepts.

Stanford CS 161: Data Structures and Algorithms - Graphs(paper)

Lecture notes from a top university course covering graph representations and algorithms.