April 2018
Beginner to intermediate
426 pages
10h 19m
English
So far, we have only demonstrated how the DFS algorithm works. We can use it for other functionalities than just outputting the order of vertices visited.
Given a graph G, the DFS algorithm traverses all the vertices of G and constructs a forest (a collection of rooted trees) together with a set of source vertices (roots) and outputs two arrays: the discovery time and finish explorer time. We can modify the depthFirstSearch function to return some information for us, such as the following:
Let's take a look at the implementation of the BFS method:
export const DFS = graph => { const vertices = graph.getVertices(); ...