Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.
n
: Number of nodes in graphm
: Number of edges in graph
current
: Starting nodep
: Parent node (Initialize with -1 at the beginning)
parent
: Array of parents for the path between source node and last visited node in graph.