Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

adjacency

Graph adjacency matrix

Syntax

A = adjacency(G)

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.

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 = 

     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=5x2 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],[2 3 4 4],[5 6 7 8])
G = 
  digraph with properties:

    Edges: [4x2 table]
    Nodes: [4x0 table]

Find the adjacency matrix of the graph.

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

This form of the adjacency matrix does not include the edge weights in the graph.

Use the sparse function to construct a weighted adjacency matrix representation of the graph.

nn = numnodes(G);
[s,t] = findedge(G);
A = sparse(s,t,G.Edges.Weight,nn,nn)
A = 
   (1,2)        5
   (1,3)        6
   (1,4)        7
   (2,4)        8

Preview a full storage version of the matrix.

full(A)
ans = 

     0     5     6     7
     0     0     0     8
     0     0     0     0
     0     0     0     0

This reconstruction of the weighted adjacency matrix also can be accomplished with undirected graphs. In that case, the procedure is similar, but has one final step:

A = A + A.' - diag(diag(A));

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])

Output Arguments

collapse all

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

Introduced in R2015b

Was this topic helpful?