/// <summary> /// Implements the DFT algorithm /// </summary> /// <param name="visitor"></param> /// <param name="v"></param> /// <param name="visited"></param> protected virtual void DepthFirstTraversal(IPrePostVisitor visitor, IVertex v, bool[] visited) { if (!visitor.IsDone) { visitor.PreVisit(v); visitor.Visit(v); visited[v.Number] = true; IEnumerator iEnumerator = v.Successors.GetEnumerator(); try { while (iEnumerator.MoveNext()) { IVertex vertex = (IVertex)iEnumerator.Current; if (!visited[vertex.Number]) { DepthFirstTraversal(visitor, vertex, visited); } } } finally { IDisposable iDisposable = iEnumerator as IDisposable; if (iDisposable != null) { iDisposable.Dispose(); } } visitor.PostVisit(v); } }
/// <summary> /// DFT of the subtree /// </summary> /// <param name="visitor"></param> public override void DepthFirstTraversal(IPrePostVisitor visitor) { if (!base.IsEmpty) { visitor.PreVisit(mKey); Left.DepthFirstTraversal(visitor); visitor.Visit(mKey); Right.DepthFirstTraversal(visitor); visitor.PostVisit(mKey); } }
/// <summary> /// Performs a BFT of the graph with the given visitor and starting at the given vertex number. /// </summary> /// <param name="visitor"></param> /// <param name="start"></param> public virtual void BreadthFirstTraversal(IPrePostVisitor visitor, int start) { bool[] flags = new bool[mNumberOfVertices]; for (int i = 0; i < mNumberOfVertices; i++) { flags[i] = false; } IQueue queue = new QueueAsLinkedList(); queue.Enqueue(vertex[start]); flags[start] = true; while (!(queue.IsEmpty || visitor.IsDone)) { IVertex vertex1 = (IVertex)queue.Dequeue(); visitor.PreVisit(vertex1); Console.WriteLine("being in " + vertex1.Number); IEnumerator iEnumerator = vertex1.Successors.GetEnumerator(); try { while (iEnumerator.MoveNext()) { IVertex vertex2 = (IVertex)iEnumerator.Current; if (!flags[vertex2.Number]) { queue.Enqueue(vertex2); visitor.Visit(vertex2); Console.WriteLine("engueueing " + vertex2.Number); flags[vertex2.Number] = true; } } } finally { IDisposable iDisposable = iEnumerator as IDisposable; if (iDisposable != null) { iDisposable.Dispose(); } } visitor.PostVisit(vertex1); } }