private static void ValidateSourceNode(ListGraphNode <TNode> source) { if (source is null) { throw new NullReferenceException("The source is not exist in the graph!"); } }
private bool HasPath( ListGraphNode <TNode> source , ListGraphNode <TNode> destination , HashSet <TNode> visited) { ValidateSourceNode(source); var alreadyVisited = visited.Contains(source.Value); if (alreadyVisited) { return(false); } MarkSourceVisited(source, visited); var destinationFinded = source == destination; if (destinationFinded) { return(true); } return(LookingUpAtNeighbors(source, destination, visited)); }
private bool LookingUpAtNeighbors( ListGraphNode <TNode> source, ListGraphNode <TNode> destination, HashSet <TNode> visited) { foreach (var child in source.Neighbors) { if (HasPath(child, destination, visited)) { return(true); } } return(false); }
private void MarkSourceVisited(ListGraphNode <TNode> source, HashSet <TNode> visited) => _ = visited.Add(source.Value);