Exemple #1
0
    public void visit(int u)
    {
        WeightSpareGraph <float> .adjIterator iter = new WeightSpareGraph <float> .adjIterator(graph, u);

        for (Edge <float> e = iter.begin(); !iter.isEnd(); e = iter.Next())
        {
            if (book [e.Other(u)])
            {
                continue;
            }
            int v = e.Other(u);              // u

            if (toEdgeArr [v] == null)
            {
                toEdgeArr [v] = e;
                indexMinHeap.Insert(v, e.weight);
            }
            else
            {
                if (toEdgeArr [v].CompareTo(e) > 0)
                {
                    toEdgeArr [v] = e;
                    indexMinHeap.Change(v, e.weight);
                }
            }
        }
    }
Exemple #2
0
    //测试是否改变某个堆中某个数字只需要,对该数字ShiftUP 和shiftDown即可
    public void testChangeInIndexHeap(IndexMinHeap <int> minIndexHeap)
    {
        print("改变前");
        minIndexHeap.print();
        int index = Random.Range(1, minIndexHeap.Size());
        int item  = 10;

        minIndexHeap.Change(index, 10);
        print("改变后,改变第" + index + "元素,改为" + item);

        minIndexHeap.print();
    }
Exemple #3
0
    public Dijkstra(WeightSpareGraph <float> graph, int src)
    {
        Debug.Assert(src >= 0 && src < graph.V());
        this.src   = src;
        this.graph = graph;
        this.n     = graph.V();
        this.m     = graph.E();
        shortPath  = new float[n];
        book       = new bool[n];
        from       = new int[n];
        for (int i = 0; i != n; i++)
        {
            book [i] = false;
            from [i] = -1;            //假设都是可达的
        }

        from [src]      = -1;
        shortPath [src] = 0f;
        IndexMinHeap <float> indexMinHeap = new IndexMinHeap <float> (n);

        indexMinHeap.Insert(src, shortPath[src]);
        while (indexMinHeap.Size() != 0)
        {
            int u = indexMinHeap.ExtraMinItemIndex();
            book [u] = true;
            WeightSpareGraph <float> .adjIterator iter = new WeightSpareGraph <float> .adjIterator(graph, u);

            for (Edge <float> e = iter.begin(); !iter.isEnd(); e = iter.Next())
            {
                int v = e.Other(u);
                if (!book[v])
                {
                    if (from [v] == -1 || shortPath [v] > shortPath [u] + e.weight)
                    {
                        from [v]      = u;
                        shortPath [v] = shortPath [u] + e.weight;
                        if (indexMinHeap.isContain(v))
                        {
                            indexMinHeap.Change(v, shortPath [v]);
                        }
                        else
                        {
                            indexMinHeap.Insert(v, shortPath[v]);
                        }
                    }
                }
            }
        }
    }
Exemple #4
0
 private void Visit(int v)
 {
     if (!marked[v])
     {
         marked[v] = true;
         foreach (Edge <TWeight> e in G.GetAdjIterator(v))
         {
             var w = e.Other(v);
             if (!marked[w])
             {
                 if (EdgeTo[w] == null)
                 {
                     ipq.Insert(w, e.Weight);
                     EdgeTo[w] = e;
                 }
                 else if (e.Weight.CompareTo(EdgeTo[w].Weight) < 0)
                 {
                     EdgeTo[w] = e;
                     ipq.Change(w, e.Weight);
                 }
             }
         }
     }
 }