/// <inheritdoc />
        public void Run(Node entrypoint)
        {
            var visited = new HashSet <Node>();
            var queue   = new Queue <(Node node, Edge origin, int depth)>();

            queue.Enqueue((entrypoint, null, 0));

            while (queue.Count > 0)
            {
                var(node, origin, depth) = queue.Dequeue();

                var eventArgs = new NodeDiscoveryEventArgs(node, origin, depth)
                {
                    ContinueExploring = visited.Add(node)
                };
                OnNodeDiscovered(eventArgs);

                if (eventArgs.Abort)
                {
                    return;
                }

                if (eventArgs.ContinueExploring)
                {
                    foreach (var edge in node.OutgoingEdges)
                    {
                        queue.Enqueue((edge.GetOtherNode(node), edge, depth + 1));
                    }
                }
            }

            OnTraversalCompleted();
        }
Exemple #2
0
        /// <inheritdoc />
        public void Run(Node entrypoint)
        {
            if (entrypoint == null)
            {
                throw new ArgumentNullException(nameof(entrypoint));
            }

            var visited = new HashSet <Node>();
            var stack   = new Stack <(Node node, Edge edge)>();

            stack.Push((entrypoint, null));

            while (stack.Count > 0)
            {
                var(node, origin) = stack.Pop();
                var eventArgs = new NodeDiscoveryEventArgs(node, origin)
                {
                    ContinueExploring = visited.Add(node)
                };
                OnNodeDiscovered(eventArgs);

                if (eventArgs.Abort)
                {
                    return;
                }

                if (eventArgs.ContinueExploring)
                {
                    foreach (var edge in node.OutgoingEdges)
                    {
                        stack.Push((edge.GetOtherNode(node), edge));
                    }
                }
            }
        }
 private void TraversalOnNodeDiscovered(object sender, NodeDiscoveryEventArgs e)
 {
     if (!_indices.ContainsKey(e.NewNode))
     {
         _indices[e.NewNode] = _indices.Count;
         _order.Add(e.NewNode);
     }
 }
        private void TraversalOnNodeDiscovered(object sender, NodeDiscoveryEventArgs e)
        {
            if (!e.ContinueExploring)
            {
                return;
            }

            if (e.Origin == null)
            {
                AddRemainingNodes();
            }
            else
            {
                var originNode = e.Origin.GetOtherNode(e.NewNode);
                while (_stack.Peek() != originNode)
                {
                    _order.Add(_stack.Pop());
                }
            }

            _stack.Push(e.NewNode);
        }
 protected virtual void OnNodeDiscovered(NodeDiscoveryEventArgs e)
 {
     NodeDiscovered?.Invoke(this, e);
 }