Ejemplo n.º 1
0
    public PrimMST(EdgeWeightedGraph G)
    {
        edgeTo = new Edge[G.V()];
        distTo = new double[G.V()];
        marked = new bool[G.V()];

        IndexMinPQComparer comparer = new IndexMinPQComparer();

        pq = new IndexMinPQ <double>(G.V(), (Comparer <double>)comparer);
        for (int v = 0; v < G.V(); v++)
        {
            distTo[v] = double.PositiveInfinity;
        }

        for (int v = 0; v < G.V(); v++)      // run from each vertex to find
        {
            if (!marked[v])
            {
                prim(G, v);                  // minimum spanning forest
            }
        }
    }
Ejemplo n.º 2
0
    private IndexMinPQ <double> pq;   // priority queue of vertices


    public DijkstraSP(EdgeWeightedDigraph G, int s)
    {
        foreach (DirectedEdge e in G.edges())
        {
            if (e.Weight() < 0)
            {
                throw new System.Exception("edge " + e + " has negative weight");
            }
        }

        distTo = new double[G.V()];
        edgeTo = new DirectedEdge[G.V()];

        validateVertex(s);

        for (int v = 0; v < G.V(); v++)
        {
            distTo[v] = double.PositiveInfinity;
        }
        distTo[s] = 0.0;


        // relax vertices in order of distance from s
        IndexMinPQComparer comparator = new IndexMinPQComparer();

        pq = new IndexMinPQ <double>(G.V(), (Comparer <double>)comparator);
        pq.insert(s, distTo[s]);
        while (!pq.isEmpty())
        {
            int v = pq.delMin();
            foreach (DirectedEdge e in G.Adj(v))
            {
                relax(e);
            }
        }
    }