Пример #1
0
        private void Explore(int start)
        {
            _color.SetValue(start, RED);

            var queue = new Queue <int>();

            queue.Enqueue(start);
            while (queue.Any())
            {
                var current       = queue.Dequeue();
                var neighborColor = GetNeighborColor(current);
                foreach (var neighbor in _graph.NeighborIndexes(current)
                         .Where(i => i != current))
                {
                    if (_color.Visited(neighbor))
                    {
                        //Check Visited neighbor is the correct color
                        if (_color.GetValue(neighbor) != neighborColor)
                        {
                            throw new NonBipartiteException();
                        }

                        continue;
                    }

                    queue.Enqueue(neighbor);
                    _color.SetValue(neighbor, neighborColor);
                }
            }
        }
        private List <int> GetShortestPath(int from, int to)
        {
            var result = new List <int>();

            while (to != from)
            {
                //No Path Found
                if (to == _visitedFrom.InitialValue)
                {
                    return(new List <int>());
                }

                result.Add(to);
                to = _visitedFrom.GetValue(to);
            }
            result.Reverse();
            return(result);
        }
        private void Explore(int start)
        {
            _distance.SetValue(start, 0);

            var queue = new Queue <int>();

            queue.Enqueue(start);
            while (queue.Any())
            {
                var current = queue.Dequeue();
                foreach (var neighbor in _graph.NeighborIndexes(current).Where(i => i != current))
                {
                    if (_distance.Visited(neighbor))
                    {
                        continue;
                    }

                    queue.Enqueue(neighbor);
                    _distance.SetValue(neighbor, _distance.GetValue(current) + 1);
                    _visitedFrom.SetValue(neighbor, current);
                }
            }
        }