adjacency

Adjacency matrix of a graph.

Category: Mathematics > Graph and Network Algorithms

Syntax

A = adjacency(G)

Returns the matrix with a 1 where two nodes are joined by an edge.

A = adjacency(G, 'weighted')

Uses the edge weights as the matrix entries.

Description

For an undirected graph the matrix is symmetric, because each edge joins the nodes in both directions. For a digraph row i holds the edges leaving node i.

The row sums of the unweighted matrix are the node degrees, which links adjacency with degree and laplacian. The inverse conversion is graph(A) or digraph(A).

Input arguments

G
A graph or digraph.
'weighted'
Request edge weights in place of ones.

Output arguments

A
Square matrix of size numnodes(G). A(i,j) is nonzero when an edge goes from node i to node j.

Example

Plain and weighted

G = graph([1 1 2], [2 3 3], [4 6 5]);
adjacency(G)
adjacency(G, 'weighted')
Output
ans = 3×3

     0     1     1
     1     0     1
     1     1     0
ans = 3×3

     0     4     6
     4     0     5
     6     5     0

Notes

MatLivre returns full (dense) matrices here, where MATLAB returns sparse ones. Values and sizes are the same.

See also

graph digraph degree laplacian incidence