Main Content

adjacency

Graph adjacency matrix

Description

example

A= adjacency(G)returns the sparse adjacency matrix for graphG. If(i,j)is an edge inG, thenA(i,j) = 1. Otherwise,A(i,j) = 0.

A= adjacency(G,'weighted')returns a weighted adjacency matrix, where for each edge(i,j), the valueA(i,j)contains the weight of the edge. If the graph has no edge weights, thenA(i,j)设置为1。对于这个语法,Gmust be a simple graph such thatismultigraph(G)returnsfalse.

A= adjacency(G,weights)returns a weighted adjacency matrix with edge weights given by the vectorweights. For each edge(i,j)inG, the adjacency matrix has valueA(i,j) = weights(findedge(G,i,j)). For this syntax,Gmust be a simple graph such thatismultigraph(G)returnsfalse.

Examples

collapse all

Create a directed graph using an edge list, and then find the equivalent adjacency matrix representation of the graph. The adjacency matrix is returned as a sparse matrix.

s = [1 1 1 2 2 3]; t = [2 3 4 5 6 7]; G = digraph(s,t)
G = digraph with properties: Edges: [6x1 table] Nodes: [7x0 table]
A = adjacency(G)
A = (1,2) 1 (1,3) 1 (1,4) 1 (2,5) 1 (2,6) 1 (3,7) 1

Create an undirected graph using an upper triangular adjacency matrix. When constructing a graph with an adjacency matrix, the nonzero values in the matrix correspond to edge weights.

A = [0 5 3 0;0 0 1 2; 0 0 0 11; 0 0 0 0]
A =4×40 5 3 0 0 0 1 2 0 0 0 11 0 0 0 0
G = graph(A,'upper')
G = graph with properties: Edges: [5x2 table] Nodes: [4x0 table]
G.Edges
ans=5×2 tableEndNodes体重…____ ______ 1 2 5 1 3 3 2 3 1 2 4 2 3 4 11

Useadjacencyto return the adjacency matrix of the graph. Regardless of the form of adjacency matrix used to construct the graph, theadjacencyfunction always returns a symmetric and sparse adjacency matrix containing only 1s and 0s.

B = adjacency(G)
B = (2,1) 1 (3,1) 1 (1,2) 1 (3,2) 1 (4,2) 1 (1,3) 1 (2,3) 1 (4,3) 1 (2,4) 1 (3,4) 1

Create a weighted graph.

G = digraph([1 1 1 2 3 4],[2 3 4 4 2 3],[5 6 7 8 9 10]); G.Edges
ans=6×2 tableEndNodes体重…____ ______ 1 2 5 1 3 6 1 4 7 2 4 8 3 2 9 4 3 10

Find the adjacency matrix of the graph.

A = adjacency(G)
A = (1,2) 1 (3,2) 1 (1,3) 1 (4,3) 1 (1,4) 1 (2,4) 1

This form of the adjacency matrix does not include the edge weights. Use the'weighted'option to include the edge weights in the adjacency matrix.

A = adjacency(G,'weighted')
A = (1,2) 5 (3,2) 9 (1,3) 6 (4,3) 10 (1,4) 7 (2,4) 8

Preview a full storage version of the matrix. SinceGis a directed graph, the adjacency matrix is not symmetric. However, the adjacency matrixissymmetric for undirected graphs.

B = full(A)
B =4×40 5 6 7 0 0 0 8 0 9 0 0 0 0 10 0

Input Arguments

collapse all

输入图,指定为either agraphordigraphobject. Usegraphto create an undirected graph ordigraphto create a directed graph.

Example:G = graph(1,2)

Example:G = digraph([1 2],[2 3])

Edge weights, specified as a vector.

Example:A = adjacency(G,[1 2 3 4])

Data Types:double|logical
Complex Number Support:Yes

Output Arguments

collapse all

Adjacency matrix, returned as a sparse matrix. The size ofAisnumnodes(G)-by-numnodes(G).

Tips

  • Edges with weight zero are not visible in the sparse adjacency matrix returned byadjacency. This means that a weighted adjacency matrix can represent a weighted graph only if there are no edges of weight zero.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2015b