If you have arrived here immediately after beginning the algorithm (i.e. with still set to be ), stop β there is no path from to . Otherwise, return to the vertex adjoined before . Set and return to Step 1.
In Step 1 of the algorithm, there is no specification on how to choose a single satisfying the search criteria from multiple possibilities. In other words, there is flexibility in the implementation of the algorithm here, and for the problem at hand there may be implementation choices that are more expedient then others.
We perform a Depth-first search on the graph in Figure 16.4.4, attempting to find a path from vertex to vertex . In carrying out the algorithm, if we always choose the vertex with the smallest label in Step 1, we obtain the graph in Figure 16.4.(a). The graph in Figure 16.4.(b) is the result of always choosing the vertex with the largest label.
Figure16.4.4.An example graph to illustrate depth-first search.
(a)Result of always choosing to move to the next adjacent vertex of smallest index.
(b)Result of always choosing to move to the next adjacent vertex of largest index.
Figure16.4.5.Results of two different implementation choices in a depth-first search.
For each vertex in added in the last application of this step (or, in the case of the first application of this step, for ), adjoin all vertices of that are adjacent to and not already in , along with a single edge between each such vertex and . If at least one vertex has been adjoined to in this step, proceed to Step 2. Otherwise, stop β there is no path from to in .