java.lang.Object
org.jgrapht.alg.tour.HamiltonianPathAlgorithmBase<V,E>
- Type Parameters:
V- the graph vertex typeE- the graph edge type
- All Implemented Interfaces:
HamiltonianPathAlgorithm<V,E>
- Direct Known Subclasses:
BacktrackingHamiltonianPath,DagHamiltonianPath,HeldKarpHamiltonianPath
public abstract class HamiltonianPathAlgorithmBase<V,E>
extends Object
implements HamiltonianPathAlgorithm<V,E>
Base class for
HamiltonianPathAlgorithm implementations.
Shares the small amount of plumbing common to the Hamiltonian path solvers in this package:
input validation, the singleton-vertex special case, an open-vertex-list to GraphPath
converter, and an adjacency-list builder that collapses parallel edges and skips self-loops.
This class is structurally similar to HamiltonianCycleAlgorithmBase but adapted to
the open-path semantics (start and end vertex may differ; no closing edge is appended).
- Author:
- seilat
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected int[][]buildAdjacency(Graph<V, E> graph, List<V> indexToVertex, Map<V, Integer> vertexToIndex, boolean directed) Builds an integer-indexed adjacency list over the supplied vertex ordering.protected voidrequireNotEmpty(Graph<V, E> graph) ThrowsIllegalArgumentExceptionifgraphhas no vertices.singletonPath(Graph<V, E> graph) Returns the trivial Hamiltonian path of a graph with exactly one vertex: aGraphWalkwhose vertex list contains only that vertex, no edges, and weight0.Converts a complete vertex sequence into aGraphPath.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.jgrapht.alg.interfaces.HamiltonianPathAlgorithm
getPath
-
Constructor Details
-
HamiltonianPathAlgorithmBase
protected HamiltonianPathAlgorithmBase()Constructs a new instance.
-
-
Method Details
-
requireNotEmpty
ThrowsIllegalArgumentExceptionifgraphhas no vertices.- Parameters:
graph- the input graph- Throws:
IllegalArgumentException- ifgraphhas no vertices
-
singletonPath
Returns the trivial Hamiltonian path of a graph with exactly one vertex: aGraphWalkwhose vertex list contains only that vertex, no edges, and weight0.- Parameters:
graph- the input graph, which must contain exactly one vertex- Returns:
- the single-vertex path
-
vertexListToPath
Converts a complete vertex sequence into aGraphPath. For every consecutive vertex pair(u, v)the edge returned bygraph.getEdge(u, v)is appended; the path weight is the sum of the chosen edges'graph.getEdgeWeightvalues. In multigraphs the chosen edge is an arbitrary representative; this method does not attempt to minimise weight across parallel edges.- Parameters:
vertices- the vertex sequence (must contain at least one vertex)graph- the input graph- Returns:
- a
GraphPathcorresponding tovertices
-
buildAdjacency
protected int[][] buildAdjacency(Graph<V, E> graph, List<V> indexToVertex, Map<V, Integer> vertexToIndex, boolean directed) Builds an integer-indexed adjacency list over the supplied vertex ordering. Self-loops are skipped because they cannot participate in a simple path; parallel edges between the same vertex pair collapse to a single neighbour entry, which is sufficient for the existence-only search performed by the Hamiltonian path solvers.For directed graphs only outgoing edges are recorded. For undirected graphs every incident edge contributes a neighbour entry.
- Parameters:
graph- the input graphindexToVertex- vertex ordering used as the row index of the returned adjacencyvertexToIndex- inverse ofindexToVertexfor fast neighbour lookupdirected-trueifgraphis directed- Returns:
- adjacency list as a jagged
int[][]; rowilists the neighbours ofindexToVertex.get(i)as indices intoindexToVertex
-