This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

adjacency

Graph adjacency matrix

Syntax

A = adjacency(G)
A = adjacency(G,'weighted')
A = adjacency(G,weights)

Description

example

A = adjacency(G) returns the sparse adjacency matrix for graph G. If (i,j) is an edge in G, then A(i,j) = 1. Otherwise, A(i,j) = 0.

A = adjacency(G,'weighted') returns a weighted adjacency matrix, where for each edge (i,j), the value A(i,j) contains the weight of the edge. If the graph has no edge weights, then A(i,j) is set to 1. For this syntax, G must be a simple graph such that ismultigraph(G) returns false.

A = adjacency(G,weights) returns a weighted adjacency matrix with edge weights given by the vector weights. For each edge (i,j) in G, the adjacency matrix has value A(i,j) = weights(findedge(G,i,j)). For this syntax, G must be a simple graph such that ismultigraph(G) returns false.

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×4

     0     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 table
    EndNodes    Weight
    ________    ______

     1    2        5  
     1    3        3  
     2    3        1  
     2    4        2  
     3    4       11  

Use adjacency to return the adjacency matrix of the graph. Regardless of the form of adjacency matrix used to construct the graph, the adjacency function 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 table
    EndNodes    Weight
    ________    ______

     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. Since G is a directed graph, the adjacency matrix is not symmetric. However, the adjacency matrix is symmetric for undirected graphs.

B = full(A)
B = 4×4

     0     5     6     7
     0     0     0     8
     0     9     0     0
     0     0    10     0

Input Arguments

collapse all

Input graph, specified as either a graph or digraph object. Use graph to create an undirected graph or digraph to 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 of A is numnodes(G)-by-numnodes(G).

Tips

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

Introduced in R2015b