Class HierholzerEulerianCycle<V,E>

Type Parameters:
V - the graph vertex type
E - the graph edge type
All Implemented Interfaces:

public class HierholzerEulerianCycle<V,E> extends Object implements EulerianCycleAlgorithm<V,E>
An implementation of Hierholzer's algorithm for finding an Eulerian cycle in Eulerian graphs. The algorithm works with directed and undirected graphs which may contain loops and/or multiple (parallel) edges. The running time is linear, i.e. $O(|E|)$ where $|E|$ is the cardinality of the edge set of the graph.

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.

Dimitrios Michail
  • Field Details

  • Constructor Details

    • HierholzerEulerianCycle

      public HierholzerEulerianCycle()
  • Method Details

    • isEulerian

      public boolean isEulerian(Graph<V,E> graph)
      Test whether a graph is Eulerian. An Eulerian graph is a graph containing an Eulerian cycle.
      graph - the input graph
      true if the graph is Eulerian, false otherwise
    • getEulerianCycle

      public GraphPath<V,E> getEulerianCycle(Graph<V,E> g)
      Compute an Eulerian cycle of a graph.
      Specified by:
      getEulerianCycle in interface EulerianCycleAlgorithm<V,E>
      g - the input graph
      an Eulerian cycle
      IllegalArgumentException - in case the graph is not Eulerian
    • initialize

      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. Ignore any vertices with zero degrees.
      g - the graph to index
    • cleanup

      protected void cleanup()
      Release any memory held.
    • computePartialCycle

      Computes a partial cycle assuming that all vertices have an even degree. The partial cycle always begin from the first graph vertex in the vertex list.
      the partial's cycle head and tail nodes as a pair
    • updateGraphAndInsertLocations

      protected void updateGraphAndInsertLocations(Pair<HierholzerEulerianCycle<V,E>.EdgeNode,HierholzerEulerianCycle<V,E>.EdgeNode> partialCycle, HierholzerEulerianCycle<V,E>.VertexNode partialCycleSourceVertex)
      Iterate over the partial cycle to remove vertices with zero degrees and compute new insert locations for vertices with non-zero degrees. It is important to move vertices with new insert locations to the front of the vertex list, in order to make sure that we always start partial cycles from already visited vertices.
      partialCycle - the partial cycle
      partialCycleSourceVertex - the source vertex of the first edge in the partial cycle
    • buildWalk

      protected GraphWalk<V,E> buildWalk()
      Build final walk
      the final walk
    • addEdge

      protected void addEdge(HierholzerEulerianCycle<V,E>.VertexNode sNode, HierholzerEulerianCycle<V,E>.VertexNode tNode, E e)
      Add an edge to the index.
      sNode - source vertex
      tNode - target vertex
      e - original (wrapped) edge
    • unlink

      protected void unlink(HierholzerEulerianCycle<V,E>.VertexNode vNode)
      Unlink a vertex from the vertex list.
      vNode - vertex to unlink
    • moveToFront

      protected void moveToFront(HierholzerEulerianCycle<V,E>.VertexNode vNode)
      Move a vertex as first in the vertex list.
      vNode - vertex to move to front
    • unlink

      protected void unlink(HierholzerEulerianCycle<V,E>.EdgeNode eNode)
      Unlink an edge from the adjacency lists of its end-points.
      eNode - edge to unlink
    • getOppositeVertex

      Compute the opposite end-point of an end-point of an edge.
      v - vertex that is part of edge
      e - edge used to find opposite vertex
      opposite vertex in edge