예제 #1
0
        public override int Execute()
        {
            if (queue.Count == 0)
            {
                IsFinished = true; // search ended, node has not been found
                return(-1);
            }
            int        node          = queue.Dequeue();
            List <int> adjacentNodes = graph.GetAdjacentNodes(node);
            bool       allVisited    = true;

            foreach (var i in adjacentNodes)
            {
                if (!visited.Contains(i))
                {
                    queue.Enqueue(i);
                    visited.Add(i);
                    allVisited = false;
                }
            }
            if (allVisited)
            {
                OnAllChildrenVisited?.Invoke(node);
                OnHasNoChildren?.Invoke(node); // same thing in BFS
            }
            return(node);                      // return current node
        }
예제 #2
0
        public GraphEnumerator(Graph g, TraversalStrategyEnum ts = TraversalStrategyEnum.BFS)
        {
            graph   = g ?? throw new NullReferenceException("The enumerable graph cannot be null.");
            version = g.Version;
            switch (ts)
            {
            case TraversalStrategyEnum.BFS:
            {
                strategy = new TraversalStrategyBFS(graph);
                break;
            }

            case TraversalStrategyEnum.DFS:
            {
                strategy = new TraversalStrategyDFS(graph);
                break;
            }
            }
            // if strategy's event fires, fire ours
            strategy.OnAllChildrenVisited += delegate(int nodeIndex)
            {
                OnAllChildrenVisited?.Invoke(nodeIndex);
            };
            strategy.OnHasNoChildren += delegate(int nodeIndex)
            {
                OnHasNoChildren?.Invoke(nodeIndex);
            };
        }
예제 #3
0
 public override int Execute()
 {
     while (true)
     {
         if (stack.Count == 0) // if stack is empty traversal is over
         {
             IsFinished = true;
             return(-1);
         }
         int node = stack.Pop();
         if (!visited.Contains(node)) // we haven't been here before
         {
             visited.Add(node);       // mark as visited
             if (HasChildren(node))
             {
                 stack.Push(node);
                 List <int> adjacentNodes = graph.GetAdjacentNodes(node);
                 foreach (var i in adjacentNodes) // push all children on stack
                 {
                     if (!visited.Contains(i))
                     {
                         stack.Push(i);
                     }
                 }
             }
             else
             {
                 OnHasNoChildren?.Invoke(node);
             }
             return(node);
         }
         else
         {
             if (!allChildrenVisited.Contains(node))
             {
                 OnAllChildrenVisited?.Invoke(node);
                 allChildrenVisited.Add(node);
             }
         }
     }
 }