What Is an Adjacency Matrix and How Do You Read or Build One?
Updated
An adjacency matrix is a square grid that represents a graph: each row and column stands for a vertex, and the cell at row i, column j records the connection between vertex i and vertex j. A 0 means no edge, a 1 means an unweighted edge, and any other number is the edge's weight. You can read a matrix to understand a graph, or build one to feed a graph tool such as Graph Online, which accepts an adjacency matrix as one of its input methods alongside incidence matrices and edge lists.
How to read an adjacency matrix
Set up the matrix so the first row and first column both list the vertices in the same order. Then read each cell:
- 0 — no edge between that row's vertex and that column's vertex.
- 1 — an edge exists (unweighted graph).
- A weight value (e.g.
5) — an edge exists with that weight (weighted graph).
For a graph with vertices A, B, C:
A B C
A [ 0 1 0 ]
B [ 1 0 4 ]
C [ 0 4 0 ]
This says A–B is connected, B–C is connected with weight 4, and A–C has no edge.
Undirected vs. directed graphs
In an undirected graph, the matrix is symmetric: the cell at row i, column j equals the cell at row j, column i, because the edge goes both ways. The example above is symmetric.
In a directed graph, the matrix is generally not symmetric. A 1 at row A, column B means an edge from A to B only; the reverse cell can be 0.
Weighted graphs
For weighted graphs, replace 1 with the edge weight. A 0 still means "no edge," so a genuine edge of weight 0 would be ambiguous — treat 0 as "no connection" unless your tool specifies otherwise.
How to build a matrix from a graph
- List every vertex and assign each a row and column in the same order.
- Start with all cells at
0. - For each edge, find the row of one endpoint and the column of the other, and write
1(or the weight). - For an undirected graph, mirror every entry across the diagonal.
How to reconstruct a graph from a matrix
- Confirm the matrix is square (same number of rows and columns).
- Label rows and columns with vertex names in matching order.
- For each non-zero cell, draw an edge between the two vertices; use the cell value as the weight.
- If the matrix is symmetric, the graph is undirected; if not, treat each entry as a one-way edge.
Common format pitfalls
Graph Online validates the matrix before creating a graph and reports specific errors, so watch for these:
- Non-square matrix — the tool states "Matrix should be square." Rows and columns must match.
- Wrong separator — entries must be separated by commas (
,). Using spaces or semicolons triggers "Matrix is incorrect. Use comma "," as separator." - Wrong format overall — you'll see "Adjacency Matrix has wrong format," with options to click fix matrix or open help on the expected format.
- Multigraphs — the tool notes that a multigraph matrix contains the weight of the minimum edge between vertices, so parallel edges collapse to one value.
Using a matrix in Graph Online
Graph Online offers Create Graph Using adjacency matrix as a dedicated entry point. Paste your comma-separated matrix, and if the format is valid the graph is generated; if not, the error messages above tell you what to correct. Once the graph exists, you can run algorithms such as find shortest path, minimum spanning tree, connected-component search, and Eulerian cycle, and view outputs including the adjacency matrix, incidence matrix, and distance matrix.
If your data is easier to express as a list of edges rather than a grid, the same site also accepts an edge list and an incidence matrix, so you can pick whichever representation matches your source data.