public virtual TVertex[] FindShortestPath([NotNull] TVertex startVertex, [NotNull] TVertex endVertex) { Validate.ArgumentNotNull(nameof(startVertex), startVertex); Validate.ArgumentNotNull(nameof(endVertex), endVertex); IBfsNode solution = null; TraverseBfs(startVertex, node => { if (!Equals(node.Vertex, endVertex)) { return(true); } solution = node; return(false); }); var result = new List <TVertex>(); while (solution != null) { result.Add(solution.Vertex); solution = solution.Parent; } result.Reverse(); return(result.ToArray()); }
public int GetMinimumNumberOfMoves(IBfsNode <THashType> start) { var matchFound = false; var numberOfMoves = 0; var currentNodes = new Queue <IBfsNode <THashType> >(); currentNodes.Enqueue(start); var nextNodes = new Queue <IBfsNode <THashType> >(); var visited = new HashSet <THashType>(); while (currentNodes.Count > 0) { var currentNode = currentNodes.Dequeue(); if (currentNode.CompareTo(_goal) == 0) { matchFound = true; break; } foreach (var nextNode in currentNode.GetNextNodes()) { if (!visited.Contains(nextNode.UniqueIdentifier)) { nextNodes.Enqueue(nextNode); visited.Add(nextNode.UniqueIdentifier); } } if (currentNodes.Count == 0) { currentNodes = nextNodes; nextNodes = new Queue <IBfsNode <THashType> >(); numberOfMoves += 1; } } return(matchFound ? numberOfMoves : -1); }
public BreadthFirstSearch(IBfsNode <THashType> goal) { _goal = goal; }