findnode

Index of a node given its name.

Category: Mathematics > Graph and Network Algorithms

Syntax

idx = findnode(G, name)

Returns the index of the node called name.

idx = findnode(G, names)

For a cell array of names, returns one index per name.

Description

Node names are stored in G.Nodes.Name. findnode is the lookup from name to position, useful because most graph functions return numeric indices.

Input arguments

G
A graph with node names.
name
Character vector, or cell array of them.

Output arguments

idx
Node index, or 0 for a name that is not in the graph.

Example

Names to indices

G = graph({'a','b','c'}, {'b','c','a'});
findnode(G, 'b')
findnode(G, {'c','a'})
findnode(G, 'zz')
Output
ans = 2
ans = 2×1

     3
     1
ans = 0

See also

findedge neighbors addnode graph