コード例 #1
0
    // Test program
    public static void Main(string[] args)
    {
        PairingHeap <int> h = new PairingHeap <int>( );
        int numItems        = 10000;
        int i = 37;
        int j;

        Console.WriteLine("Checking; no bad output is good");
        for (i = 37; i != 0; i = (i + 37) % numItems)
        {
            h.Insert(i);
        }
        for (i = 1; i < numItems; i++)
        {
            if (h.DeleteMin( ) != i)
            {
                Console.WriteLine("Oops! " + i);
            }
        }

        List <IPriorityQueuePosition <int> > p = new List <IPriorityQueuePosition <int> >( );

        for (i = 0; i < numItems; i++)
        {
            p.Add(null);
        }

        for (i = 0, j = numItems / 2; i < numItems; i++, j = (j + 71) % numItems)
        {
            p[j] = h.Insert(j + numItems);
        }
        for (i = 0, j = numItems / 2; i < numItems; i++, j = (j + 53) % numItems)
        {
            h.DecreaseKey(p[j], p[j].GetValue( ) - numItems);
        }
        i = -1;
        while (!h.IsEmpty( ))
        {
            if (h.DeleteMin( ) != ++i)
            {
                Console.WriteLine("Oops! " + i + " ");
            }
        }
        Console.WriteLine("Check completed");
    }
コード例 #2
0
        // Single-source weighted shortest-path algorithm using pairing heaps.
        public void Dijkstra2(string startName)
        {
            IPriorityQueue <Path> pq = new PairingHeap <Path>();

            Vertex start = vertexMap[startName]; // throws an exception if not found

            ClearAll();
            start.pos  = pq.Insert(new Path(start, 0));
            start.dist = 0;

            while (!pq.IsEmpty())
            {
                Path   vrec = pq.DeleteMin();
                Vertex v    = vrec.dest;

                foreach (Edge e in v.adj)
                {
                    Vertex w   = e.dest;
                    double cvw = e.cost;

                    if (cvw < 0)
                    {
                        throw new GraphException("Graph has negative edges");
                    }

                    if (w.dist > v.dist + cvw)
                    {
                        w.dist = v.dist + cvw;
                        w.prev = v;

                        Path newVal = new Path(w, w.dist);
                        if (w.pos == null)
                        {
                            w.pos = pq.Insert(newVal);
                        }
                        else
                        {
                            pq.DecreaseKey(w.pos, newVal);
                        }
                    }
                }
            }
        }