31 lines
746 B
C#
31 lines
746 B
C#
namespace NodePipeline.Engine.Graph;
|
|
|
|
public static class CycleDetector
|
|
{
|
|
public static bool HasCycle(Dictionary<string, List<string>> graph)
|
|
{
|
|
var visited = new HashSet<string>();
|
|
var inStack = new HashSet<string>();
|
|
|
|
foreach (var node in graph.Keys)
|
|
if (Dfs(node))
|
|
return true;
|
|
|
|
return false;
|
|
|
|
bool Dfs(string current)
|
|
{
|
|
if (inStack.Contains(current)) return true;
|
|
|
|
if (!visited.Add(current)) return false;
|
|
|
|
inStack.Add(current);
|
|
foreach (var next in graph[current])
|
|
if (Dfs(next))
|
|
return true;
|
|
|
|
inStack.Remove(current);
|
|
return false;
|
|
}
|
|
}
|
|
} |