|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.LinkedList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.Stack; |
| 11 | + |
| 12 | +/** |
| 13 | + * Implementation of Hierholzer's algorithm to find an Eulerian Circuit in an undirected graph. |
| 14 | + * <p> |
| 15 | + * An Eulerian circuit is a trail in a graph that visits every edge exactly once, |
| 16 | + * starting and ending at the same vertex. This algorithm finds such a circuit if one exists. |
| 17 | + * </p> |
| 18 | + * <p> |
| 19 | + * This implementation is designed for an <strong>undirected graph</strong>. For a valid Eulerian |
| 20 | + * circuit to exist, the graph must satisfy two conditions: |
| 21 | + * <ol> |
| 22 | + * <li>All vertices with a non-zero degree must be part of a single connected component.</li> |
| 23 | + * <li>Every vertex must have an even degree (an even number of edges connected to it).</li> |
| 24 | + * </ol> |
| 25 | + * </p> |
| 26 | + * <p> |
| 27 | + * The algorithm runs in O(E + V) time, where E is the number of edges and V is the number of vertices. |
| 28 | + * The graph is represented by a Map where keys are vertices and values are a LinkedList of adjacent vertices. |
| 29 | + * </p> |
| 30 | + * |
| 31 | + * @see <a href="https://en.wikipedia.org/wiki/Eulerian_path#Hierholzer's_algorithm">Wikipedia: Hierholzer's algorithm</a> |
| 32 | + */ |
| 33 | +public final class HierholzerAlgorithm { |
| 34 | + |
| 35 | + private final Map<Integer, LinkedList<Integer>> graph; |
| 36 | + |
| 37 | + public HierholzerAlgorithm(Map<Integer, LinkedList<Integer>> graph) { |
| 38 | + this.graph = (graph == null) ? new HashMap<>() : graph; |
| 39 | + } |
| 40 | + |
| 41 | + public boolean hasEulerianCircuit() { |
| 42 | + if (graph.isEmpty()) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + for (List<Integer> neighbors : graph.values()) { |
| 47 | + if (neighbors.size() % 2 != 0) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return isCoherentlyConnected(); |
| 53 | + } |
| 54 | + |
| 55 | + public List<Integer> findEulerianCircuit() { |
| 56 | + if (!hasEulerianCircuit()) { |
| 57 | + return Collections.emptyList(); |
| 58 | + } |
| 59 | + |
| 60 | + Map<Integer, LinkedList<Integer>> tempGraph = new HashMap<>(); |
| 61 | + for (Map.Entry<Integer, LinkedList<Integer>> entry : graph.entrySet()) { |
| 62 | + tempGraph.put(entry.getKey(), new LinkedList<>(entry.getValue())); |
| 63 | + } |
| 64 | + |
| 65 | + Stack<Integer> currentPath = new Stack<>(); |
| 66 | + LinkedList<Integer> circuit = new LinkedList<>(); |
| 67 | + |
| 68 | + int startVertex = -1; |
| 69 | + for (Map.Entry<Integer, LinkedList<Integer>> entry : tempGraph.entrySet()) { |
| 70 | + if (!entry.getValue().isEmpty()) { |
| 71 | + startVertex = entry.getKey(); |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (startVertex == -1) { |
| 77 | + if (graph.isEmpty()) { |
| 78 | + return Collections.emptyList(); |
| 79 | + } |
| 80 | + return Collections.singletonList(graph.keySet().iterator().next()); |
| 81 | + } |
| 82 | + |
| 83 | + currentPath.push(startVertex); |
| 84 | + |
| 85 | + while (!currentPath.isEmpty()) { |
| 86 | + int currentVertex = currentPath.peek(); |
| 87 | + |
| 88 | + if (tempGraph.containsKey(currentVertex) && !tempGraph.get(currentVertex).isEmpty()) { |
| 89 | + int nextVertex = tempGraph.get(currentVertex).pollFirst(); |
| 90 | + tempGraph.get(nextVertex).remove(Integer.valueOf(currentVertex)); |
| 91 | + currentPath.push(nextVertex); |
| 92 | + } else { |
| 93 | + circuit.addFirst(currentVertex); |
| 94 | + currentPath.pop(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return circuit; |
| 99 | + } |
| 100 | + |
| 101 | + private boolean isCoherentlyConnected() { |
| 102 | + if (graph.isEmpty()) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + Set<Integer> visited = new HashSet<>(); |
| 107 | + int startNode = -1; |
| 108 | + |
| 109 | + for (Map.Entry<Integer, LinkedList<Integer>> entry : graph.entrySet()) { |
| 110 | + if (!entry.getValue().isEmpty()) { |
| 111 | + startNode = entry.getKey(); |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (startNode == -1) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + dfs(startNode, visited); |
| 121 | + |
| 122 | + for (Map.Entry<Integer, LinkedList<Integer>> entry : graph.entrySet()) { |
| 123 | + if (!entry.getValue().isEmpty() && !visited.contains(entry.getKey())) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + } |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + private void dfs(int u, Set<Integer> visited) { |
| 131 | + visited.add(u); |
| 132 | + if (graph.containsKey(u)) { |
| 133 | + for (int v : graph.get(u)) { |
| 134 | + if (!visited.contains(v)) { |
| 135 | + dfs(v, visited); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments