liveBook · Manning

Adjacency matrix

As explained in Advanced Algorithms and Data Structures

An adjacency matrix is a matrix representation of a graph in which entries indicate whether pairs of vertices are connected, and may encode edge information.

An adjacency matrix represents a graph with a |V|×|V| table. Cell (i,j) records the edge from vertex i to vertex j: its weight for weighted graphs, or true/false or 1/0 for unweighted graphs. section 14.1.1

Reading a matrix

For vertices V1–V4, a 1 in row V1 and column V2 indicates an edge from V1 to V2. A 0 indicates no such edge. The displayed example has edges V1→V2, V1→V3, and V2→V4. section 14.1.1

The representation makes edge insertion and deletion constant-time operations. Listing outgoing or incoming edges requires scanning a row or column, taking O(|V|). Its fixed table uses O(|V|²) space, so it can waste memory when few possible connections exist. section 14.1.1

 

V1

V2

V3

V4

V1

0

1

1

0

V2

0

0

0

1

V3

0

0

0

0

V4

0

0

0

0

Browse the concrete 4×4 matrix to connect row-column positions with directed edges. section 14.1.1

Choose an adjacency matrix mainly for dense graphs or workloads requiring fast direct edge updates. For most other graph problems, an adjacency list generally uses less space. section 14.6 section 14.1.1

How Advanced Algorithms and Data Structures covers it

How deep it goes
Defines cells, demonstrates a matrix, and compares operation and space costs without implementing traversal algorithms.
Examples
Uses a four-vertex directed matrix with entries for V1→V2, V1→V3, and V2→V4.
What it assumes
Assumes vertices, edges, weighted graphs, and unweighted graphs as graph terminology.
What you can do after
Evaluate/understand when a matrix representation suits a graph, including its operation times and quadratic space cost.
How it connects
Contrasts adjacency lists and connects graph representation with dense graphs, breadth-first search, depth-first search, and shortest-path algorithms.

Also explained in

Compare how 8 books explain adjacency matrix →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.