Skip to content

Commit 8fbf71f

Browse files
committed
Added Hierholzer Algorith to find Eulerian Path
1 parent 85ce312 commit 8fbf71f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/main/java/com/thealgorithms/graph/HierholzerEulerianPath.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private List<Integer> rotateEulerianCircuitIfNeeded(List<Integer> path, int[] ou
227227

228228
if (preferredStart != -1 && path.get(0) != preferredStart) {
229229
int idx = 0;
230-
for (int node : path) {
230+
for (Integer node : path) { // replaced indexed loop
231231
if (node == preferredStart) {
232232
break;
233233
}
@@ -236,11 +236,19 @@ private List<Integer> rotateEulerianCircuitIfNeeded(List<Integer> path, int[] ou
236236

237237
if (idx > 0) {
238238
List<Integer> rotated = new ArrayList<>();
239-
for (int i = idx; i < path.size(); i++) {
240-
rotated.add(path.get(i));
239+
int currentIndex = 0;
240+
for (Integer node : path) { // replaced indexed loop
241+
if (currentIndex >= idx) {
242+
rotated.add(node);
243+
}
244+
currentIndex++;
241245
}
242-
for (int i = 0; i < idx; i++) {
243-
rotated.add(path.get(i));
246+
currentIndex = 0;
247+
for (Integer node : path) { // replaced indexed loop
248+
if (currentIndex < idx) {
249+
rotated.add(node);
250+
}
251+
currentIndex++;
244252
}
245253
path = rotated;
246254
}

0 commit comments

Comments
 (0)