org.jgrapht.alg.shortestpath.BaseManyToManyShortestPaths Maven / Gradle / Ivy
The newest version!
/*
* (C) Copyright 2019-2023, by Semen Chudakov and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* See the CONTRIBUTORS.md file distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the
* GNU Lesser General Public License v2.1 or later
* which is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
*
* SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
*/
package org.jgrapht.alg.shortestpath;
import org.jgrapht.*;
import org.jgrapht.alg.interfaces.*;
import java.util.*;
/**
* Base class for many-to-many shortest paths algorithms. Currently extended by
* {@link CHManyToManyShortestPaths} and {@link DijkstraManyToManyShortestPaths}.
*
* @param the graph vertex type
* @param the graph edge type
* @author Semen Chudakov
* @author Dimitrios Michail
*/
abstract class BaseManyToManyShortestPaths
implements ManyToManyShortestPathsAlgorithm
{
protected final Graph graph;
/**
* Constructs a new instance of the algorithm for a given graph.
*
* @param graph the graph
*/
public BaseManyToManyShortestPaths(Graph graph)
{
this.graph = Objects.requireNonNull(graph, "Graph is null");
}
/**
* {@inheritDoc}
*/
@Override
public GraphPath getPath(V source, V sink)
{
return getManyToManyPaths(Collections.singleton(source), Collections.singleton(sink))
.getPath(source, sink);
}
/**
* {@inheritDoc}
*/
@Override
public double getPathWeight(V source, V sink)
{
GraphPath p = getPath(source, sink);
if (p == null) {
return Double.POSITIVE_INFINITY;
} else {
return p.getWeight();
}
}
/**
* {@inheritDoc}
*/
@Override
public SingleSourcePaths getPaths(V source)
{
if (!graph.containsVertex(source)) {
throw new IllegalArgumentException("graph must contain the source vertex");
}
Map> paths = new HashMap<>();
for (V v : graph.vertexSet()) {
paths.put(v, getPath(source, v));
}
return new ListSingleSourcePathsImpl<>(graph, source, paths);
}
/**
* Computes shortest paths tree starting at {@code source} and stopping as soon as all of the
* {@code targets} are reached. Here the {@link DijkstraClosestFirstIterator} is used.
*
* @param graph a graph
* @param source source vertex
* @param targets target vertices
* @param the graph vertex type
* @param the graph edge type
* @return shortest paths starting from {@code source} and reaching all {@code targets}
*/
protected static ShortestPathAlgorithm.SingleSourcePaths getShortestPathsTree(
Graph graph, V source, Set targets)
{
DijkstraClosestFirstIterator iterator =
new DijkstraClosestFirstIterator<>(graph, source);
int reachedTargets = 0;
while (iterator.hasNext() && reachedTargets < targets.size()) {
if (targets.contains(iterator.next())) {
++reachedTargets;
}
}
return iterator.getPaths();
}
}