In a graph, what is the time complexity of searching for an edge in an adjacency matrix representation?

In a graph, what is the time complexity of searching for an edge in an adjacency matrix representation?

a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)

Answer:

a) O(1)

Explanation:

In an adjacency matrix representation of a graph, searching for an edge between two vertices can be done in constant time O(1). The adjacency matrix is a 2D array where the presence of an edge between vertices i and j is indicated by the value at index [i][j].

This allows for efficient edge lookups but consumes more memory, especially for sparse graphs, as it stores a matrix of size n^2, where n is the number of vertices.

Adjacency lists, on the other hand, are more memory-efficient for sparse graphs but have slower search times for specific edges.

Reference:

DSA (Data Structures and Algorithms) MCQ Questions and Answers

Scroll to Top