Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entered"></param>
        /// <param name="uData"></param>
        private void Visit(T entered, DFSNodeData <T> uData)
        {
            uData.Color      = Color.Gray;
            uData.Discovered = ++_time;

            OnNodeEntered(entered);

            ISet <T> adjNodes = Graph.Adjacent(entered);

            foreach (T discovered in adjNodes)
            {
                OnEdgeDiscovered(Graph[entered, discovered]);
                DFSNodeData <T> vData = GetNodeData <DFSNodeData <T> >(discovered);

                if (vData.Color == Color.White)
                {
                    vData.Predecessor = entered;
                    OnNodeDiscovered(discovered);
                    Visit(discovered, vData);
                }

                OnEdgeTraversed(Graph[entered, discovered]);
            }
            uData.Color    = Color.Black;
            uData.Finished = ++_time;
            OnNodeExited(entered);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Compute Depth-first search.
        /// </summary>
        public override void Compute()
        {
            ISet <T> nodes = Graph.Vertices;

            foreach (T u in nodes)
            {
                SetNodeData(u, new DFSNodeData <T>(u, Color.White));
            }

            _time = 0;
            if (_useStartNode)
            {
                DFSNodeData <T> uData = GetNodeData <DFSNodeData <T> >(_startNode);

                //this is the actual discovery
                if (uData != null && uData.Color == Color.White)
                {
                    OnNodeDiscovered(_startNode);
                    Visit(_startNode, uData);
                }
            }

            foreach (T u in nodes)
            {
                DFSNodeData <T> uData = GetNodeData <DFSNodeData <T> >(u);

                //this is the actual discovery
                if (uData != null && uData.Color == Color.White)
                {
                    OnNodeDiscovered(u);
                    Visit(u, uData);
                }
            }

            OnComputeCompleted();
        }