-
Notifications
You must be signed in to change notification settings - Fork 20.8k
feat: Add Hierholzer's Algorithm for Eulerian Circuits #6726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DenizAltunkapan
merged 11 commits into
TheAlgorithms:master
from
sairamsharan:feat-hierholzer-algorithm
Nov 2, 2025
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d3b0e3b
feat: Add Hierholzer's Algorithm for Eulerian Circuits
sairamsharan 568913c
fix: Add more test cases to improve code coverage
sairamsharan 456e0cf
feat: Add Hierholzer's Algorithm for Eulerian Circuits
sairamsharan 96bd4fc
fix: Apply clang-format after merge
sairamsharan 0fac806
fix: Apply all formatting, style, and efficiency fixes
sairamsharan 7647a8d
Merge branch 'master' into feat-hierholzer-algorithm
sairamsharan accbbfb
Merge branch 'master' into feat-hierholzer-algorithm
sairamsharan 1de236e
docs: Apply feedback and improve Javadoc
sairamsharan ef458a2
docs: Add Hierholzer's Algorithm to DIRECTORY.md
sairamsharan a62a7c4
Merge branch 'master' into feat-hierholzer-algorithm
sairamsharan 78d950c
Merge branch 'master' into feat-hierholzer-algorithm
DenizAltunkapan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
src/main/java/com/thealgorithms/graph/HierholzerAlgorithm.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package com.thealgorithms.graph; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.Stack; | ||
|
|
||
| public class HierholzerAlgorithm { | ||
|
|
||
| private final Map<Integer, LinkedList<Integer>> graph; | ||
|
|
||
| public HierholzerAlgorithm(Map<Integer, LinkedList<Integer>> graph) { | ||
DenizAltunkapan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.graph = (graph == null) ? new HashMap<>() : graph; | ||
| } | ||
|
|
||
| public boolean hasEulerianCircuit() { | ||
| if (graph.isEmpty()) { | ||
| return true; | ||
| } | ||
|
|
||
| // FINAL FIX: Loop over values directly for efficiency. | ||
| for (List<Integer> neighbors : graph.values()) { | ||
| if (neighbors.size() % 2 != 0) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (!isCoherentlyConnected()) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public List<Integer> findEulerianCircuit() { | ||
| if (!hasEulerianCircuit()) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| Map<Integer, LinkedList<Integer>> tempGraph = new HashMap<>(); | ||
| for (Map.Entry<Integer, LinkedList<Integer>> entry : graph.entrySet()) { | ||
| tempGraph.put(entry.getKey(), new LinkedList<>(entry.getValue())); | ||
| } | ||
|
|
||
| Stack<Integer> currentPath = new Stack<>(); | ||
| List<Integer> circuit = new LinkedList<>(); | ||
|
|
||
| int startVertex = -1; | ||
| // FINAL FIX: Use entrySet for efficiency. | ||
DenizAltunkapan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (Map.Entry<Integer, LinkedList<Integer>> entry : tempGraph.entrySet()) { | ||
| if (!entry.getValue().isEmpty()) { | ||
| startVertex = entry.getKey(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (startVertex == -1) { | ||
| if (graph.isEmpty()) { | ||
| return Collections.emptyList(); | ||
| } | ||
| return Collections.singletonList(graph.keySet().iterator().next()); | ||
| } | ||
|
|
||
| currentPath.push(startVertex); | ||
|
|
||
| while (!currentPath.isEmpty()) { | ||
| int currentVertex = currentPath.peek(); | ||
|
|
||
| if (tempGraph.containsKey(currentVertex) && !tempGraph.get(currentVertex).isEmpty()) { | ||
| int nextVertex = tempGraph.get(currentVertex).pollFirst(); | ||
| tempGraph.get(nextVertex).remove(Integer.valueOf(currentVertex)); | ||
| currentPath.push(nextVertex); | ||
| } else { | ||
| circuit.add(0, currentPath.pop()); | ||
DenizAltunkapan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return circuit; | ||
| } | ||
|
|
||
| private boolean isCoherentlyConnected() { | ||
| if (graph.isEmpty()) { | ||
| return true; | ||
| } | ||
|
|
||
| Set<Integer> visited = new HashSet<>(); | ||
| int startNode = -1; | ||
|
|
||
| // FINAL FIX: Use entrySet for efficiency. | ||
| for (Map.Entry<Integer, LinkedList<Integer>> entry : graph.entrySet()) { | ||
| if (!entry.getValue().isEmpty()) { | ||
| startNode = entry.getKey(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (startNode == -1) { | ||
| return true; | ||
| } | ||
|
|
||
| dfs(startNode, visited); | ||
|
|
||
| // FINAL FIX: Use entrySet for efficiency. | ||
DenizAltunkapan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (Map.Entry<Integer, LinkedList<Integer>> entry : graph.entrySet()) { | ||
| if (!entry.getValue().isEmpty() && !visited.contains(entry.getKey())) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private void dfs(int u, Set<Integer> visited) { | ||
| visited.add(u); | ||
| if (graph.containsKey(u)) { | ||
| for (int v : graph.get(u)) { | ||
| if (!visited.contains(v)) { | ||
| dfs(v, visited); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
src/test/java/com/thealgorithms/graph/HierholzerAlgorithmTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package com.thealgorithms.graph; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class HierholzerAlgorithmTest { | ||
|
|
||
| @Test | ||
| public void testFindsEulerianCircuitInSimpleTriangleGraph() { | ||
| Map<Integer, LinkedList<Integer>> graph = new HashMap<>(); | ||
| graph.put(0, new LinkedList<>(Arrays.asList(1, 2))); | ||
| graph.put(1, new LinkedList<>(Arrays.asList(0, 2))); | ||
| graph.put(2, new LinkedList<>(Arrays.asList(0, 1))); | ||
| HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph); | ||
| assertTrue(algorithm.hasEulerianCircuit()); | ||
| List<Integer> circuit = algorithm.findEulerianCircuit(); | ||
| assertEquals(4, circuit.size()); | ||
| assertEquals(circuit.get(0), circuit.get(circuit.size() - 1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFailsForGraphWithOddDegreeVertices() { | ||
| Map<Integer, LinkedList<Integer>> graph = new HashMap<>(); | ||
| graph.put(0, new LinkedList<>(Collections.singletonList(1))); | ||
| graph.put(1, new LinkedList<>(Collections.singletonList(0))); | ||
| HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph); | ||
| assertFalse(algorithm.hasEulerianCircuit()); | ||
| assertTrue(algorithm.findEulerianCircuit().isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFailsForDisconnectedGraph() { | ||
| Map<Integer, LinkedList<Integer>> graph = new HashMap<>(); | ||
| graph.put(0, new LinkedList<>(Arrays.asList(1, 2))); | ||
| graph.put(1, new LinkedList<>(Arrays.asList(0, 2))); | ||
| graph.put(2, new LinkedList<>(Arrays.asList(0, 1))); | ||
| graph.put(3, new LinkedList<>(Arrays.asList(4, 5))); | ||
| graph.put(4, new LinkedList<>(Arrays.asList(3, 5))); | ||
| graph.put(5, new LinkedList<>(Arrays.asList(3, 4))); | ||
| HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph); | ||
| assertFalse(algorithm.hasEulerianCircuit()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHandlesEmptyGraph() { | ||
| Map<Integer, LinkedList<Integer>> graph = new HashMap<>(); | ||
| HierholzerAlgorithm algorithm = new HierholzerAlgorithm(graph); | ||
| assertTrue(algorithm.hasEulerianCircuit()); | ||
| assertTrue(algorithm.findEulerianCircuit().isEmpty()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.