# What Is Adjacent Matrix in Data Structure?

//

Angela Bailey

What Is Adjacent Matrix in Data Structure?

An adjacency matrix is a way to represent the connections between vertices in a graph. It is a two-dimensional array, where the rows and columns represent the vertices, and the values indicate whether there is an edge between the vertices or not.

• Efficient for dense graphs: If most of the vertices in the graph are connected to each other, then using an adjacency matrix can be more efficient than other representations like an adjacency list.
• Checking for edge existence: With an adjacency matrix, checking whether there is an edge between two vertices can be done in constant time.
• Simple implementation: The implementation of an adjacency matrix is straightforward and intuitive.

• Inefficient for sparse graphs: If the graph has very few edges compared to the number of vertices, using an adjacency matrix can waste space and computational resources.
• Space complexity: An adjacency matrix requires O(V^2) space, where V is the number of vertices in the graph. This can be problematic for large graphs with a high number of vertices.

Example:

To better understand how an adjacency matrix works, let’s consider a simple undirected graph with four vertices labeled A, B, C, and D. We will represent this graph using an adjacency matrix:

```   A  B  C  D
A [0][1][1][0]
B [1][0][1][1]
C [1][1][0][0]
D [0][1][0][0]
```

In the matrix above, a value of 1 represents an edge between two vertices, while a value of 0 indicates no edge. For example, in row B and column C, the value is 1, indicating that there is an edge between vertex B and vertex C.

Conclusion:

An adjacency matrix is a useful data structure for representing the connections between vertices in a graph. It offers advantages such as efficient edge existence checks and simple implementation.

However, it may not be suitable for sparse graphs due to its space complexity. Understanding the pros and cons of different graph representations can help in choosing the most appropriate one for specific applications.