Skip to content

Commit 202f32a

Browse files
Merge pull request #4 from eugenp/master
Sync
2 parents 8574d99 + c1933c6 commit 202f32a

435 files changed

Lines changed: 12540 additions & 2160 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
- [Check If a Number Is Prime in Java](http://www.baeldung.com/java-prime-numbers)
99
- [Example of Hill Climbing Algorithm](http://www.baeldung.com/java-hill-climbing-algorithm)
1010
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](http://www.baeldung.com/java-monte-carlo-tree-search)
11+
- [String Search Algorithms for Large Texts](http://www.baeldung.com/java-full-text-search-algorithms)

algorithms/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<artifactId>jenetics</artifactId>
3535
<version>3.7.0</version>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.jgrapht</groupId>
39+
<artifactId>jgrapht-core</artifactId>
40+
<version>1.0.1</version>
41+
</dependency>
3742
</dependencies>
3843

3944
<build>

algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionBruteForce.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
public class CycleDetectionBruteForce {
44

5-
public static <T> boolean detectCycle(Node<T> head) {
5+
public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
66
if (head == null) {
7-
return false;
7+
return new CycleDetectionResult<>(false, null);
88
}
99

1010
Node<T> it1 = head;
@@ -25,14 +25,14 @@ public static <T> boolean detectCycle(Node<T> head) {
2525
}
2626

2727
if (noOfTimesCurrentNodeVisited == 2) {
28-
return true;
28+
return new CycleDetectionResult<>(true, it1);
2929
}
3030

3131
x--;
3232
}
3333
}
3434

35-
return false;
35+
return new CycleDetectionResult<>(false, null);
3636
}
3737

3838
}

algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByFastAndSlowIterators.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
public class CycleDetectionByFastAndSlowIterators {
44

5-
public static <T> boolean detectCycle(Node<T> head) {
5+
public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
66
if (head == null) {
7-
return false;
7+
return new CycleDetectionResult<>(false, null);
88
}
99

1010
Node<T> slow = head;
@@ -15,11 +15,11 @@ public static <T> boolean detectCycle(Node<T> head) {
1515
fast = fast.next.next;
1616

1717
if (slow == fast) {
18-
return true;
18+
return new CycleDetectionResult<>(true, fast);
1919
}
2020
}
2121

22-
return false;
22+
return new CycleDetectionResult<>(false, null);
2323
}
2424

2525
}

algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleDetectionByHashing.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55

66
public class CycleDetectionByHashing {
77

8-
public static <T> boolean detectCycle(Node<T> head) {
8+
public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
99
if (head == null) {
10-
return false;
10+
return new CycleDetectionResult<>(false, null);
1111
}
1212

1313
Set<Node<T>> set = new HashSet<>();
1414
Node<T> node = head;
1515

1616
while (node != null) {
1717
if (set.contains(node)) {
18-
return true;
18+
return new CycleDetectionResult<>(true, node);
1919
}
2020
set.add(node);
2121
node = node.next;
2222
}
2323

24-
return false;
24+
return new CycleDetectionResult<>(false, null);
2525
}
2626

2727
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.algorithms.linkedlist;
2+
3+
public class CycleDetectionResult<T> {
4+
boolean cycleExists;
5+
Node<T> node;
6+
7+
public CycleDetectionResult(boolean cycleExists, Node<T> node) {
8+
super();
9+
this.cycleExists = cycleExists;
10+
this.node = node;
11+
}
12+
}

algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalBruteForce.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,21 @@
33
public class CycleRemovalBruteForce {
44

55
public static <T> boolean detectAndRemoveCycle(Node<T> head) {
6-
if (head == null) {
7-
return false;
8-
}
9-
10-
Node<T> slow = head;
11-
Node<T> fast = head;
6+
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
127

13-
while (fast != null && fast.next != null) {
14-
slow = slow.next;
15-
fast = fast.next.next;
16-
17-
if (slow == fast) {
18-
removeCycle(slow, head);
19-
return true;
20-
}
8+
if (result.cycleExists) {
9+
removeCycle(result.node, head);
2110
}
2211

23-
return false;
12+
return result.cycleExists;
2413
}
2514

15+
/**
16+
* @param loopNodeParam - reference to the node where Flyods cycle
17+
* finding algorithm ends, i.e. the fast and the slow iterators
18+
* meet.
19+
* @param head - reference to the head of the list
20+
*/
2621
private static <T> void removeCycle(Node<T> loopNodeParam, Node<T> head) {
2722
Node<T> it = head;
2823

@@ -39,12 +34,12 @@ private static <T> void removeCycle(Node<T> loopNodeParam, Node<T> head) {
3934
private static <T> boolean isNodeReachableFromLoopNode(Node<T> it, Node<T> loopNodeParam) {
4035
Node<T> loopNode = loopNodeParam;
4136

42-
while (loopNode.next != loopNodeParam) {
37+
do {
4338
if (it == loopNode) {
4439
return true;
4540
}
4641
loopNode = loopNode.next;
47-
}
42+
} while (loopNode.next != loopNodeParam);
4843

4944
return false;
5045
}

algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalByCountingLoopNodes.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,17 @@
33
public class CycleRemovalByCountingLoopNodes {
44

55
public static <T> boolean detectAndRemoveCycle(Node<T> head) {
6-
if (head == null) {
7-
return false;
8-
}
9-
10-
Node<T> slow = head;
11-
Node<T> fast = head;
12-
13-
while (fast != null && fast.next != null) {
14-
slow = slow.next;
15-
fast = fast.next.next;
6+
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
167

17-
if (slow == fast) {
18-
int cycleLength = calculateCycleLength(slow);
19-
removeCycle(head, cycleLength);
20-
return true;
21-
}
8+
if (result.cycleExists) {
9+
removeCycle(result.node, head);
2210
}
2311

24-
return false;
12+
return result.cycleExists;
2513
}
2614

27-
private static <T> void removeCycle(Node<T> head, int cycleLength) {
15+
private static <T> void removeCycle(Node<T> loopNodeParam, Node<T> head) {
16+
int cycleLength = calculateCycleLength(loopNodeParam);
2817
Node<T> cycleLengthAdvancedIterator = head;
2918
Node<T> it = head;
3019

algorithms/src/main/java/com/baeldung/algorithms/linkedlist/CycleRemovalWithoutCountingLoopNodes.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,13 @@
33
public class CycleRemovalWithoutCountingLoopNodes {
44

55
public static <T> boolean detectAndRemoveCycle(Node<T> head) {
6-
if (head == null) {
7-
return false;
8-
}
9-
10-
Node<T> slow = head;
11-
Node<T> fast = head;
12-
13-
while (fast != null && fast.next != null) {
14-
slow = slow.next;
15-
fast = fast.next.next;
6+
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
167

17-
if (slow == fast) {
18-
removeCycle(slow, head);
19-
return true;
20-
}
8+
if (result.cycleExists) {
9+
removeCycle(result.node, head);
2110
}
2211

23-
return false;
12+
return result.cycleExists;
2413
}
2514

2615
private static <T> void removeCycle(Node<T> meetingPointParam, Node<T> head) {

algorithms/src/test/java/com/baeldung/algorithms/linkedlist/CycleDetectionBruteForceTest.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
import org.junit.Assert;
44
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.Parameterized;
57

8+
@RunWith(value = Parameterized.class)
69
public class CycleDetectionBruteForceTest extends CycleDetectionTestBase {
10+
boolean cycleExists;
11+
Node<Integer> head;
712

8-
@Test
9-
public void givenNormalList_dontDetectLoop() {
10-
Node<Integer> root = createList();
11-
Assert.assertFalse(CycleDetectionBruteForce.detectCycle(root));
13+
public CycleDetectionBruteForceTest(Node<Integer> head, boolean cycleExists) {
14+
super();
15+
this.cycleExists = cycleExists;
16+
this.head = head;
1217
}
1318

1419
@Test
15-
public void givenCyclicList_detectLoop() {
16-
Node<Integer> root = createList();
17-
createLoop(root);
18-
Assert.assertTrue(CycleDetectionBruteForce.detectCycle(root));
20+
public void givenList_detectLoop() {
21+
Assert.assertEquals(cycleExists, CycleDetectionBruteForce.detectCycle(head).cycleExists);
1922
}
2023
}

0 commit comments

Comments
 (0)