Main Content

neighbors

Neighbors of graph node

Description

N = neighbors(G,nodeID) returns the node IDs of all nodes connected by an edge to the node specified by nodeID.

example

Examples

collapse all

Create and plot a graph, and then determine the neighbors of node 10.

G = graph(bucky);
plot(G)

Figure contains an axes object. The axes object contains an object of type graphplot.

N = neighbors(G,10)
N = 3×1

     6
     9
    12

Input Arguments

collapse all

Input graph, specified as a graph object. Use graph to create an undirected graph object.

Example: G = graph(1,2)

Node identifier, specified as one of the values in this table.

ValueExample
Scalar node index1
Character vector node name'A'
String scalar node name"A"

Example: N = neighbors(G,3)

Example: N = neighbors(G,'A')

Output Arguments

collapse all

Neighboring nodes, returned as node indices if nodeID is numeric, or as node names if nodeID is a node name. A node that is connected to itself by an edge (a self-loop) is listed as its own neighbor only once.

Extended Capabilities

Version History

Introduced in R2015b

expand all