O'Reilly logo

PHP 7 Data Structures and Algorithms by Mizanur Rahman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adjacency matrix

In an adjacency matrix, we represent the graph in a two-dimensional array, where each node represents the array index horizontally and vertically. If the edge from A to B is directional, then we mark that array index [A][B] to 1 to mark the connection; otherwise, it's 0. If the edge is not directional, then both [A][B] and [B][A] are set to 1. If the graph is a weighted graph, then [A][B] or [B][A] will store the weight instead of 1. The following diagram shows the undirected graph representation using a matrix:

This one shows the directed graph representation of the matrix:

Though our graph representation shows an alphabetic ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required