/// <summary> /// Determine whether node is contained in currrent or previous segments. /// </summary> /// <param name="node">Tested node.</param> /// <returns><c>true</c> whether node is contained, <c>false</c> otherwise.</returns> internal bool Contains(NodeReference node) { if (Node.Equals(node)) { return(true); } if (PreviousSegment == null) { return(false); } return(PreviousSegment.Contains(node)); }
/// <summary> /// Get paths between given nodes. /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="maxLength"></param> /// <param name="maxWidth"></param> /// <returns></returns> public IEnumerable <KnowledgePath> GetPaths(NodeReference from, NodeReference to, int maxLength, int maxWidth) { if (from == null || to == null) { yield break; } var currentQueue = new Queue <PathSegment>(); var visitedNodes = new HashSet <NodeReference>(); var startSegment = new PathSegment(null, null, from); if (from.Equals(to)) { yield return(new KnowledgePath(startSegment)); yield break; } //starting node currentQueue.Enqueue(startSegment); //delimiter - for counting path length currentQueue.Enqueue(null); visitedNodes.Add(from); visitedNodes.Add(to); var currentPathLength = 0; while (currentQueue.Count > 0 && currentPathLength < maxLength) { var currentSegment = currentQueue.Dequeue(); if (currentSegment == null) { ++currentPathLength; //add next delimiter currentQueue.Enqueue(null); continue; } //test if we can get into end node foreach (var edge in BetweenEdges(currentSegment.Node, to)) { var segment = new PathSegment(currentSegment, edge, to); yield return(new KnowledgePath(segment)); } //explore next children foreach (var childPair in GetNeighbours(currentSegment.Node, maxWidth)) { var edge = childPair.Item1; var child = childPair.Item2; if (!visitedNodes.Add(child)) { //this node has already been visited continue; } var childSegment = new PathSegment(currentSegment, edge, child); currentQueue.Enqueue(childSegment); } } }