/** * Work to be done if this is a shortest path from the start node to * neighbor via current. * * @param current Current node * @param neighbor Neighbor node * @param e Edge (current, neighbor) */ protected void shortestPathStep(V current, V neighbor, E e) { // Set the predecessor. neighbor.addPredecessor(current); neighbor.addPredecessorEdge(e); } }
/** * Enqueue neighbor and set neighbor's distance to be one more than * current's distance. * * @param current Current node * @param neighbor Neighbor node * @param queue The queue */ protected void enqueueAndUpdateDistance(final V current, final V neighbor, LinkedList<V> queue) { // Update the distance. neighbor.setDistance(current.getDistance() + 1); // Enqueue the neighbor. queue.add(neighbor); }
/** * 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(); }
/** * 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(); }
/** * Does a breadth first search from the given start node to all other nodes. * * @param startNode Start node */ @Override public void calculate(V startNode) { init(startNode); // While the queue is not empty ... while (!queue.isEmpty()) { V current = dequeueStep(queue); // For every neighbor of the current node ... Set<E> outgoingEdges = outgoingEdgesOf(current); for (E e : outgoingEdges) { V neighbor = Graphs.getOppositeVertex(graph, e, current); // If this neighbor is found for the first time ... if (neighbor.getDistance() < 0) { enqueueAndUpdateDistance(current, neighbor, queue); firstTimeFoundStep(current, neighbor); } // If this is a shortest path from startNode to neighbor // via current ... if (neighbor.getDistance() == current.getDistance() + 1) { shortestPathStep(current, neighbor, e); } } } }