// compilation unit Johnson.nice
package graph;
<Edge,Graph,Distance,Vertex,ReadWritePM |
Edge <: GraphEdge<Vertex>,
ReadWritePM <: ReadWritePropertyMap<Vertex, Distance>
>
boolean johnsonAllPairsShortestPaths(
VertexListAndIncidenceAndEdgeListGraph<Vertex,Edge> graph,
ReadablePropertyMap<Edge, Distance> w,
ReadWritePropertyMap<Edge, Distance> w2,
ReadWritePropertyMap<Vertex, Distance> distance,
ReadablePropertyMap<Vertex, ReadWritePM> distanceMatrix,
Distance zero,
Distance inf,
(Distance, Distance)->Distance combine,
Distance->Distance negate,
(Distance, Distance)->boolean compare
){
// Set distance to all vertices to zero (emulates special "s" vertex used in CLR)
for (Vertex v : graph.vertices())
distance[v] = zero;
// Do Bellman-Ford to get factors for reweighting
let bfWorked = bellmanFordShortestPaths(
graph, graph.nVertices(), w, new NullPropertyMap(),
distance, combine, compare
);
if (!bfWorked) return false; // Negative-weight cycle
// Set up reweighting
for (e : graph.edges())
w2[e] = combine(
combine(w[e], distance[e.source]),
negate(distance[e.target]) );
// Do Dijkstra from each vertex
for (Vertex v : graph.vertices()) {
let dm = distanceMatrix[v];
dijkstraShortestPaths(
graph, v, new NullPropertyMap(), dm,
w2, compare, combine, inf, zero
);
// Adjust returned distances to account for reweighting
for (Vertex v2 : graph.vertices())
dm[v2] = combine(
combine(dm[v2], distance[v2]),
negate(distance[v]) );
}
return true;
}
-- IsaacGouy - 07 Jan 2004
| Topic JohnsonAllPairsShortestPathsExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.6 | > | r1.5 | > | r1.4 | More } |
|
Revision r1.6 - 28 Apr 2005 - 11:53 GMT - TWikiGuest Parents: WebHome > CodeExamples > GraphParametricTypeExample |
Copyright © 1999-2003 by the contributing authors.
All material on this collaboration platform is the property of the contributing authors. Ideas, requests, problems regarding TWiki? Send feedback. |