/** * Determine weighted path length to a vertex via an edge, using the path length for the * opposite vertex. * * @param vertex the vertex for which to calculate the path length. * @param edge the edge via which the path is being extended. * * @return calculated path length. */ private double calculatePathLength(V vertex, E edge) { assertNonNegativeEdge(edge); V otherVertex = Graphs.getOppositeVertex(getGraph(), edge, vertex); FibonacciHeapNode<QueueEntry<V, E>> otherEntry = getSeenData(otherVertex); return otherEntry.getKey() + getGraph().getEdgeWeight(edge); }
/** * @see CrossComponentIterator#encounterVertex(Object, Object) */ @Override protected void encounterVertex(V vertex, E edge) { double shortestPathLength; if (edge == null) { shortestPathLength = 0; } else { shortestPathLength = calculatePathLength(vertex, edge); } FibonacciHeapNode<QueueEntry<V, E>> node = createSeenData(vertex, edge); putSeenData(vertex, node); heap.insert(node, shortestPathLength); }
checkRadiusTraversal(isCrossComponentTraversal()); initialized = true; if (!crossComponentTraversal) { hasNext(); Iterator<V> iter = startVertices.iterator(); if (iter.hasNext()) { encounterVertex(v, null);
/** * Override superclass. When we see a vertex again, we need to see if the new edge provides a * shorter path than the old edge. * * @param vertex the vertex re-encountered * @param edge the edge via which the vertex was re-encountered */ @Override protected void encounterVertexAgain(V vertex, E edge) { FibonacciHeapNode<QueueEntry<V, E>> node = getSeenData(vertex); if (node.getData().frozen) { // no improvement for this vertex possible return; } double candidatePathLength = calculatePathLength(vertex, edge); if (candidatePathLength < node.getKey()) { node.getData().spanningTreeEdge = edge; heap.decreaseKey(node, candidatePathLength); } }
/** * Get the weighted length of the shortest path known to the given vertex. If the vertex has * already been visited, then it is truly the shortest path length; otherwise, it is the best * known upper bound. * * @param vertex vertex being sought from start vertex * * @return weighted length of shortest path known, or Double.POSITIVE_INFINITY if no path found * yet */ public double getShortestPathLength(V vertex) { FibonacciHeapNode<QueueEntry<V, E>> node = getSeenData(vertex); if (node == null) { return Double.POSITIVE_INFINITY; } return node.getKey(); }
@Override public void setCrossComponentTraversal(boolean crossComponentTraversal) { if (initialized) { checkRadiusTraversal(crossComponentTraversal); } super.setCrossComponentTraversal(crossComponentTraversal); }
private void assertNonNegativeEdge(E edge) { if (getGraph().getEdgeWeight(edge) < 0) { throw new IllegalArgumentException("negative edge weights not allowed"); } }
/** * Get the spanning tree edge reaching a vertex which has been seen already in this traversal. * This edge is the last link in the shortest known path between the start vertex and the * requested vertex. If the vertex has already been visited, then it is truly the minimum * spanning tree edge; otherwise, it is the best candidate seen so far. * * @param vertex the spanned vertex. * * @return the spanning tree edge, or null if the vertex either has not been seen yet or is a * start vertex. */ public E getSpanningTreeEdge(V vertex) { FibonacciHeapNode<QueueEntry<V, E>> node = getSeenData(vertex); if (node == null) { return null; } return node.getData().spanningTreeEdge; }