Skip to content

AdjacencyMatrix ​

The vertex-by-vertex adjacency matrix of a Graph, indexed by VertexList order.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageAdjacencyMatrix!
AdjacencyMatrix(g)entry (i,j)(i, j) is the number of edges from vertex ii to vertex jj — 0 or 1 for a simple graph, symmetric when every edge is undirected.
Typed signatures
TypePackageApplies
(value) -> list<list<integer>>combinatoricsalways

Domain: Collections

Details
  • Returned as a plain matrix (a list of lists) rather than Wolfram's default SparseArray — a deliberate simplification here, since this library has no sparse-matrix head to return instead.

Examples

Basic #

K4K_4's adjacency matrix — every off-diagonal entry is 1

live ↗
InAdjacencyMatrix(CompleteGraph(4))\mathrm{AdjacencyMatrix}(\mathrm{CompleteGraph}(4))Out[[0,1,1,1],[1,0,1,1],[1,1,0,1],[1,1,1,0]]\bigl\lbrack\bigl\lbrack0, 1, 1, 1\bigr\rbrack, \bigl\lbrack1, 0, 1, 1\bigr\rbrack, \bigl\lbrack1, 1, 0, 1\bigr\rbrack, \bigl\lbrack1, 1, 1, 0\bigr\rbrack\bigr\rbrack

A single directed edge gives an asymmetric matrix

live ↗
InAdjacencyMatrix(Graph([1,2],[DirectedEdge(1,2)]))\mathrm{AdjacencyMatrix}(\mathrm{Graph}(\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack\mathrm{DirectedEdge}(1, 2)\bigr\rbrack))Out[[0,1],[0,0]]\bigl\lbrack\bigl\lbrack0, 1\bigr\rbrack, \bigl\lbrack0, 0\bigr\rbrack\bigr\rbrack
Properties #
live ↗
InShape(AdjacencyMatrix(g))\mathrm{Shape}(\mathrm{AdjacencyMatrix}(g))OutShape(AdjacencyMatrix(g))\mathrm{Shape}(\mathrm{AdjacencyMatrix}(g))
Possible issues #
live ↗
InAdjacencyMatrix(Graph([],[]))\mathrm{AdjacencyMatrix}(\mathrm{Graph}(\bigl\lbrack\bigr\rbrack, \bigl\lbrack\bigr\rbrack))Out[]\bigl\lbrack\bigr\rbrack
About
Examples4
Checked againstWolfram Language 4 differ
CrosswalkWolfram Language

See also: IncidenceMatrix, VertexDegree, WeightedAdjacencyMatrix