neighbors

Nodes directly connected to a node.

Category: Mathematics > Graph and Network Algorithms

Syntax

nodes = neighbors(G, nodeID)

Returns the neighbors of nodeID.

Description

In an undirected graph a neighbor is any node that shares an edge with nodeID. The count of neighbors is the degree, except that parallel edges give repeated entries. For a digraph only the nodes at the end of outgoing edges are returned; use predecessors for the incoming side.

Input arguments

G
A graph or digraph.
nodeID
A single node, as index or name.

Output arguments

nodes
Column vector of node indices, or a cell array of names if the graph has names. For a digraph these are the successors of the node.

Example

Indices and names

G = graph([1 1 2 3], [2 3 3 4]);
neighbors(G, 3)'
N = graph({'a','b','c'}, {'b','c','a'});
neighbors(N, 'a')'
Output
ans = 1×3

     1     2     4
ans = 1×2 cell

    {'b'}    {'c'}

See also

degree successors predecessors nearest