Cycle Detection for Undirected Graph or Directed Graph Using Recursive Function(Java)
Cycle is a path of edges that traverse from a node to itself or from a node to its starting
vertex. For cycle detection, Depth First Traversal (DFS) can be used to detect cycle in the
graph and able to work on both undirected graph and digraph. The data structure of the
graph is first created by using the graph function and used by the algorithm. The algorithm
is based on the concept of recursion to move to the next adjacent vertices and backtrack
to the previous vertex if there is no other adjacent vertex. For every visited vertex A, if
there is an adjacent vertex B such that B is already visited and B is not the parent vertex of
A, then there is a cycle. If there is not such vertex, then we can say that there is no cycle.