java - Single root directed acyclic graph cycle detection -
the graph has 1 root. , saved in following format:
0 -> 1, 0 -> 2, 1 -> 3, 1 -> 4, 2 -> 4, 2 -> 5, 4 -> 5, 5 -> 2 (this cycle)
what efficient way detect if there exists @ least 1 cycle in graph using java? thanks!
as mentioned in comments sort of dfs like
set iscyclic false dfs(node) if iscyclic true return each neighbor in node.neighbors if node.visited true set iscyclic true return set neighbor.visited true dfs(neighbor)
Comments
Post a Comment