/*
* (C) Copyright 2016-2017, by Dimitrios Michail 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 java.io.*;
import java.util.*;
import org.jgrapht.*;
import org.jgrapht.alg.interfaces.ShortestPathAlgorithm.*;
import org.jgrapht.graph.*;
/**
* An implementation of {@link SingleSourcePaths} which stores one path per vertex.
*
*
* This is an explicit representation which stores all paths. For a more compact representation see
* {@link TreeSingleSourcePathsImpl}.
*
* @author Dimitrios Michail
*
* @param the graph vertex type
* @param the graph edge type
*/
public class ListSingleSourcePathsImpl
implements SingleSourcePaths, Serializable
{
private static final long serialVersionUID = -60070018446561686L;
/**
* The graph
*/
protected Graph graph;
/**
* The source vertex of all paths
*/
protected V source;
/**
* One path per vertex
*/
protected Map> paths;
/**
* Construct a new instance.
*
* @param graph the graph
* @param source the source vertex
* @param paths one path per target vertex
*/
public ListSingleSourcePathsImpl(Graph graph, V source, Map> paths)
{
this.graph = Objects.requireNonNull(graph, "Graph is null");
this.source = Objects.requireNonNull(source, "Source vertex is null");
this.paths = Objects.requireNonNull(paths, "Paths are null");
}
/**
* {@inheritDoc}
*/
@Override
public Graph getGraph()
{
return graph;
}
/**
* {@inheritDoc}
*/
@Override
public V getSourceVertex()
{
return source;
}
/**
* {@inheritDoc}
*/
@Override
public double getWeight(V targetVertex)
{
GraphPath p = paths.get(targetVertex);
if (p == null) {
if (source.equals(targetVertex)) {
return 0d;
} else {
return Double.POSITIVE_INFINITY;
}
} else {
return p.getWeight();
}
}
/**
* {@inheritDoc}
*/
@Override
public GraphPath getPath(V targetVertex)
{
GraphPath p = paths.get(targetVertex);
if (p == null) {
if (source.equals(targetVertex)) {
return new GraphWalk<>(
graph, source, targetVertex, Collections.singletonList(source),
Collections.emptyList(), 0d);
} else {
return null;
}
} else {
return p;
}
}
}
Ads help maintain this website.