Esempio n. 1
0
 public virtual void Search()
 {
     for (var v = 0; v < _g.Size(); v++)
     {
         Explore(v, new HashSet <int>());
     }
 }
        public static PrimsResult Calculate(AdjacencyListGraph <decimal> graph)
        {
            var size = graph.Size();

            //Initialize result with 0 as first vertex
            var result = new PrimsResult(size);

            result.Cost.SetValue(0, 0);

            //priority queue of costs
            var q = new MinPriorityQueue <decimal>(size, decimal.MaxValue);

            for (var i = 0; i < size; i++)
            {
                q.Enqueue(i, result.Cost.GetValue(i));
            }

            //Walk
            while (!q.IsEmpty())
            {
                //Console.WriteLine("Queue: {0}", q);
                var currentIndex = q.Dequeue();

                //Console.WriteLine("Extract: {0}", currentIndex);
                foreach (var edge in graph.Neighbors(currentIndex))
                {
                    var z = edge.Right;
                    var w = edge.Weight;
                    if (!q.Contains(z) || result.Cost.GetValue(z) <= w)
                    {
                        continue;
                    }

                    result.Cost.SetValue(z, w);
                    result.Parent.SetValue(z, currentIndex);
                    q.ChangePriority(z, w);
                }
            }
            return(result);
        }
 public BreadthFirstSearchWithShortestPath(AdjacencyListGraph <long> g)
 {
     _graph       = g;
     _visitedFrom = new SearchData <int>(g.Size(), -1);
     _distance    = new SearchData <int>(g.Size(), -1);
 }
Esempio n. 4
0
 public DepthFirstSearchWithComponents(AdjacencyListGraph <long> g)
 {
     _graph     = g;
     _component = new SearchData <int>(g.Size(), -1);
 }
Esempio n. 5
0
 public BreadthFirstSearchWithBipartiteDetection(AdjacencyListGraph <long> g)
 {
     _graph = g;
     _color = new SearchData <int>(g.Size(), -1);
 }