Note
This is the documentation for the current state of the development branch of rustworkx. The documentation or APIs here can change prior to being released.
rustworkx.digraph_dfs_search#
- digraph_dfs_search(graph, source=None, visitor=None)#
Depth-first traversal of a directed graph with several source vertices.
Pseudo-code for the depth-first search algorithm with a single source vertex is listed below with annotated event points at which a method of the given
DFSVisitor
is called.# G - graph, s - single source node DFS(G, s) let color be a mapping # color[u] - vertex u color WHITE/GRAY/BLACK for each u in G # u - vertex in G color[u] := WHITE # color all as undiscovered end for time := 0 let S be a stack PUSH(S, (s, iterator of OutEdges(G, s))) # S - stack of vertices and edge iterators color[s] := GRAY # event: discover_vertex(s, time) while (S is not empty) let (u, iterator) := LAST(S) flag := False # whether edge to undiscovered vertex found for each v, w in iterator # v - target vertex, w - edge weight if (WHITE = color[v]) # event: tree_edge((u, v, w)) time := time + 1 color[v] := GRAY # event: discover_vertex(v, time) flag := True break elif (GRAY = color[v]) # event: back_edge((u, v, w)) ... elif (BLACK = color[v]) # event: forward_or_cross_edge((u, v, w)) ... end for if (flag is True) PUSH(S, (v, iterator of OutEdges(G, v))) elif (flag is False) time := time + 1 color[u] := BLACK # event: finish_vertex(u, time) POP(S) end while
For several source nodes, the DFS algorithm is applied on source nodes by the given order.
If an exception is raised inside the callback method of the
DFSVisitor
instance, the graph traversal will be stopped immediately. You can exploit this to exit early by raising aStopSearch
exception. You can also prune part of the search tree by raisingPruneSearch
.In the following example we keep track of the tree edges:
import rustworkx as rx from rustworkx.visit import DFSVisitor class TreeEdgesRecorder(DFSVisitor): def __init__(self): self.edges = [] def tree_edge(self, edge): self.edges.append(edge) graph = rx.PyDiGraph() graph.extend_from_edge_list([(1, 3), (0, 1), (2, 1), (0, 2)]) vis = TreeEdgesRecorder() rx.digraph_dfs_search(graph, [0], vis) print('Tree edges:', vis.edges)
Tree edges: [(0, 2, None), (2, 1, None), (1, 3, None)]
Note
Graph can not be mutated while traversing. Trying to do so raises an exception.
Note
An exception is raised if the
PruneSearch
is raised in thefinish_vertex
event.- Parameters:
graph (PyDiGraph) – The graph to be used.
source – An optional list of node indices to use as the starting nodes for the depth-first search. If
None
or not specified then a source will be chosen arbitrarily and repeated until all components of the graph are searched. This can be aSequence[int]
orNone
.visitor – A visitor object that is invoked at the event points inside the algorithm. This should be a subclass of
DFSVisitor
. This has a default value ofNone
as a backwards compatibility artifact (to preserve argument ordering from an earlier version) but it is a required argument and will raise aTypeError
if not specified.