Main Content

graphtraverse

(Removed) Traverse graph by following adjacent nodes

graphtraverse has been removed. Use bfsearch or dfsearch instead. For details, see Version History.

Syntax

[disc, pred, closed] = graphtraverse(GS)
[...] = graphtraverse(GS, ...'Depth', DepthValue, ...)
[...] = graphtraverse(GS, ...'Directed', DirectedValue, ...)
[...] = graphtraverse(GS, ...'Method', MethodValue, ...)

Arguments

G N-by-N adjacency matrix that represents a directed graph. Nonzero entries in matrix G indicate the presence of an edge.
SInteger that indicates the source node in graph G.
DepthValueInteger that indicates a node in graph G that specifies the depth of the search. Default is Inf (infinity).
DirectedValueProperty that indicates whether graph G is directed or undirected. Enter false for an undirected graph. This results in the upper triangle of the adjacency matrix being ignored. Default is true.
MethodValueCharacter vector or string that specifies the algorithm used to traverse the graph. Choices are:
  • 'BFS' — Breadth-first search. Time complexity is O(N+E), where N and E are number of nodes and edges respectively.

  • 'DFS' — Default algorithm. Depth-first search. Time complexity is O(N+E), where N and E are number of nodes and edges respectively.

Description

Tip

For introductory information on graph theory functions, see Graph Theory Functions.

[disc, pred, closed] = graphtraverse(GS) traverses graph G starting from the node indicated by integer S. G is an N-by-N adjacency matrix that represents a directed graph. Nonzero entries in matrix G indicate the presence of an edge. disc is a vector of node indices in the order in which they are discovered. pred is a vector of predecessor node indices (listed in the order of the node indices) of the resulting spanning tree. closed is a vector of node indices in the order in which they are closed.

[...] = graphtraverse(GS, ...'PropertyName', PropertyValue, ...) calls graphtraverse with optional properties that use property name/property value pairs. You can specify one or more properties in any order. Each PropertyName must be enclosed in single quotes and is case insensitive. These property name/property value pairs are as follows:

[...] = graphtraverse(GS, ...'Depth', DepthValue, ...) specifies the depth of the search. DepthValue is an integer indicating a node in graph G. Default is Inf (infinity). The 'Depth' name-value argument is no longer supported for the 'DFS' method.

[...] = graphtraverse(GS, ...'Directed', DirectedValue, ...) indicates whether the graph is directed or undirected. Set DirectedValue to false for an undirected graph. This results in the upper triangle of the adjacency matrix being ignored. Default is true.

[...] = graphtraverse(GS, ...'Method', MethodValue, ...) lets you specify the algorithm used to traverse the graph. Choices are:

  • 'BFS' — Breadth-first search. Time complexity is O(N+E), where N and E are number of nodes and edges respectively.

  • 'DFS' — Default algorithm. Depth-first search. Time complexity is O(N+E), where N and E are number of nodes and edges respectively. The 'Depth' name-value argument is no longer supported for the 'DFS' method.

References

[1] Sedgewick, R., (2002). Algorithms in C++, Part 5 Graph Algorithms (Addison-Wesley).

[2] Siek, J.G., Lee, L-Q, and Lumsdaine, A. (2002). The Boost Graph Library User Guide and Reference Manual, (Upper Saddle River, NJ:Pearson Education).

Version History

Introduced in R2006b

expand all

R2022b: Removed

graphtraverse has been removed. Use bfsearch or dfsearch instead. Note that bfsearch and dfsearch do not have the 'Depth' option. An example of an alternative approach would be as follows:

s = [1 1 2 2 2 3 4 7 8 8 8 8];
t = [3 4 7 5 6 2 6 2 9 10 11 12];
g = digraph(s,t);
v = bfsearch(g,1);
d = distances(g,1,v);
maxDepth = 2;
v2 = v(d <= maxDepth);

See Also

|