Adjacency Matrix Calculator

Edge list → adjacency matrix · degree sequence · density · connected components · matrix powers A² A³

Input:
Type:

Separators: A-B A B A,B A->B. Arrow (->) forces directed edge.

Quick Examples

What Is an Adjacency Matrix?

An adjacency matrix is a square n×n matrix A used to represent a graph G = (V, E) with n vertices. Entry A[i][j] = 1 if there is an edge from vertex i to vertex j, and 0 otherwise. For weighted graphs the entry stores the edge weight instead of 1. The matrix is one of the two canonical graph representations in computer science — the other being the adjacency list.

Adjacency matrices are fundamental to graph theory, network analysis, social network modelling, routing algorithms, and linear algebra applications in computer science. They allow O(1) edge existence queries and enable powerful matrix-algebraic techniques such as computing walk counts, detecting triangles, and finding connected components.

How to Use This Calculator

Three input modes are available:

  • Edge List (default) — type vertex pairs, one per line. Flexible separators: A-B, A B, A,B, A->B. Use -> to force a directed edge.
  • Matrix Grid — fill a 0/1 grid directly. Choose size 2–8 and customise vertex labels in the blue header cells.
  • Adj. List — enter in vertex: n1, n2, ... format, one vertex per line.

Select Undirected or Directed, then click Calculate. The tool outputs the adjacency matrix, adjacency list, edge list, degree table, graph density, connectivity, matrix powers A² and A³, and an interactive graph visualisation. The Advanced tab adds a point-to-point path checker.

Key Formulas

Vertex Degree

Undirected: deg(v) = Σⱼ A[v][j] (row sum = column sum) Directed: out-deg(v) = Σⱼ A[v][j] (row sum) in-deg(v) = Σᵢ A[i][v] (column sum)

Graph Density

Undirected: D = 2|E| / (n(n−1)) Directed: D = |E| / (n(n−1)) D = 0 → empty graph (no edges) D = 1 → complete graph (every pair connected)

Matrix Powers and Walk Counts

A^k [i][j] = number of walks of length exactly k from i to j A¹[i][j] → direct edges A²[i][j] → walks through one intermediate vertex A³[i][j] → walks through two intermediate vertices Triangles (undirected) = trace(A³) / 6 Diagonal of A² (undirected) = degree of each vertex

Symmetry Property

A is symmetric ⟺ A[i][j] = A[j][i] for all i, j Symmetric matrix ⟺ undirected graph (A = Aᵀ)

Worked Examples

Example 1 — Triangle Graph K₃ (Undirected)

Vertices: A, B, C. Edges: A–B, B–C, A–C.

  1. Index vertices: A=0, B=1, C=2. Initialise 3×3 zero matrix.
  2. Edge A–B: A[0][1] = A[1][0] = 1
  3. Edge B–C: A[1][2] = A[2][1] = 1
  4. Edge A–C: A[0][2] = A[2][0] = 1
  5. All degrees = 2. Density = 2×3 / (3×2) = 1.0 → this is the complete graph K₃.
ABC
A011
B101
C110

Example 2 — Directed Cycle (3 vertices)

Vertices: 1, 2, 3. Edges: 1→2, 2→3, 3→1.

  1. A[0][1] = 1, A[1][2] = 1, A[2][0] = 1. All other entries = 0.
  2. Matrix is NOT symmetric. Each vertex: out-degree = 1, in-degree = 1.
  3. Density = 3 / (3×2) = 0.50 (half of all directed pairs are connected).
123
1010
2001
3100

Example 3 — Reading A² Walk Counts

For the triangle K₃ above, A²[A][A] = A[A][B]×A[B][A] + A[A][C]×A[C][A] = 1+1 = 2. This means there are 2 walks of length 2 from A back to A (A→B→A and A→C→A). The diagonal of A² always equals the degree sequence for undirected graphs.

Frequently Asked Questions

What is an adjacency matrix?
An adjacency matrix is a square n×n matrix A where entry A[i][j] = 1 if there is an edge from vertex i to vertex j, and 0 otherwise. It is one of the two primary graph representations (the other is the adjacency list). Undirected graphs always produce a symmetric matrix (A = Aᵀ), while directed graphs (digraphs) may produce asymmetric ones. The matrix uses O(n²) memory regardless of the number of edges, making it ideal for dense graphs.
How do you build an adjacency matrix from an edge list?
Assign a sequential integer index to each unique vertex label. Create an n×n matrix filled with zeros. For each edge (u, v) in the list, set A[index(u)][index(v)] = 1. For undirected graphs, also set A[index(v)][index(u)] = 1 to ensure symmetry. The diagonal stays 0 unless self-loops exist. This calculator handles all those steps automatically — just type your edges in the Edge List box and click Calculate.
What is the difference between a directed and undirected adjacency matrix?
In an undirected matrix, edge (u, v) and (v, u) are the same, so A[i][j] = A[j][i] always — the matrix is symmetric about the main diagonal. In a directed matrix (digraph), A[i][j] = 1 means there is a directed edge from i to j; A[j][i] may still be 0. Row sums give out-degrees and column sums give in-degrees for directed graphs, while both equal degree for undirected ones.
What does the squared adjacency matrix A² represent?
Entry A²[i][j] counts the number of walks of exactly length 2 from vertex i to vertex j — i.e. the number of distinct intermediate vertices k such that edges (i,k) and (k,j) both exist. In general, A^k[i][j] is the count of walks of length k. For undirected graphs, the diagonal of A² gives the degree of each vertex. The trace of A³ divided by 6 equals the number of triangles in the graph.
How do you find vertex degrees from an adjacency matrix?
For an undirected graph, deg(v) = sum of row v (which equals the sum of column v since the matrix is symmetric). For a directed graph, out-deg(v) = sum of row v (edges leaving v), and in-deg(v) = sum of column v (edges entering v). This calculator shows a per-vertex degree table and the degree sequence (sorted in non-increasing order) for quick verification.
What is graph density and how is it calculated?
Density measures how many edges exist relative to the maximum possible. Undirected: D = 2m / (n(n−1)). Directed: D = m / (n(n−1)), where m is the edge count and n is the vertex count. D = 0 is an empty graph (no edges); D = 1 is a complete graph (every pair connected). Real-world networks (social graphs, web graphs) are typically very sparse with density close to 0.
How do you check graph connectivity using an adjacency matrix?
Run a BFS or DFS from any vertex, using the adjacency matrix to enumerate neighbours of each node. If all n vertices are reached in one traversal, the graph is connected. Algebraically, computing (I + A)^(n−1) and checking that all off-diagonal entries are positive also works, but BFS is more practical. This calculator counts connected components automatically — component count 1 means the graph is connected.
When should I use an adjacency list instead of a matrix?
Use an adjacency matrix when: the graph is dense (m ≈ n²), you need O(1) edge lookups, or you want to apply matrix algebra. Use an adjacency list when: the graph is sparse (m ≪ n²), memory is constrained, or you need fast iteration over neighbours. Lists use O(n + m) vs. O(n²) for matrices. For large real-world graphs (millions of nodes, few edges per node), adjacency lists are far more practical and memory-efficient.

Related Calculators