/** * Updates to be performed if the path to v through u is the shortest * path to v found so far. * * @param u Vertex u * @param v Vertex v * @param uvWeight w(u,v) * @param e Edge e * @param queue Queue */ protected void shortestPathSoFarUpdate(V startNode, V u, V v, Double uvWeight, E e, PriorityQueue<V> queue) { // Reset the predecessors and add u as a predecessor v.clear(); v.addPredecessor(u); v.addPredecessorEdge(e); // Set the distance v.setDistance(u.getDistance() + uvWeight); largestDistanceSoFar = v.getDistance(); // Update the queue. queue.remove(v); queue.add(v); }
/** * Updates to be performed if the path to v through u is a new multiple * shortest path. There is no need to set the distance on v since this * is a multiple shortest path. * * @param u Vertex u * @param v Vertex v * @param e Edge e */ protected void multipleShortestPathUpdate(V u, V v, E e) { // Add u to the list of predecessors. v.addPredecessor(u); v.addPredecessorEdge(e); }
private void addPredEdges(KeyedGraph<VDijkstra, Edge> graph, VDijkstra dest, SimpleResultSet output, int localID) throws SQLException { // Rebuild the shortest path(s). (Yes, there could be more than // one if they have the same distance!) final Set<Edge> predEdges = dest.getPredecessorEdges(); // The only vertex with no predecessors is the source vertex, so we can // start renumbering here. if (predEdges.isEmpty()) { globalID++; } // Recursively add the predecessor edges. for (Edge e : predEdges) { final VDijkstra edgeSource = graph.getEdgeSource(e); final VDijkstra edgeDestination = graph.getEdgeTarget(e); // Right order if (edgeDestination.equals(dest)) { output.addRow(e.getID(), globalID, localID, edgeSource.getID(), edgeDestination.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeSource, output, localID + 1); } // Wrong order else { output.addRow(e.getID(), globalID, localID, edgeDestination.getID(), edgeSource.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeDestination, output, localID + 1); } } }
graph.getEdgeFactory(), currentStartNode); for (V v : graph.vertexSet()) { Set<E> predEdges = (Set<E>) v.getPredecessorEdges(); for (E e : predEdges) { V source = graph.getEdgeSource(e); V target = graph.getEdgeTarget(e); if (source.getDistance() < radius && target.getDistance() < radius) { traversalGraph.addVertex(source); traversalGraph.addVertex(target);
source.setSource(); return source.getDistance(); } else { return target.getDistance();
@Override public int compare(V v1, V v2) { return Double.compare( v1.getDistance(), v2.getDistance()); } });
/** * Clears the predecessor list, sets the distance and dependency to zero and * the shortest path count to one. */ @Override public void setSource() { // Clear the predecessor list and set the distance to zero. super.setSource(); // Set the shortest path count to one and the dependency to zero. vCent.setSource(); }
/** * Clears the predecessor list, resets the distance to the default value and * resets the shortest path count and dependency to zero. */ @Override public void reset() { // Clear the predecessor list and reset the distance to the default // distance. super.reset(); // Reset the shortest path count and dependency to zero. vCent.reset(); }
private void addPredEdges(KeyedGraph<VDijkstra, Edge> graph, VDijkstra dest, SimpleResultSet output, int localID) throws SQLException { // Rebuild the shortest path(s). (Yes, there could be more than // one if they have the same distance!) final Set<Edge> predEdges = dest.getPredecessorEdges(); // The only vertex with no predecessors is the source vertex, so we can // start renumbering here. if (predEdges.isEmpty()) { globalID++; } // Recursively add the predecessor edges. for (Edge e : predEdges) { final VDijkstra edgeSource = graph.getEdgeSource(e); final VDijkstra edgeDestination = graph.getEdgeTarget(e); // Right order if (edgeDestination.equals(dest)) { output.addRow(e.getID(), globalID, localID, edgeSource.getID(), edgeDestination.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeSource, output, localID + 1); } // Wrong order else { output.addRow(e.getID(), globalID, localID, edgeDestination.getID(), edgeSource.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeDestination, output, localID + 1); } } }
@Override protected boolean preRelaxStep(V startNode, V u) { // If there are no more targets, then stop the search. if (remainingTargets.isEmpty()) { return true; } else { // If u is a target, then remove it from the // remaining targets and record its distance. if (remainingTargets.remove(u)) { distances.put(u, u.getDistance()); } // If there are no more targets, then stop the search. if (remainingTargets.isEmpty()) { return true; } } return false; } }.calculate(source);
private void addPredEdges(KeyedGraph<VDijkstra, Edge> graph, VDijkstra dest, SimpleResultSet output, Map<Integer, Geometry> edgeGeomMap, int localID) throws SQLException { // Rebuild the shortest path(s). (Yes, there could be more than // one if they have the same distance!) final Set<Edge> predEdges = dest.getPredecessorEdges(); // The only vertex with no predecessors is the source vertex, so we can // start renumbering here. if (predEdges.isEmpty()) { globalID++; } // Recursively add the predecessor edges. for (Edge e : predEdges) { final VDijkstra edgeSource = graph.getEdgeSource(e); final VDijkstra edgeDestination = graph.getEdgeTarget(e); final Geometry geometry = edgeGeomMap.get(Math.abs(e.getID())); // Right order if (edgeDestination.equals(dest)) { output.addRow(geometry, e.getID(), globalID, localID, edgeSource.getID(), edgeDestination.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeSource, output, edgeGeomMap, localID + 1); } // Wrong order else { output.addRow(geometry, e.getID(), globalID, localID, edgeDestination.getID(), edgeSource.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeDestination, output, edgeGeomMap, localID + 1); } } }
/** * Relaxes the edge outgoing from u and updates the queue appropriately. * * @param u Vertex u. * @param e Edge e. * @param queue The queue. */ protected void relax(V startNode, V u, E e, PriorityQueue<V> queue) { // Get the target vertex. V v = Graphs.getOppositeVertex(graph, e, u); // Get the weight. double uvWeight = graph.getEdgeWeight(e); // If a smaller distance estimate is available, make the necessary // updates. if (v.getDistance() > u.getDistance() + uvWeight) { shortestPathSoFarUpdate(startNode, u, v, uvWeight, e, queue); } else if (Math.abs(v.getDistance() - (u.getDistance() + uvWeight)) < TOLERANCE) { multipleShortestPathUpdate(u, v, e); } }
private void addPredEdges(KeyedGraph<VDijkstra, Edge> graph, VDijkstra dest, SimpleResultSet output, int localID) throws SQLException { // Rebuild the shortest path(s). (Yes, there could be more than // one if they have the same distance!) final Set<Edge> predEdges = dest.getPredecessorEdges(); // The only vertex with no predecessors is the source vertex, so we can // start renumbering here. if (predEdges.isEmpty()) { globalID++; } // Recursively add the predecessor edges. for (Edge e : predEdges) { final VDijkstra edgeSource = graph.getEdgeSource(e); final VDijkstra edgeDestination = graph.getEdgeTarget(e); // Right order if (edgeDestination.equals(dest)) { output.addRow(e.getID(), globalID, localID, edgeSource.getID(), edgeDestination.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeSource, output, localID + 1); } // Wrong order else { output.addRow(e.getID(), globalID, localID, edgeDestination.getID(), edgeSource.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeDestination, output, localID + 1); } } }
private static ResultSet manyToManySeparateTables( Connection connection, String inputTable, String orientation, String weight, String sourceTable, String destTable) throws SQLException { final SimpleResultSet output = prepareResultSet(); final KeyedGraph<VDijkstra, Edge> graph = prepareGraph(connection, inputTable, orientation, weight, VDijkstra.class, Edge.class); final Statement st = connection.createStatement(); try { final Set<VDijkstra> destSet = getSet(st, graph, destTable); final Set<VDijkstra> sourceSet = getSet(st, graph, sourceTable); final Dijkstra<VDijkstra, Edge> dijkstra = new Dijkstra<VDijkstra, Edge>(graph); for (VDijkstra source : sourceSet) { Map<VDijkstra, Double> distances = dijkstra.oneToMany(source, destSet); for (Map.Entry<VDijkstra, Double> destToDistMap : distances.entrySet()) { output.addRow(source.getID(), destToDistMap.getKey().getID(), destToDistMap.getValue()); } } } finally { st.close(); } return output; }
private void addPredEdges(KeyedGraph<VDijkstra, Edge> graph, VDijkstra dest, SimpleResultSet output, Map<Integer, Geometry> edgeGeomMap, int localID) throws SQLException { // Rebuild the shortest path(s). (Yes, there could be more than // one if they have the same distance!) final Set<Edge> predEdges = dest.getPredecessorEdges(); // The only vertex with no predecessors is the source vertex, so we can // start renumbering here. if (predEdges.isEmpty()) { globalID++; } // Recursively add the predecessor edges. for (Edge e : predEdges) { final VDijkstra edgeSource = graph.getEdgeSource(e); final VDijkstra edgeDestination = graph.getEdgeTarget(e); final Geometry geometry = edgeGeomMap.get(Math.abs(e.getID())); // Right order if (edgeDestination.equals(dest)) { output.addRow(geometry, e.getID(), globalID, localID, edgeSource.getID(), edgeDestination.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeSource, output, edgeGeomMap, localID + 1); } // Wrong order else { output.addRow(geometry, e.getID(), globalID, localID, edgeDestination.getID(), edgeSource.getID(), graph.getEdgeWeight(e)); addPredEdges(graph, edgeDestination, output, edgeGeomMap, localID + 1); } } }
private static ResultSet manyToManySeparateTables( Connection connection, String inputTable, String orientation, String weight, String sourceTable, String destTable) throws SQLException { final SimpleResultSet output = prepareResultSet(); final KeyedGraph<VDijkstra, Edge> graph = prepareGraph(connection, inputTable, orientation, weight, VDijkstra.class, Edge.class); final Statement st = connection.createStatement(); try { final Set<VDijkstra> destSet = getSet(st, graph, destTable); final Set<VDijkstra> sourceSet = getSet(st, graph, sourceTable); final Dijkstra<VDijkstra, Edge> dijkstra = new Dijkstra<VDijkstra, Edge>(graph); for (VDijkstra source : sourceSet) { Map<VDijkstra, Double> distances = dijkstra.oneToMany(source, destSet); for (Map.Entry<VDijkstra, Double> destToDistMap : distances.entrySet()) { output.addRow(source.getID(), destToDistMap.getKey().getID(), destToDistMap.getValue()); } } } finally { st.close(); } return output; }