/*
* (C) Copyright 2003-2017, by John V Sichi and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* This program and the accompanying materials are dual-licensed under
* either
*
* (a) the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation, or (at your option) any
* later version.
*
* or (per the licensee's choosing)
*
* (b) the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*/
package org.jgrapht.alg.shortestpath;
import org.jgrapht.*;
/**
* An implementation of Dijkstra's
* shortest path algorithm using a Fibonacci heap.
*
* @param the graph vertex type
* @param the graph edge type
*
* @author John V. Sichi
*/
public final class DijkstraShortestPath
extends BaseShortestPathAlgorithm
{
private final double radius;
/**
* Constructs a new instance of the algorithm for a given graph.
*
* @param graph the graph
*/
public DijkstraShortestPath(Graph graph)
{
this(graph, Double.POSITIVE_INFINITY);
}
/**
* Constructs a new instance of the algorithm for a given graph.
*
* @param graph the graph
* @param radius limit on path length, or Double.POSITIVE_INFINITY for unbounded search
*/
public DijkstraShortestPath(Graph graph, double radius)
{
super(graph);
if (radius < 0.0) {
throw new IllegalArgumentException("Radius must be non-negative");
}
this.radius = radius;
}
/**
* {@inheritDoc}
*/
@Override
public GraphPath getPath(V source, V sink)
{
if (!graph.containsVertex(source)) {
throw new IllegalArgumentException("graph must contain the source vertex");
}
if (!graph.containsVertex(sink)) {
throw new IllegalArgumentException("graph must contain the sink vertex");
}
if (source.equals(sink)) {
return createEmptyPath(source, sink);
}
DijkstraClosestFirstIterator it =
new DijkstraClosestFirstIterator<>(graph, source, radius);
while (it.hasNext()) {
V vertex = it.next();
if (vertex.equals(sink)) {
break;
}
}
return it.getPaths().getPath(sink);
}
/**
* {@inheritDoc}
*
* Note that in the case of Dijkstra's algorithm it is more efficient to compute all
* single-source shortest paths using this method than repeatedly invoking
* {@link #getPath(Object, Object)} for the same source but different sink vertex.
*/
@Override
public SingleSourcePaths getPaths(V source)
{
if (!graph.containsVertex(source)) {
throw new IllegalArgumentException("graph must contain the source vertex");
}
DijkstraClosestFirstIterator it =
new DijkstraClosestFirstIterator<>(graph, source, radius);
while (it.hasNext()) {
it.next();
}
return it.getPaths();
}
/**
* Find a path between two vertices. For a more advanced search (e.g. limited by radius), use
* the constructor instead.
*
* @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 the graph vertex type
* @param the graph edge type
*
* @return a shortest path, or null if no path exists
*/
public static GraphPath findPathBetween(Graph graph, V source, V sink)
{
return new DijkstraShortestPath<>(graph).getPath(source, sink);
}
}
Ads help maintain this website.