({{, (RefLink[DepthFirstScan, paclet:ref/DepthFirstScan])[g,s,{"(Subscript[event, 1])"->(Subscript[f, 1]),"(Subscript[event, 2])"->(Subscript[f, 2]),…}]
performs a depth-first scan of the graph g starting at the vertex s and evaluates (Subscript[f, i]) whenever "(Subscript[event, i])" occurs.}, {, (RefLink[DepthFirstScan, paclet:ref/DepthFirstScan])[g,{"(Subscript[event, 1])"->(Subscript[f, 1]),"(Subscript[event, 2])"->(Subscript[f, 2]),…}]
performs a depth-first scan of the whole graph g.}, {, (RefLink[DepthFirstScan, paclet:ref/DepthFirstScan])[{v->w,…},…]
uses rules v->w to specify the graph g.}})

OpenersNotesSection(Spacer1)Details

(RefLink[DepthFirstScan, paclet:ref/DepthFirstScan])[g,s,{…}] visits vertices in the graph g connected to the vertex s in depth-first order.

In depth-first order, vertices adjacent to the most recently visited are visited first.

(RefLink[DepthFirstScan, paclet:ref/DepthFirstScan])[g,{…}] performs multiple depth-first scans starting from the first vertex in the vertex list of g, then starts from the first vertex in the vertex list that has not been visited, and so on, effectively scanning each connected component.

(RefLink[DepthFirstScan, paclet:ref/DepthFirstScan])[g,…] gives a list {(Subscript[w, 1]),(Subscript[w, 2]),…} representing a tree where (Subscript[w, i]) is the predecessor of (Subscript[v, i]) and where {(Subscript[v, 1]),(Subscript[v, 2]),…} is the vertex list of g.

Events that provide access to vertex discovery include:

"DiscoverVertex"->fd calls fd[u,v,d] when vertex u is discovered from visited vertex v at distance d from the start vertex s.

"UnvisitedVertex"->fru calls fru[u,v] when the unvisited vertex u is rediscovered from the visited vertex v.

"VisitedVertex"->frv calls frv[u,v] when the visited vertex u is rediscovered from the visited vertex v.

Events that provide access to vertex visits include:

"PrevisitVertex"->fs calls fs[u] before the vertex u has been visited.

"PostvisitVertex"->fe calls fe[u] after the vertex u has been visited.

The DFS tree is the tree generated by the edges traversed during a depth-first scan.

Events that provide access to edge exploration from the visited vertex include:

"FrontierEdge"->ffe calls ffe[v->u] for an edge v->u where the vertex v is being visited and u has not been discovered. This is typically useful for scanning the DFS tree.

"BackEdge"->fbe calls fbe[v->u] for an edge v->u where the vertex v is being visited and u has already been discovered and is an ancestor to v in the DFS tree. This is typically useful for finding loops.

"ForwardEdge"->gfe calls gfe[v->u] for an edge v->u where vertex v is being visited and u has already been discovered and is a descendant to v in the DFS tree.

"CrossEdge"->fce calls fce[v->u] for an edge v->u where v is being visited and u has already been discovered and is not in the current DFS tree or is in the same DFS tree, but in a different branch. This is typically useful for detecting multiple DFS trees.

For an undirected graph, the edges used in the callbacks are taken to be undirected edges v<->u.

OpenersPrimaryExamplesSection(Spacer1)Examples  (3)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Visit the vertices of a tree in depth-first order:

DepthFirstScan[,1,{"PrevisitVertex"->(Print["Visiting ",#]&)}];

Highlight the depth-first scan tree:

GridGraph[{3,5},VertexSize->{2->Medium},GraphHighlightStyle->"Thick"]

Reap[DepthFirstScan[%,2,{"FrontierEdge"->Sow}]][[2,1]]//HighlightGraph[%,#]&

Add labels that show the order in which vertices are visited:

g=CompleteKaryTree[3,2,ImagePadding->10];

i=1;
DepthFirstScan[g,1,{"PrevisitVertex"->((PropertyValue[{g,#},VertexLabels]=i++)&)}];
g

g=GridGraph[{5,5},ImagePadding->10];

i=1;
DepthFirstScan[g,1,{"PrevisitVertex"->((PropertyValue[{g,#},VertexLabels]=i++)&)}]; g

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[BreadthFirstScan, paclet:ref/BreadthFirstScan])  ▪  (RefLink[Graph, paclet:ref/Graph])  ▪  (RefLink[TreeGraph, paclet:ref/TreeGraph])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Computation on Graphs, paclet:guide/ComputationOnGraphs])}, {▪(RefLinkPlain[Graph Programming, paclet:guide/GraphProgramming])}})}})