Skip to content

Commit

Permalink
Terminate BFSShortestPath#getPath when target vertex is found. Issue#…
Browse files Browse the repository at this point in the history
…1158 jgrapht#1158
  • Loading branch information
Pratyushgoyal66 committed May 19, 2024
1 parent 6324b13 commit 4bb970e
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ public BFSShortestPath(Graph<V, E> graph)
*/
@Override
public SingleSourcePaths<V, E> getPaths(V source)
{
return getPaths(source, null);
}

/**
* Compute all shortest paths from single source vertex if no sink provided.
* If sink provided, will compute shortest path from source vertex to target vertex.
* @param source - The source vertex.
* @param sink - Optional, the target vertex.
* @return - The shortest paths if no sink provided.
* Returns the shortest path between source and sink if sink is provided.
*/
private SingleSourcePaths<V, E> getPaths(V source, V sink)
{
if (!graph.containsVertex(source)) {
throw new IllegalArgumentException(GRAPH_MUST_CONTAIN_THE_SOURCE_VERTEX);
Expand Down Expand Up @@ -87,6 +100,14 @@ public SingleSourcePaths<V, E> getPaths(V source)
double newDist = distanceAndPredecessorMap.get(v).getFirst() + 1.0;
distanceAndPredecessorMap.put(u, Pair.of(newDist, e));
}

/*
* Break the loop if target vertex is found.
*/
if(u.equals(sink)) {
queue.clear();
break;
}
}
}

Expand All @@ -104,16 +125,16 @@ public GraphPath<V, E> getPath(V source, V sink)
if (!graph.containsVertex(sink)) {
throw new IllegalArgumentException(GRAPH_MUST_CONTAIN_THE_SINK_VERTEX);
}
return getPaths(source).getPath(sink);
return getPaths(source, sink).getPath(sink);
}

/**
* Find a path between two vertices.
*
*
* @param graph the graph to be searched
* @param source the vertex at which the path should start
* @param sink the vertex at which the path should end
*
*
* @param <V> the graph vertex type
* @param <E> the graph edge type
*
Expand Down

0 comments on commit 4bb970e

Please sign in to comment.