private IndexMinPQ<Double> pq;    // priority queue of vertices

    /**
     * Computes a shortest-paths tree from the source vertex {@code s} to every
     * other vertex in the edge-weighted graph {@code G}.
     *
     * @param  G the edge-weighted digraph
     * @param  s the source vertex
     * @throws IllegalArgumentException if an edge weight is negative
     * @throws IllegalArgumentException unless {@code 0 <= s < V}
     */
    public DijkstraUndirectedSP(EdgeWeightedGraph G, int s) {
        for (Edge e : G.edges()) {
            if (e.weight() < 0)
                throw new IllegalArgumentException("edge " + e + " has negative weight");
        }

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

        validateVertex(s);

        for (int v = 0; v < G.V(); v++)
            distTo[v] = Double.POSITIVE_INFINITY;
        distTo[s] = 0.0;

        // relax vertices in order of distance from s
        pq = new IndexMinPQ<Double>(G.V());
        pq.insert(s, distTo[s]);
        while (!pq.isEmpty()) {
            int v = pq.delMin();
            for (Edge e : G.adj(v))
                relax(e, v);
        }

        // check optimality conditions
        assert check(G, s);
    }
示例#2
0
    private static void merge(int[] streams)
    {
        int n = streams.Length;

        IndexMinPQ <string> pq = new IndexMinPQ <string>(n);

        for (int i = 0; i < n; i++)
        {
            if (!streams[i].Equals(null))
            {
                pq.insert(i, streams[i].ToString());
            }
        }

        // Extract and print min and read next from its stream.
        while (!pq.isEmpty())
        {
            print(pq.minKey() + " ");
            int i = pq.delMin();
            if (!streams[i].Equals(null))
            {
                pq.insert(i, streams[i].ToString());
            }
        }
    }
示例#3
0
 // run Prim's algorithm in graph G, starting from vertex s
 private void prim(EdgeWeightedGraph G, int s) {
     distTo[s] = 0.0;
     pq.insert(s, distTo[s]);
     while (!pq.isEmpty()) {
         int v = pq.delMin();
         scan(G, v);
     }
 }
示例#4
0
    // merge together the sorted input streams and write the sorted result to standard output
    private static void merge(In[] streams) {
        int n = streams.length;
        IndexMinPQ<String> pq = new IndexMinPQ<String>(n);
        for (int i = 0; i < n; i++)
            if (!streams[i].isEmpty())
                pq.insert(i, streams[i].readString());

        // Extract and print min and read next from its stream. 
        while (!pq.isEmpty()) {
            StdOut.print(pq.minKey() + " ");
            int i = pq.delMin();
            if (!streams[i].isEmpty())
                pq.insert(i, streams[i].readString());
        }
        StdOut.println();
    }
示例#5
0
        public PrimMST(EdgeWeightedGraph G)
        {
            edgeTo = new Edge[G.v()];
            distTo = new double[G.v()];
            mark   = new bool[G.v()];
            for (int v = 0; v < G.v(); v++)
            {
                distTo[v] = double.MaxValue;
            }
            pq = new IndexMinPQ <double>(4);

            distTo[0] = 0.0;
            pq.insert(0, 0.0);
            while (!pq.isEmpty())
            {
                visit(G, pq.delMin());
            }
        }
示例#6
0
        public PrimeMST(EdgeWeightedGraph graph)
        {
            _edgeTo = new Edge[graph.V];
            _distTo = new double[graph.V];
            _marked = new bool[graph.V];

            for (int i = 0; i < graph.V; i++)
            {
                _distTo[i] = double.PositiveInfinity;
            }
            _pq = new IndexMinPQ <double>(graph.V);

            _distTo[0] = 0.0;
            _pq.insert(0, 0.0);
            while (!_pq.isEmpty())
            {
                Visit(graph, _pq.delMin());
            }
        }
示例#7
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);
            }
        }
    }