private void Visit(EdgeWeightedGraph g, int v)
 {
     marked[v] = true;
     foreach (var e in g.Adj(v))
     {
         int w = e.Other(v);
         if (marked[w])
         {
             continue;
         }
         if (e.Weight < distTo[w])
         {
             edgeTo[w] = e;
             distTo[w] = e.Weight;
             if (pq.Contains(w))
             {
                 pq.ChangeKey(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
Exemple #2
0
        private void Scan(EdgeWeightedGraph G, int v)
        {
            _marked[v] = true;

            foreach (Edge e in G.Adj(v))
            {
                int w = e.Target(v);
                if (_marked[w])
                {
                    continue;
                }
                if (e.Weight() < _distTo[w])
                {
                    _distTo[w] = e.Weight();
                    _edgeTo[w] = e;
                    if (_pq.Contains(w))
                    {
                        _pq.ChangeKey(w, _distTo[w]);
                    }
                    else
                    {
                        _pq.Insert(w, _distTo[w]);
                    }
                }
            }
        }
Exemple #3
0
    List <List <int> > GetMST()
    {
        var size    = nodes.NodeList.Count;
        var parents = new int[size];
        var weights = new float[size];
        var marked  = new bool[size];
        var pq      = new IndexMinPriorityQueue <float>(size);

        for (var x = 1; x < size; x++)
        {
            weights[x] = float.MaxValue;
        }
        weights[0] = 0;

        pq.Insert(0, 0);
        while (!pq.IsEmpty())
        {
            int v = pq.DeleteMin();
            marked[v] = true;
            for (var x = 0; x < size; x++)
            {
                if (x != v && !marked[x])
                {
                    var distance = Vector2.Distance(nodes.NodeList[v], nodes.NodeList[x]);
                    if (distance < weights[x])
                    {
                        weights[x] = distance;
                        parents[x] = v;
                        if (pq.Contains(x))
                        {
                            pq.ChangeKey(x, distance);
                        }
                        else
                        {
                            pq.Insert(x, distance);
                        }
                    }
                }
            }
        }

        var result = GetAdjacencyLists(size);

        for (var x = 1; x < parents.Length; x++)
        {
            var target = parents[x];
            result[x].Add(target);
            result[target].Add(x);
        }
        return(result);
    }
Exemple #4
0
        public void ChangeKey_WhenIndexIsInQueue_WillChangeOldKeyToNewKey()
        {
            IndexMinPriorityQueue <double> queue = new IndexMinPriorityQueue <double>(10);

            queue.Insert(4, 12.5);
            queue.Insert(3, 40.12);
            queue.Insert(7, 4.3);
            queue.Insert(2, 162.75);
            double newKey = 5.54;

            queue.ChangeKey(3, newKey);

            double keyAtIndex = queue.KeyAt(3);

            Assert.AreEqual(newKey, keyAtIndex);
        }
Exemple #5
0
 private void Relax(EdgeWeightedDigraph g, int v)
 {
     foreach (var e in g.Adj(v))
     {
         int w = e.To;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (pq.Contains(w))
             {
                 pq.ChangeKey(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }