How do I check if a directed graph is acyclic?

后端 未结 11 1775
野的像风
野的像风 2020-11-30 19:16

How do I check if a directed graph is acyclic? And how is the algorithm called? I would appreciate a reference.

11条回答
  •  无人及你
    2020-11-30 19:40

    The solution given by ShuggyCoUk is incomplete because it might not check all nodes.

    
    def isDAG(nodes V):
        while there is an unvisited node v in V:
            bool cycleFound = dfs(v)
            if cyclefound:
                return false
        return true
    

    This has timecomplexity O(n+m) or O(n^2)

提交回复
热议问题