Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -41,3 +41,5 @@ SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties
|
|||||||
spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
|
spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
|
||||||
|
|
||||||
spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
|
spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
|
||||||
|
*.springBeans
|
||||||
|
|
||||||
|
|||||||
@@ -23,4 +23,4 @@ Any IDE can be used to work with the projects, but if you're using Eclipse, cons
|
|||||||
|
|
||||||
CI - Jenkins
|
CI - Jenkins
|
||||||
================================
|
================================
|
||||||
This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/tutorials/)**
|
This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/github%20projects%20Jobs/job/tutorials/)**
|
||||||
|
|||||||
@@ -8,3 +8,4 @@
|
|||||||
- [Check If a Number Is Prime in Java](http://www.baeldung.com/java-prime-numbers)
|
- [Check If a Number Is Prime in Java](http://www.baeldung.com/java-prime-numbers)
|
||||||
- [Example of Hill Climbing Algorithm](http://www.baeldung.com/java-hill-climbing-algorithm)
|
- [Example of Hill Climbing Algorithm](http://www.baeldung.com/java-hill-climbing-algorithm)
|
||||||
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](http://www.baeldung.com/java-monte-carlo-tree-search)
|
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](http://www.baeldung.com/java-monte-carlo-tree-search)
|
||||||
|
- [String Search Algorithms for Large Texts](http://www.baeldung.com/java-full-text-search-algorithms)
|
||||||
|
|||||||
@@ -34,6 +34,11 @@
|
|||||||
<artifactId>jenetics</artifactId>
|
<artifactId>jenetics</artifactId>
|
||||||
<version>3.7.0</version>
|
<version>3.7.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jgrapht</groupId>
|
||||||
|
<artifactId>jgrapht-core</artifactId>
|
||||||
|
<version>1.0.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package com.baeldung.algorithms.binarysearch;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BinarySearch {
|
||||||
|
|
||||||
|
public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
|
||||||
|
|
||||||
|
int index = Integer.MAX_VALUE;
|
||||||
|
|
||||||
|
while (low <= high) {
|
||||||
|
|
||||||
|
int mid = (low + high) / 2;
|
||||||
|
|
||||||
|
if (sortedArray[mid] < key) {
|
||||||
|
low = mid + 1;
|
||||||
|
} else if (sortedArray[mid] > key) {
|
||||||
|
high = mid - 1;
|
||||||
|
} else if (sortedArray[mid] == key) {
|
||||||
|
index = mid;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
|
||||||
|
|
||||||
|
int middle = (low + high) / 2;
|
||||||
|
if (high < low) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key == sortedArray[middle]) {
|
||||||
|
return middle;
|
||||||
|
} else if (key < sortedArray[middle]) {
|
||||||
|
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
|
||||||
|
} else {
|
||||||
|
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
|
||||||
|
int index = Arrays.binarySearch(sortedArray, key);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
|
||||||
|
int index = Collections.binarySearch(sortedList, key);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
public class CycleDetectionBruteForce {
|
||||||
|
|
||||||
|
public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
|
||||||
|
if (head == null) {
|
||||||
|
return new CycleDetectionResult<>(false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node<T> it1 = head;
|
||||||
|
int nodesTraversedByOuter = 0;
|
||||||
|
while (it1 != null && it1.next != null) {
|
||||||
|
it1 = it1.next;
|
||||||
|
nodesTraversedByOuter++;
|
||||||
|
|
||||||
|
int x = nodesTraversedByOuter;
|
||||||
|
Node<T> it2 = head;
|
||||||
|
int noOfTimesCurrentNodeVisited = 0;
|
||||||
|
|
||||||
|
while (x > 0) {
|
||||||
|
it2 = it2.next;
|
||||||
|
|
||||||
|
if (it2 == it1) {
|
||||||
|
noOfTimesCurrentNodeVisited++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (noOfTimesCurrentNodeVisited == 2) {
|
||||||
|
return new CycleDetectionResult<>(true, it1);
|
||||||
|
}
|
||||||
|
|
||||||
|
x--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CycleDetectionResult<>(false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
public class CycleDetectionByFastAndSlowIterators {
|
||||||
|
|
||||||
|
public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
|
||||||
|
if (head == null) {
|
||||||
|
return new CycleDetectionResult<>(false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node<T> slow = head;
|
||||||
|
Node<T> fast = head;
|
||||||
|
|
||||||
|
while (fast != null && fast.next != null) {
|
||||||
|
slow = slow.next;
|
||||||
|
fast = fast.next.next;
|
||||||
|
|
||||||
|
if (slow == fast) {
|
||||||
|
return new CycleDetectionResult<>(true, fast);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CycleDetectionResult<>(false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class CycleDetectionByHashing {
|
||||||
|
|
||||||
|
public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
|
||||||
|
if (head == null) {
|
||||||
|
return new CycleDetectionResult<>(false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Node<T>> set = new HashSet<>();
|
||||||
|
Node<T> node = head;
|
||||||
|
|
||||||
|
while (node != null) {
|
||||||
|
if (set.contains(node)) {
|
||||||
|
return new CycleDetectionResult<>(true, node);
|
||||||
|
}
|
||||||
|
set.add(node);
|
||||||
|
node = node.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CycleDetectionResult<>(false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
public class CycleDetectionResult<T> {
|
||||||
|
boolean cycleExists;
|
||||||
|
Node<T> node;
|
||||||
|
|
||||||
|
public CycleDetectionResult(boolean cycleExists, Node<T> node) {
|
||||||
|
super();
|
||||||
|
this.cycleExists = cycleExists;
|
||||||
|
this.node = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
public class CycleRemovalBruteForce {
|
||||||
|
|
||||||
|
public static <T> boolean detectAndRemoveCycle(Node<T> head) {
|
||||||
|
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
|
||||||
|
|
||||||
|
if (result.cycleExists) {
|
||||||
|
removeCycle(result.node, head);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.cycleExists;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param loopNodeParam - reference to the node where Flyods cycle
|
||||||
|
* finding algorithm ends, i.e. the fast and the slow iterators
|
||||||
|
* meet.
|
||||||
|
* @param head - reference to the head of the list
|
||||||
|
*/
|
||||||
|
private static <T> void removeCycle(Node<T> loopNodeParam, Node<T> head) {
|
||||||
|
Node<T> it = head;
|
||||||
|
|
||||||
|
while (it != null) {
|
||||||
|
if (isNodeReachableFromLoopNode(it, loopNodeParam)) {
|
||||||
|
Node<T> loopStart = it;
|
||||||
|
findEndNodeAndBreakCycle(loopStart);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
it = it.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> boolean isNodeReachableFromLoopNode(Node<T> it, Node<T> loopNodeParam) {
|
||||||
|
Node<T> loopNode = loopNodeParam;
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (it == loopNode) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
loopNode = loopNode.next;
|
||||||
|
} while (loopNode.next != loopNodeParam);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void findEndNodeAndBreakCycle(Node<T> loopStartParam) {
|
||||||
|
Node<T> loopStart = loopStartParam;
|
||||||
|
|
||||||
|
while (loopStart.next != loopStartParam) {
|
||||||
|
loopStart = loopStart.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
loopStart.next = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
+44
@@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
public class CycleRemovalByCountingLoopNodes {
|
||||||
|
|
||||||
|
public static <T> boolean detectAndRemoveCycle(Node<T> head) {
|
||||||
|
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
|
||||||
|
|
||||||
|
if (result.cycleExists) {
|
||||||
|
removeCycle(result.node, head);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.cycleExists;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void removeCycle(Node<T> loopNodeParam, Node<T> head) {
|
||||||
|
int cycleLength = calculateCycleLength(loopNodeParam);
|
||||||
|
Node<T> cycleLengthAdvancedIterator = head;
|
||||||
|
Node<T> it = head;
|
||||||
|
|
||||||
|
for (int i = 0; i < cycleLength; i++) {
|
||||||
|
cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (it.next != cycleLengthAdvancedIterator.next) {
|
||||||
|
it = it.next;
|
||||||
|
cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
cycleLengthAdvancedIterator.next = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> int calculateCycleLength(Node<T> loopNodeParam) {
|
||||||
|
Node<T> loopNode = loopNodeParam;
|
||||||
|
int length = 1;
|
||||||
|
|
||||||
|
while (loopNode.next != loopNodeParam) {
|
||||||
|
length++;
|
||||||
|
loopNode = loopNode.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
public class CycleRemovalWithoutCountingLoopNodes {
|
||||||
|
|
||||||
|
public static <T> boolean detectAndRemoveCycle(Node<T> head) {
|
||||||
|
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);
|
||||||
|
|
||||||
|
if (result.cycleExists) {
|
||||||
|
removeCycle(result.node, head);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.cycleExists;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void removeCycle(Node<T> meetingPointParam, Node<T> head) {
|
||||||
|
Node<T> loopNode = meetingPointParam;
|
||||||
|
Node<T> it = head;
|
||||||
|
|
||||||
|
while (loopNode.next != it.next) {
|
||||||
|
it = it.next;
|
||||||
|
loopNode = loopNode.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
loopNode.next = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
public class Node<T> {
|
||||||
|
T data;
|
||||||
|
Node<T> next;
|
||||||
|
|
||||||
|
public static <T> Node<T> createNewNode(T data, Node<T> next) {
|
||||||
|
Node<T> node = new Node<T>();
|
||||||
|
node.data = data;
|
||||||
|
node.next = next;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> void traverseList(Node<T> root) {
|
||||||
|
if (root == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node<T> node = root;
|
||||||
|
while (node != null) {
|
||||||
|
System.out.println(node.data);
|
||||||
|
node = node.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Node<T> getTail(Node<T> root) {
|
||||||
|
if (root == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node<T> node = root;
|
||||||
|
while (node.next != null) {
|
||||||
|
node = node.next;
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Executable
+194
@@ -0,0 +1,194 @@
|
|||||||
|
package com.baeldung.algorithms.string.search;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class StringSearchAlgorithms {
|
||||||
|
public static long getBiggerPrime(int m) {
|
||||||
|
BigInteger prime = BigInteger.probablePrime(getNumberOfBits(m) + 1, new Random());
|
||||||
|
return prime.longValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long getLowerPrime(long number) {
|
||||||
|
BigInteger prime = BigInteger.probablePrime(getNumberOfBits(number) - 1, new Random());
|
||||||
|
return prime.longValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getNumberOfBits(final int number) {
|
||||||
|
return Integer.SIZE - Integer.numberOfLeadingZeros(number);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getNumberOfBits(final long number) {
|
||||||
|
return Long.SIZE - Long.numberOfLeadingZeros(number);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int simpleTextSearch(char[] pattern, char[] text) {
|
||||||
|
int patternSize = pattern.length;
|
||||||
|
int textSize = text.length;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while ((i + patternSize) <= textSize) {
|
||||||
|
int j = 0;
|
||||||
|
while (text[i + j] == pattern[j]) {
|
||||||
|
j += 1;
|
||||||
|
if (j >= patternSize)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int RabinKarpMethod(char[] pattern, char[] text) {
|
||||||
|
int patternSize = pattern.length; // m
|
||||||
|
int textSize = text.length; // n
|
||||||
|
|
||||||
|
long prime = getBiggerPrime(patternSize);
|
||||||
|
|
||||||
|
long r = 1;
|
||||||
|
for (int i = 0; i < patternSize - 1; i++) {
|
||||||
|
r *= 2;
|
||||||
|
r = r % prime;
|
||||||
|
}
|
||||||
|
|
||||||
|
long[] t = new long[textSize];
|
||||||
|
t[0] = 0;
|
||||||
|
|
||||||
|
long pfinger = 0;
|
||||||
|
|
||||||
|
for (int j = 0; j < patternSize; j++) {
|
||||||
|
t[0] = (2 * t[0] + text[j]) % prime;
|
||||||
|
pfinger = (2 * pfinger + pattern[j]) % prime;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
boolean passed = false;
|
||||||
|
|
||||||
|
int diff = textSize - patternSize;
|
||||||
|
for (i = 0; i <= diff; i++) {
|
||||||
|
if (t[i] == pfinger) {
|
||||||
|
passed = true;
|
||||||
|
for (int k = 0; k < patternSize; k++) {
|
||||||
|
if (text[i + k] != pattern[k]) {
|
||||||
|
passed = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (passed) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i < diff) {
|
||||||
|
long value = 2 * (t[i] - r * text[i]) + text[i + patternSize];
|
||||||
|
t[i + 1] = ((value % prime) + prime) % prime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int KnuthMorrisPrattSearch(char[] pattern, char[] text) {
|
||||||
|
int patternSize = pattern.length; // m
|
||||||
|
int textSize = text.length; // n
|
||||||
|
|
||||||
|
int i = 0, j = 0;
|
||||||
|
|
||||||
|
int[] shift = KnuthMorrisPrattShift(pattern);
|
||||||
|
|
||||||
|
while ((i + patternSize) <= textSize) {
|
||||||
|
while (text[i + j] == pattern[j]) {
|
||||||
|
j += 1;
|
||||||
|
if (j >= patternSize)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j > 0) {
|
||||||
|
i += shift[j - 1];
|
||||||
|
j = Math.max(j - shift[j - 1], 0);
|
||||||
|
} else {
|
||||||
|
i++;
|
||||||
|
j = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] KnuthMorrisPrattShift(char[] pattern) {
|
||||||
|
int patternSize = pattern.length;
|
||||||
|
|
||||||
|
int[] shift = new int[patternSize];
|
||||||
|
shift[0] = 1;
|
||||||
|
|
||||||
|
int i = 1, j = 0;
|
||||||
|
|
||||||
|
while ((i + j) < patternSize) {
|
||||||
|
if (pattern[i + j] == pattern[j]) {
|
||||||
|
shift[i + j] = i;
|
||||||
|
j++;
|
||||||
|
} else {
|
||||||
|
if (j == 0)
|
||||||
|
shift[i] = i + 1;
|
||||||
|
|
||||||
|
if (j > 0) {
|
||||||
|
i = i + shift[j - 1];
|
||||||
|
j = Math.max(j - shift[j - 1], 0);
|
||||||
|
} else {
|
||||||
|
i = i + 1;
|
||||||
|
j = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return shift;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int BoyerMooreHorspoolSimpleSearch(char[] pattern, char[] text) {
|
||||||
|
int patternSize = pattern.length;
|
||||||
|
int textSize = text.length;
|
||||||
|
|
||||||
|
int i = 0, j = 0;
|
||||||
|
|
||||||
|
while ((i + patternSize) <= textSize) {
|
||||||
|
j = patternSize - 1;
|
||||||
|
while (text[i + j] == pattern[j]) {
|
||||||
|
j--;
|
||||||
|
if (j < 0)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int BoyerMooreHorspoolSearch(char[] pattern, char[] text) {
|
||||||
|
|
||||||
|
int shift[] = new int[256];
|
||||||
|
|
||||||
|
for (int k = 0; k < 256; k++) {
|
||||||
|
shift[k] = pattern.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int k = 0; k < pattern.length - 1; k++) {
|
||||||
|
shift[pattern[k]] = pattern.length - 1 - k;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0, j = 0;
|
||||||
|
|
||||||
|
while ((i + pattern.length) <= text.length) {
|
||||||
|
j = pattern.length - 1;
|
||||||
|
|
||||||
|
while (text[i + j] == pattern[j]) {
|
||||||
|
j -= 1;
|
||||||
|
if (j < 0)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
i = i + shift[text[i + pattern.length - 1]];
|
||||||
|
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package algorithms;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.algorithms.string.search.StringSearchAlgorithms;
|
||||||
|
|
||||||
|
public class StringSearchAlgorithmsTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStringSearchAlgorithms(){
|
||||||
|
String text = "This is some nice text.";
|
||||||
|
String pattern = "some";
|
||||||
|
|
||||||
|
int realPosition = text.indexOf(pattern);
|
||||||
|
Assert.assertTrue(realPosition == StringSearchAlgorithms.simpleTextSearch(pattern.toCharArray(), text.toCharArray()));
|
||||||
|
Assert.assertTrue(realPosition == StringSearchAlgorithms.RabinKarpMethod(pattern.toCharArray(), text.toCharArray()));
|
||||||
|
Assert.assertTrue(realPosition == StringSearchAlgorithms.KnuthMorrisPrattSearch(pattern.toCharArray(), text.toCharArray()));
|
||||||
|
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSimpleSearch(pattern.toCharArray(), text.toCharArray()));
|
||||||
|
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSearch(pattern.toCharArray(), text.toCharArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package algorithms.binarysearch;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import com.baeldung.algorithms.binarysearch.BinarySearch;
|
||||||
|
|
||||||
|
public class BinarySearchTest {
|
||||||
|
|
||||||
|
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||||
|
int key = 6;
|
||||||
|
int expectedIndexForSearchKey = 7;
|
||||||
|
int low = 0;
|
||||||
|
int high = sortedArray.length - 1;
|
||||||
|
List<Integer> sortedList = Arrays.asList(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() {
|
||||||
|
BinarySearch binSearch = new BinarySearch();
|
||||||
|
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() {
|
||||||
|
BinarySearch binSearch = new BinarySearch();
|
||||||
|
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||||
|
BinarySearch binSearch = new BinarySearch();
|
||||||
|
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||||
|
BinarySearch binSearch = new BinarySearch();
|
||||||
|
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
|
@RunWith(value = Parameterized.class)
|
||||||
|
public class CycleDetectionBruteForceTest extends CycleDetectionTestBase {
|
||||||
|
boolean cycleExists;
|
||||||
|
Node<Integer> head;
|
||||||
|
|
||||||
|
public CycleDetectionBruteForceTest(Node<Integer> head, boolean cycleExists) {
|
||||||
|
super();
|
||||||
|
this.cycleExists = cycleExists;
|
||||||
|
this.head = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_detectLoop() {
|
||||||
|
Assert.assertEquals(cycleExists, CycleDetectionBruteForce.detectCycle(head).cycleExists);
|
||||||
|
}
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
|
@RunWith(value = Parameterized.class)
|
||||||
|
public class CycleDetectionByFastAndSlowIteratorsTest extends CycleDetectionTestBase {
|
||||||
|
boolean cycleExists;
|
||||||
|
Node<Integer> head;
|
||||||
|
|
||||||
|
public CycleDetectionByFastAndSlowIteratorsTest(Node<Integer> head, boolean cycleExists) {
|
||||||
|
super();
|
||||||
|
this.cycleExists = cycleExists;
|
||||||
|
this.head = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_detectLoop() {
|
||||||
|
Assert.assertEquals(cycleExists, CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||||
|
}
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
|
@RunWith(value = Parameterized.class)
|
||||||
|
public class CycleDetectionByHashingTest extends CycleDetectionTestBase {
|
||||||
|
boolean cycleExists;
|
||||||
|
Node<Integer> head;
|
||||||
|
|
||||||
|
public CycleDetectionByHashingTest(Node<Integer> head, boolean cycleExists) {
|
||||||
|
super();
|
||||||
|
this.cycleExists = cycleExists;
|
||||||
|
this.head = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_detectLoop() {
|
||||||
|
Assert.assertEquals(cycleExists, CycleDetectionByHashing.detectCycle(head).cycleExists);
|
||||||
|
}
|
||||||
|
}
|
||||||
+62
@@ -0,0 +1,62 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.runners.Parameterized.Parameters;
|
||||||
|
|
||||||
|
public class CycleDetectionTestBase {
|
||||||
|
|
||||||
|
@Parameters
|
||||||
|
public static Collection<Object[]> getLists() {
|
||||||
|
return Arrays.asList(new Object[][] {
|
||||||
|
{ createList(), false },
|
||||||
|
{ createListWithLoop(), true },
|
||||||
|
{ createListWithFullCycle(), true },
|
||||||
|
{ createListWithSingleNodeInCycle(), true }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Node<Integer> createList() {
|
||||||
|
Node<Integer> root = Node.createNewNode(10, null);
|
||||||
|
|
||||||
|
for (int i = 9; i >= 1; --i) {
|
||||||
|
Node<Integer> current = Node.createNewNode(i, root);
|
||||||
|
root = current;
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Node<Integer> createListWithLoop() {
|
||||||
|
Node<Integer> node = createList();
|
||||||
|
createLoop(node);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Node<Integer> createListWithFullCycle() {
|
||||||
|
Node<Integer> head = createList();
|
||||||
|
Node<Integer> tail = Node.getTail(head);
|
||||||
|
tail.next = head;
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Node<Integer> createListWithSingleNodeInCycle() {
|
||||||
|
Node<Integer> head = createList();
|
||||||
|
Node<Integer> tail = Node.getTail(head);
|
||||||
|
tail.next = tail;
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createLoop(Node<Integer> root) {
|
||||||
|
Node<Integer> tail = Node.getTail(root);
|
||||||
|
|
||||||
|
Node<Integer> middle = root;
|
||||||
|
for (int i = 1; i <= 4; i++) {
|
||||||
|
middle = middle.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
tail.next = middle;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
|
@RunWith(value = Parameterized.class)
|
||||||
|
public class CycleRemovalBruteForceTest extends CycleDetectionTestBase {
|
||||||
|
boolean cycleExists;
|
||||||
|
Node<Integer> head;
|
||||||
|
|
||||||
|
public CycleRemovalBruteForceTest(Node<Integer> head, boolean cycleExists) {
|
||||||
|
super();
|
||||||
|
this.cycleExists = cycleExists;
|
||||||
|
this.head = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
|
||||||
|
Assert.assertEquals(cycleExists, CycleRemovalBruteForce.detectAndRemoveCycle(head));
|
||||||
|
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||||
|
}
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
|
@RunWith(value = Parameterized.class)
|
||||||
|
public class CycleRemovalByCountingLoopNodesTest extends CycleDetectionTestBase {
|
||||||
|
boolean cycleExists;
|
||||||
|
Node<Integer> head;
|
||||||
|
|
||||||
|
public CycleRemovalByCountingLoopNodesTest(Node<Integer> head, boolean cycleExists) {
|
||||||
|
super();
|
||||||
|
this.cycleExists = cycleExists;
|
||||||
|
this.head = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
|
||||||
|
Assert.assertEquals(cycleExists, CycleRemovalByCountingLoopNodes.detectAndRemoveCycle(head));
|
||||||
|
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||||
|
}
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.algorithms.linkedlist;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
|
@RunWith(value = Parameterized.class)
|
||||||
|
public class CycleRemovalWithoutCountingLoopNodesTest extends CycleDetectionTestBase {
|
||||||
|
boolean cycleExists;
|
||||||
|
Node<Integer> head;
|
||||||
|
|
||||||
|
public CycleRemovalWithoutCountingLoopNodesTest(Node<Integer> head, boolean cycleExists) {
|
||||||
|
super();
|
||||||
|
this.cycleExists = cycleExists;
|
||||||
|
this.head = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
|
||||||
|
Assert.assertEquals(cycleExists, CycleRemovalWithoutCountingLoopNodes.detectAndRemoveCycle(head));
|
||||||
|
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.jgrapht;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.jgrapht.VertexFactory;
|
||||||
|
import org.jgrapht.alg.HamiltonianCycle;
|
||||||
|
import org.jgrapht.generate.CompleteGraphGenerator;
|
||||||
|
import org.jgrapht.graph.DefaultEdge;
|
||||||
|
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class CompleteGraphTest {
|
||||||
|
|
||||||
|
static SimpleWeightedGraph<String, DefaultEdge> completeGraph;
|
||||||
|
static int size = 10;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void createCompleteGraph() {
|
||||||
|
completeGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
|
||||||
|
CompleteGraphGenerator<String, DefaultEdge> completeGenerator = new CompleteGraphGenerator<String, DefaultEdge>(size);
|
||||||
|
VertexFactory<String> vFactory = new VertexFactory<String>() {
|
||||||
|
private int id = 0;
|
||||||
|
public String createVertex() {
|
||||||
|
return "v" + id++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
completeGenerator.generateGraph(completeGraph, vFactory, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCompleteGraph_whenGetHamiltonianCyclePath_thenGetVerticeListInSequence() {
|
||||||
|
List<String> verticeList = HamiltonianCycle.getApproximateOptimalForCompleteGraph(completeGraph);
|
||||||
|
assertEquals(verticeList.size(), completeGraph.vertexSet().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package com.baeldung.jgrapht;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import org.jgrapht.DirectedGraph;
|
||||||
|
import org.jgrapht.GraphPath;
|
||||||
|
import org.jgrapht.alg.CycleDetector;
|
||||||
|
import org.jgrapht.alg.KosarajuStrongConnectivityInspector;
|
||||||
|
import org.jgrapht.alg.interfaces.StrongConnectivityAlgorithm;
|
||||||
|
import org.jgrapht.alg.shortestpath.AllDirectedPaths;
|
||||||
|
import org.jgrapht.alg.shortestpath.BellmanFordShortestPath;
|
||||||
|
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
|
||||||
|
import org.jgrapht.graph.DefaultDirectedGraph;
|
||||||
|
import org.jgrapht.graph.DefaultEdge;
|
||||||
|
import org.jgrapht.graph.DirectedSubgraph;
|
||||||
|
import org.jgrapht.traverse.BreadthFirstIterator;
|
||||||
|
import org.jgrapht.traverse.DepthFirstIterator;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class DirectedGraphTests {
|
||||||
|
DirectedGraph<String, DefaultEdge> directedGraph;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void createDirectedGraph() {
|
||||||
|
directedGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
|
||||||
|
IntStream.range(1, 10).forEach(i -> {
|
||||||
|
directedGraph.addVertex("v" + i);
|
||||||
|
});
|
||||||
|
directedGraph.addEdge("v1", "v2");
|
||||||
|
directedGraph.addEdge("v2", "v4");
|
||||||
|
directedGraph.addEdge("v4", "v3");
|
||||||
|
directedGraph.addEdge("v3", "v1");
|
||||||
|
directedGraph.addEdge("v5", "v4");
|
||||||
|
directedGraph.addEdge("v5", "v6");
|
||||||
|
directedGraph.addEdge("v6", "v7");
|
||||||
|
directedGraph.addEdge("v7", "v5");
|
||||||
|
directedGraph.addEdge("v8", "v5");
|
||||||
|
directedGraph.addEdge("v9", "v8");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDirectedGraph_whenGetStronglyConnectedSubgraphs_thenPathExistsBetweenStronglyconnectedVertices() {
|
||||||
|
StrongConnectivityAlgorithm<String, DefaultEdge> scAlg = new KosarajuStrongConnectivityInspector<>(directedGraph);
|
||||||
|
List<DirectedSubgraph<String, DefaultEdge>> stronglyConnectedSubgraphs = scAlg.stronglyConnectedSubgraphs();
|
||||||
|
List<String> stronglyConnectedVertices = new ArrayList<>(stronglyConnectedSubgraphs.get(3).vertexSet());
|
||||||
|
|
||||||
|
String randomVertex1 = stronglyConnectedVertices.get(0);
|
||||||
|
String randomVertex2 = stronglyConnectedVertices.get(3);
|
||||||
|
AllDirectedPaths<String, DefaultEdge> allDirectedPaths = new AllDirectedPaths<>(directedGraph);
|
||||||
|
|
||||||
|
List<GraphPath<String, DefaultEdge>> possiblePathList = allDirectedPaths.getAllPaths(randomVertex1, randomVertex2, false, stronglyConnectedVertices.size());
|
||||||
|
assertTrue(possiblePathList.size() > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() {
|
||||||
|
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(directedGraph);
|
||||||
|
assertTrue(cycleDetector.detectCycles());
|
||||||
|
Set<String> cycleVertices = cycleDetector.findCycles();
|
||||||
|
assertTrue(cycleVertices.size() > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDirectedGraph_whenCreateInstanceDepthFirstIterator_thenGetIterator() {
|
||||||
|
DepthFirstIterator depthFirstIterator = new DepthFirstIterator<>(directedGraph);
|
||||||
|
assertNotNull(depthFirstIterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator() {
|
||||||
|
BreadthFirstIterator breadthFirstIterator = new BreadthFirstIterator<>(directedGraph);
|
||||||
|
assertNotNull(breadthFirstIterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDirectedGraph_whenGetDijkstraShortestPath_thenGetNotNullPath() {
|
||||||
|
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(directedGraph);
|
||||||
|
List<String> shortestPath = dijkstraShortestPath.getPath("v1", "v4").getVertexList();
|
||||||
|
assertNotNull(shortestPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDirectedGraph_whenGetBellmanFordShortestPath_thenGetNotNullPath() {
|
||||||
|
BellmanFordShortestPath bellmanFordShortestPath = new BellmanFordShortestPath(directedGraph);
|
||||||
|
List<String> shortestPath = bellmanFordShortestPath.getPath("v1", "v4").getVertexList();
|
||||||
|
assertNotNull(shortestPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.jgrapht;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import org.jgrapht.GraphPath;
|
||||||
|
import org.jgrapht.alg.cycle.HierholzerEulerianCycle;
|
||||||
|
import org.jgrapht.graph.DefaultEdge;
|
||||||
|
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class EulerianCircuitTest {
|
||||||
|
SimpleWeightedGraph<String, DefaultEdge> simpleGraph;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void createGraphWithEulerianCircuit() {
|
||||||
|
simpleGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
|
||||||
|
IntStream.range(1, 6).forEach(i -> {
|
||||||
|
simpleGraph.addVertex("v" + i);
|
||||||
|
});
|
||||||
|
IntStream.range(1, 6).forEach(i -> {
|
||||||
|
int endVertexNo = (i + 1) > 5 ? 1 : i + 1;
|
||||||
|
simpleGraph.addEdge("v" + i, "v" + endVertexNo);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGraph_whenCheckEluerianCycle_thenGetResult() {
|
||||||
|
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
|
||||||
|
assertTrue(eulerianCycle.isEulerian(simpleGraph));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGraphWithEulerianCircuit_whenGetEulerianCycle_thenGetGraphPath() {
|
||||||
|
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
|
||||||
|
GraphPath path = eulerianCycle.getEulerianCycle(simpleGraph);
|
||||||
|
assertTrue(path.getEdgeList().containsAll(simpleGraph.edgeSet()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>animal-sniffer-mvn-plugin</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>example-animal-sniffer-mvn-plugin</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.7.0</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.6</source>
|
||||||
|
<target>1.6</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>animal-sniffer-maven-plugin</artifactId>
|
||||||
|
<version>1.16</version>
|
||||||
|
<configuration>
|
||||||
|
<signature>
|
||||||
|
<groupId>org.codehaus.mojo.signature</groupId>
|
||||||
|
<artifactId>java16</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</signature>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>animal-sniffer</id>
|
||||||
|
<phase>verify</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>check</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
//import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hello world!
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class App
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
System.out.println( "Hello World!" );
|
||||||
|
//System.out.println(StandardCharsets.UTF_8.name());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest
|
||||||
|
extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the test case
|
||||||
|
*
|
||||||
|
* @param testName name of the test case
|
||||||
|
*/
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
super( testName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the suite of tests being tested
|
||||||
|
*/
|
||||||
|
public static Test suite()
|
||||||
|
{
|
||||||
|
return new TestSuite( AppTest.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rigourous Test :-)
|
||||||
|
*/
|
||||||
|
public void testApp()
|
||||||
|
{
|
||||||
|
|
||||||
|
assertTrue( true );
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>apache-cayenne</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>apache-cayenne</name>
|
||||||
|
<description>Introduction to Apache Cayenne</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<mysql.connector.version>5.1.44</mysql.connector.version>
|
||||||
|
<cayenne.version>4.0.M5</cayenne.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cayenne</groupId>
|
||||||
|
<artifactId>cayenne-server</artifactId>
|
||||||
|
<version>${cayenne.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>${mysql.connector.version}</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.cayenne.plugins</groupId>
|
||||||
|
<artifactId>cayenne-modeler-maven-plugin</artifactId>
|
||||||
|
<version>${cayenne.version}</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user