/// <summary> /// Computes a shortest paths tree from <tt>s</tt> to every other vertex in the edge-weighted digraph <tt>G</tt>. /// </summary> /// <param name="graph">the edge-weighted digraph</param> /// <param name="source">the source vertex</param> public DijkstraDirectedSearch(EdgeWeightedDirectedGraph graph, int source) { for (int i = graph.NumberOfEdges - 1; i >= 0; i--) { WeightedGraphEdge e = graph.GetEdge(i); if (e.Cost < 0) { throw new RPGException(ErrorMessage.INTERNAL_BAD_ARGUMENT, "edge " + e + " has negative weight"); } } distTo = new double[graph.NumberOfVertices]; edgeTo = new WeightedGraphEdge[graph.NumberOfVertices]; for (int v = 0; v < graph.NumberOfVertices; v++) { distTo[v] = Double.PositiveInfinity; } distTo[source] = 0.0; // relax vertices in order of distance from s pq = new IndexMinPQ <Double>(graph.NumberOfVertices); pq.Insert(source, distTo[source]); while (!pq.IsEmpty) { int v = pq.DelMin(); WeightedGraphEdge[] adj = graph.GetVertexAdjacencies(v); for (int i = adj.Length - 1; i >= 0; i--) { Relax(adj[i], v); } } // check optimality conditions Debug.Assert(Check(graph, source)); }
// check optimality conditions: // (i) for all edges e: distTo[e.to()] <= distTo[e.from()] + e.weight() // (ii) for all edge e on the SPT: distTo[e.to()] == distTo[e.from()] + // e.weight() private bool Check(EdgeWeightedDirectedGraph graph, int s) { // check that edge weights are nonnegative for (int i = graph.NumberOfEdges - 1; i >= 0; i--) { WeightedGraphEdge e = graph.GetEdge(i); if (e.Cost < 0) { Console.WriteLine("negative edge weight detected"); return(false); } } // check that distTo[v] and edgeTo[v] are consistent if (distTo[s] != 0.0 || edgeTo[s] != null) { Console.WriteLine("distTo[s] and edgeTo[s] inconsistent"); return(false); } for (int v = graph.NumberOfVertices - 1; v >= 0; v--) { if (v == s) { continue; } if (edgeTo[v] == null && distTo[v] != Double.PositiveInfinity) { Console.WriteLine("distTo[] and edgeTo[] inconsistent"); return(false); } } // check that all edges e = v->w satisfy distTo[w] <= distTo[v] + // e.weight() for (int v = graph.NumberOfVertices - 1; v >= 0; v--) { WeightedGraphEdge[] adj = graph.GetVertexAdjacencies(v); for (int i = adj.Length - 1; i >= 0; i--) { WeightedGraphEdge e = adj[i]; int w; if (v == e.To) { w = e.From; } else { w = e.To; } if (distTo[v] + e.Cost < distTo[w]) { Console.WriteLine("edge " + e + " not relaxed"); return(false); } } } // check that all edges e = v->w on SPT satisfy distTo[w] == distTo[v] + // e.weight() for (int w = 0; w < graph.NumberOfVertices; w++) { if (edgeTo[w] == null) { continue; } WeightedGraphEdge e = edgeTo[w]; int v = e.From; if (w != e.To) { return(false); } if (distTo[v] + e.Cost != distTo[w]) { Console.WriteLine("edge " + e + " on shortest path not tight"); return(false); } } return(true); }