The incidence matrix

We can also represent a graph using an incidence matrix. In an incidence matrix, each row of the matrix represents a vertex, and each column represents an edge. We will represent the connectivity between two objects using a two-dimensional array, as array[v][e] === 1 if the vertex v is an incident upon edge e or as array[v][e] === 0 otherwise, as demonstrated in the following diagram:

An incidence matrix is usually used to save space and memory when we have more edges than vertices.

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.