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

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -