Files
ersatztv/ErsatzTV.Core/Search/AdjGraph.cs
T
Jason DoveandGitHub 7e30444857 dependencies and code cleanup (#2117)
* fix validation in new form layout

* pin mediatr to last oss version

* update dependencies

* cleanup code in core

* cleanup code in ffmpeg

* cleanup code in infra

* cleanup code in scanner

* cleanup code in application

* cleanup main code

* cleanup test code

* solution-wide code cleanup
2025-07-06 15:56:17 +00:00

48 lines
1.2 KiB
C#

namespace ErsatzTV.Core.Search;
public class AdjGraph
{
private readonly List<Edge> _edges = [];
public void Clear() => _edges.Clear();
public void AddEdge(string from, string to) => _edges.Add(new Edge(from.ToLowerInvariant(), to.ToLowerInvariant()));
public bool HasCycle(string from)
{
var visited = new System.Collections.Generic.HashSet<string>();
var stack = new System.Collections.Generic.HashSet<string>();
return HasCycleImpl(from.ToLowerInvariant(), visited, stack);
}
public bool HasAnyCycle() => _edges.Any(edge => HasCycle(edge.From));
private bool HasCycleImpl(string node, ISet<string> visited, ISet<string> stack)
{
if (stack.Contains(node))
{
return true;
}
if (!visited.Add(node))
{
return false;
}
stack.Add(node);
foreach (Edge edge in _edges.Where(e => e.From == node))
{
if (HasCycleImpl(edge.To, visited, stack))
{
return true;
}
}
stack.Remove(node);
return false;
}
private record Edge(string From, string To);
}