V
- the graph vertex typeE
- the graph edge typepublic class HierholzerEulerianCycle<V,E> extends Object implements EulerianCycleAlgorithm<V,E>
See the Wikipedia article for details and references about Eulerian cycles and a short description of Hierholzer's algorithm for the construction of an Eulerian cycle. The original presentation of the algorithm dates back to 1873 and the following paper: Carl Hierholzer: Über die Möglichkeit, einen Linienzug ohne Wiederholung und ohne Unterbrechung zu umfahren. Mathematische Annalen 6(1), 30–32, 1873.
Modifier and Type | Class and Description |
---|---|
protected class |
HierholzerEulerianCycle.EdgeNode |
protected class |
HierholzerEulerianCycle.VertexNode |
Modifier and Type | Field and Description |
---|---|
protected HierholzerEulerianCycle.EdgeNode |
eulerianHead |
protected Graph<V,E> |
g |
protected boolean |
isDirected |
protected V |
startVertex |
protected HierholzerEulerianCycle.VertexNode |
verticesHead |
Constructor and Description |
---|
HierholzerEulerianCycle() |
Modifier and Type | Method and Description |
---|---|
protected void |
addEdge(HierholzerEulerianCycle.VertexNode sNode,
HierholzerEulerianCycle.VertexNode tNode,
E e)
Add an edge to the index.
|
protected GraphWalk<V,E> |
buildWalk()
Build final walk
|
protected void |
cleanup()
Release any memory held.
|
protected Pair<HierholzerEulerianCycle.EdgeNode,HierholzerEulerianCycle.EdgeNode> |
computePartialCycle()
Computes a partial cycle assuming that all vertices have an even degree.
|
GraphPath<V,E> |
getEulerianCycle(Graph<V,E> g)
Compute an Eulerian cycle of a graph.
|
protected HierholzerEulerianCycle.VertexNode |
getOppositeVertex(HierholzerEulerianCycle.VertexNode v,
HierholzerEulerianCycle.EdgeNode e)
Compute the opposite end-point of an end-point of an edge.
|
protected void |
initialize(Graph<V,E> g)
Index the graph and create a double-linked list representation suitable for vertex and edge
removals in constant time.
|
boolean |
isEulerian(Graph<V,E> graph)
Test whether a graph is Eulerian.
|
protected void |
moveToFront(HierholzerEulerianCycle.VertexNode vNode)
Move a vertex as first in the vertex list.
|
protected void |
unlink(HierholzerEulerianCycle.EdgeNode eNode)
Unlink an edge from the adjacency lists of its end-points.
|
protected void |
unlink(HierholzerEulerianCycle.VertexNode vNode)
Unlink a vertex from the vertex list.
|
protected void |
updateGraphAndInsertLocations(Pair<HierholzerEulerianCycle.EdgeNode,HierholzerEulerianCycle.EdgeNode> partialCycle,
HierholzerEulerianCycle.VertexNode partialCycleSourceVertex)
Iterate over the partial cycle to remove vertices with zero degrees and compute new insert
locations for vertices with non-zero degrees.
|
protected boolean isDirected
protected HierholzerEulerianCycle.VertexNode verticesHead
protected HierholzerEulerianCycle.EdgeNode eulerianHead
protected V startVertex
public boolean isEulerian(Graph<V,E> graph)
graph
- the input graphpublic GraphPath<V,E> getEulerianCycle(Graph<V,E> g)
getEulerianCycle
in interface EulerianCycleAlgorithm<V,E>
g
- the input graphIllegalArgumentException
- in case the graph is not Eulerianprotected void initialize(Graph<V,E> g)
g
- the graph to indexprotected void cleanup()
protected Pair<HierholzerEulerianCycle.EdgeNode,HierholzerEulerianCycle.EdgeNode> computePartialCycle()
protected void updateGraphAndInsertLocations(Pair<HierholzerEulerianCycle.EdgeNode,HierholzerEulerianCycle.EdgeNode> partialCycle, HierholzerEulerianCycle.VertexNode partialCycleSourceVertex)
partialCycle
- the partial cyclepartialCycleSourceVertex
- the source vertex of the first edge in the partial cycleprotected void addEdge(HierholzerEulerianCycle.VertexNode sNode, HierholzerEulerianCycle.VertexNode tNode, E e)
sNode
- source vertextNode
- target vertexe
- original (wrapped) edgeprotected void unlink(HierholzerEulerianCycle.VertexNode vNode)
vNode
- vertex to unlinkprotected void moveToFront(HierholzerEulerianCycle.VertexNode vNode)
vNode
- vertex to move to frontprotected void unlink(HierholzerEulerianCycle.EdgeNode eNode)
eNode
- edge to unlinkprotected HierholzerEulerianCycle.VertexNode getOppositeVertex(HierholzerEulerianCycle.VertexNode v, HierholzerEulerianCycle.EdgeNode e)
v
- vertex that is part of edgee
- edge used to find opposite vertexCopyright © 2019. All rights reserved.