Beispiel #1
0
        public void Run(IVertex startVertex)
        {
            if (_verticesCount == 0)
            {
                return;
            }

            var queue = new Queue <IVertex>();

            queue.Enqueue(startVertex);
            _discovered[startVertex.Index - 1] = true;
            var adjList = _graph.ToAdjList();

            while (queue.Count > 0)
            {
                var v = queue.Dequeue();
                ProcessVertexEarly?.Invoke(v);
                _processed[v.Index - 1] = true;
                var p = adjList[v];
                var i = 0;
                var adjVerticesCount = p.Count();
                while (i < adjVerticesCount)
                {
                    var y = p.ElementAt(i);
                    if (!_processed[y.Index - 1] || _graph.IsDirected)
                    {
                        ProcessEdge?.Invoke(v, y);
                    }
                    if (!_discovered[y.Index - 1])
                    {
                        ProcessChild?.Invoke(v, y);
                        queue.Enqueue(y);
                        _discovered[y.Index - 1] = true;
                        _parent[y.Index - 1]     = v;
                    }
                    i++;
                }
                ProcessVertexLate?.Invoke(v);
            }
        }
        public void Run(IVertex vertex)
        {
            if (_verticesCount == 0 || _isFinished)
            {
                return;
            }

            ProcessVertexEarly?.Invoke(vertex);

            _time++;
            _entryTime[vertex.Index - 1]    = _time;
            _isDiscovered[vertex.Index - 1] = true;

            foreach (var adjVertex in _adjList[vertex])
            {
                if (!_isDiscovered[adjVertex.Index - 1])
                {
                    _parent = vertex;
                    ProcessEdge?.Invoke(vertex, adjVertex);
                    Run(adjVertex);
                }
                else if (!_isProcessed[adjVertex.Index - 1] || _graph.IsDirected)
                {
                    ProcessEdge?.Invoke(vertex, adjVertex);
                }
                if (_isFinished)
                {
                    return;
                }
            }

            ProcessVertexLate?.Invoke(vertex);
            _time++;
            // write exit time
            _isProcessed[vertex.Index - 1] = true;
        }