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!

example

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

Popular posts from this blog

testing - Detect whether test has failed within fixture -

AbotX : How do you create a parallel crawler that stays on and can be added to at run time from new requests -

android - Create single AAR file from multiple modules using Gradle -