public ShortestPathTree(EdgeWeightedDirectedGraph G, int s)
        {
            foreach (DirectedEdge e in G.Edges())
            {
                if (e.Weight < 0)
                    throw new ArgumentException("edge " + e + " has negative weight");
            }

            distTo = new double[G.VerticesCount];
            edgeTo = new DirectedEdge[G.VerticesCount];
            for (int v = 0; v < G.VerticesCount; v++)
                distTo[v] = Double.PositiveInfinity;
            distTo[s] = 0.0;

            // relax vertices in order of distance from s
            pq = new IndexMinPriorityQueue<Double>(G.VerticesCount);
            pq.Insert(s, distTo[s]);
            while (!pq.IsEmpty)
            {
                int v = pq.DeleteMin();
                foreach (DirectedEdge e in G.AdjacentsOf(v))
                    Relax(e);
            }

        }
 private EdgeWeightedDirectedGraph CreateEdgeWeightedDirectedGraph()
 {
     EdgeWeightedDirectedGraph g = new EdgeWeightedDirectedGraph(8);
     DirectedEdge[] edges = { new DirectedEdge(0, 1, 5), new DirectedEdge(0, 7, 8), new DirectedEdge(0, 4, 9),
                             new DirectedEdge(1, 2, 12), new DirectedEdge(1, 3, 15), new DirectedEdge(1, 7, 4),
                             new DirectedEdge(2, 3, 3), new DirectedEdge(2, 6, 11),
                             new DirectedEdge(3, 6, 9),
                             new DirectedEdge(4, 5, 4), new DirectedEdge(4, 6, 20), new DirectedEdge(4, 7, 5),
                             new DirectedEdge(5, 2, 1), new DirectedEdge(5, 6, 13),
                             new DirectedEdge(7, 5, 6), new DirectedEdge(7, 2, 7) };
     foreach (DirectedEdge e in edges)
     {
         g.AddEdge(e);
     }
     return g;
 }