public IPath <TNode, TEdge> FindFirst(TNode initialNode, Func <TNode, bool> targetPredicate, ProgressReporterCallback progressReporter = null) { var result = FindAll(initialNode, targetPredicate, progressReporter, 1); return(result.FirstOrDefault()); }
public IList <IPath <TNode, TEdge> > FindAll(TNode initialNode, Func <TNode, bool> targetPredicate, ProgressReporterCallback progressReporter = null, int minResults = int.MaxValue) { var visitedNodes = new HashSet <NodeWithPredecessor>(_comparer); var nextNodes = new HashSet <NodeWithPredecessor>(_comparer) { new NodeWithPredecessor(initialNode) }; var results = new List <IPath <TNode, TEdge> >(); if (targetPredicate(initialNode)) { results.Add(new BfsPath(initialNode)); } while (nextNodes.Count > 0) { progressReporter?.Invoke(visitedNodes.Count, nextNodes.Count); visitedNodes.UnionWith(nextNodes); var expanded = nextNodes.AsParallel() .SelectMany(sourceNode => _expander(sourceNode.Current) .Select(dest => new NodeWithPredecessor(dest, sourceNode)) .Where(dest => !visitedNodes.Contains(dest))); nextNodes = new HashSet <NodeWithPredecessor>(expanded, _comparer); foreach (var node in nextNodes) { if (targetPredicate(node.Current)) { results.Add(new BfsPath(node)); } } if (results.Count >= minResults) { break; } } return(results); }