What is an adjacency matrix?

An adjacency matrix is a two-dimensional matrix used to map the association between the nodes of a graph. A graph is a set of vertices (nodes) connected by edges.

In an adjacency matrix, 0 means that no association between nodes exists and 1 means that an association between nodes does exist.

svg viewer

Code

In the example below, we have implemented the adjacency matrix for the above illustration.

In a two-dimensional matrix, rows and columns are initialized with index 0 and so on. That’s why we have subtracted 1 from each index below.

Copyright ©2024 Educative, Inc. All rights reserved