How do I check if a directed graph is acyclic? And how is the algorithm called? I would appreciate a reference.
I know this is an old topic but for future searchers here is a C# implementation I created (no claim that it's most efficient!). This is designed to use a simple integer to identify each node. You can decorate that however you like provided your node object hashes and equals properly.
For Very deep graphs this may have high overhead, as it creates a hashset at each node in depth (they are destroyed over breadth).
You input the node from which you want to search and the path take to that node.
When checking for cycles below any given node, just pass that node along with an empty hashset
private bool FindCycle(int node, HashSet path)
{
if (path.Contains(node))
return true;
var extendedPath = new HashSet(path) {node};
foreach (var child in GetChildren(node))
{
if (FindCycle(child, extendedPath))
return true;
}
return false;
}