JAVA-12097: renamed algorithms-module to algorithms-modules
This commit is contained in:
13
algorithms-modules/algorithms-miscellaneous-6/README.md
Normal file
13
algorithms-modules/algorithms-miscellaneous-6/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Boruvka’s Algorithm for Minimum Spanning Trees in Java](https://www.baeldung.com/java-boruvka-algorithm)
|
||||
- [Gradient Descent in Java](https://www.baeldung.com/java-gradient-descent)
|
||||
- [Kruskal’s Algorithm for Spanning Trees with a Java Implementation](https://www.baeldung.com/java-spanning-trees-kruskal)
|
||||
- [Balanced Brackets Algorithm in Java](https://www.baeldung.com/java-balanced-brackets-algorithm)
|
||||
- [Efficiently Merge Sorted Java Sequences](https://www.baeldung.com/java-merge-sorted-sequences)
|
||||
- [Introduction to Greedy Algorithms with Java](https://www.baeldung.com/java-greedy-algorithms)
|
||||
- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher)
|
||||
- [Implementing a 2048 Solver in Java](https://www.baeldung.com/2048-java-solver)
|
||||
- [Finding Top K Elements in a Java Array](https://www.baeldung.com/java-array-top-elements)
|
||||
- [Reversing a Linked List in Java](https://www.baeldung.com/java-reverse-linked-list)
|
||||
- More articles: [[<-- prev]](/algorithms-miscellaneous-5)
|
||||
39
algorithms-modules/algorithms-miscellaneous-6/pom.xml
Normal file
39
algorithms-modules/algorithms-miscellaneous-6/pom.xml
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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>algorithms-miscellaneous-6</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-miscellaneous-6</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>algorithms-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.algorithms.balancedbrackets;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class BalancedBracketsUsingDeque {
|
||||
|
||||
public boolean isBalanced(String str) {
|
||||
if (null == str || ((str.length() % 2) != 0)) {
|
||||
return false;
|
||||
} else {
|
||||
char[] ch = str.toCharArray();
|
||||
for (char c : ch) {
|
||||
if (!(c == '{' || c == '[' || c == '(' || c == '}' || c == ']' || c == ')')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Deque<Character> deque = new LinkedList<>();
|
||||
for (char ch : str.toCharArray()) {
|
||||
if (ch == '{' || ch == '[' || ch == '(') {
|
||||
deque.addFirst(ch);
|
||||
} else {
|
||||
if (!deque.isEmpty() && ((deque.peekFirst() == '{' && ch == '}') || (deque.peekFirst() == '[' && ch == ']') || (deque.peekFirst() == '(' && ch == ')'))) {
|
||||
deque.removeFirst();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return deque.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.algorithms.balancedbrackets;
|
||||
|
||||
public class BalancedBracketsUsingString {
|
||||
|
||||
public boolean isBalanced(String str) {
|
||||
if (null == str || ((str.length() % 2) != 0)) {
|
||||
return false;
|
||||
} else {
|
||||
char[] ch = str.toCharArray();
|
||||
for (char c : ch) {
|
||||
if (!(c == '{' || c == '[' || c == '(' || c == '}' || c == ']' || c == ')')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
while (str.contains("()") || str.contains("[]") || str.contains("{}")) {
|
||||
str = str.replaceAll("\\(\\)", "")
|
||||
.replaceAll("\\[\\]", "")
|
||||
.replaceAll("\\{\\}", "");
|
||||
}
|
||||
return (str.length() == 0);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
import com.google.common.graph.EndpointPair;
|
||||
import com.google.common.graph.MutableValueGraph;
|
||||
import com.google.common.graph.ValueGraphBuilder;
|
||||
|
||||
public class BoruvkaMST {
|
||||
|
||||
private static MutableValueGraph<Integer, Integer> mst = ValueGraphBuilder.undirected()
|
||||
.build();
|
||||
private static int totalWeight;
|
||||
|
||||
public BoruvkaMST(MutableValueGraph<Integer, Integer> graph) {
|
||||
|
||||
int size = graph.nodes().size();
|
||||
|
||||
UnionFind uf = new UnionFind(size);
|
||||
|
||||
// repeat at most log N times or until we have N-1 edges
|
||||
for (int t = 1; t < size && mst.edges().size() < size - 1; t = t + t) {
|
||||
|
||||
EndpointPair<Integer>[] closestEdgeArray = new EndpointPair[size];
|
||||
|
||||
// foreach tree in graph, find closest edge
|
||||
for (EndpointPair<Integer> edge : graph.edges()) {
|
||||
int u = edge.nodeU();
|
||||
int v = edge.nodeV();
|
||||
int uParent = uf.find(u);
|
||||
int vParent = uf.find(v);
|
||||
if (uParent == vParent) {
|
||||
continue; // same tree
|
||||
}
|
||||
|
||||
int weight = graph.edgeValueOrDefault(u, v, 0);
|
||||
|
||||
if (closestEdgeArray[uParent] == null) {
|
||||
closestEdgeArray[uParent] = edge;
|
||||
}
|
||||
if (closestEdgeArray[vParent] == null) {
|
||||
closestEdgeArray[vParent] = edge;
|
||||
}
|
||||
|
||||
int uParentWeight = graph.edgeValueOrDefault(closestEdgeArray[uParent].nodeU(), closestEdgeArray[uParent].nodeV(), 0);
|
||||
int vParentWeight = graph.edgeValueOrDefault(closestEdgeArray[vParent].nodeU(), closestEdgeArray[vParent].nodeV(), 0);
|
||||
|
||||
if (weight < uParentWeight) {
|
||||
closestEdgeArray[uParent] = edge;
|
||||
}
|
||||
if (weight < vParentWeight) {
|
||||
closestEdgeArray[vParent] = edge;
|
||||
}
|
||||
}
|
||||
|
||||
// add newly discovered edges to MST
|
||||
for (int i = 0; i < size; i++) {
|
||||
EndpointPair<Integer> edge = closestEdgeArray[i];
|
||||
if (edge != null) {
|
||||
int u = edge.nodeU();
|
||||
int v = edge.nodeV();
|
||||
int weight = graph.edgeValueOrDefault(u, v, 0);
|
||||
// don't add the same edge twice
|
||||
if (uf.find(u) != uf.find(v)) {
|
||||
mst.putEdgeValue(u, v, weight);
|
||||
totalWeight += weight;
|
||||
uf.union(u, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MutableValueGraph<Integer, Integer> getMST() {
|
||||
return mst;
|
||||
}
|
||||
|
||||
public int getTotalWeight() {
|
||||
return totalWeight;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "MST: " + mst.toString() + " | Total Weight: " + totalWeight;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
public class UnionFind {
|
||||
private int[] parents;
|
||||
private int[] ranks;
|
||||
|
||||
public UnionFind(int n) {
|
||||
parents = new int[n];
|
||||
ranks = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
parents[i] = i;
|
||||
ranks[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int find(int u) {
|
||||
while (u != parents[u]) {
|
||||
u = parents[u];
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
public void union(int u, int v) {
|
||||
int uParent = find(u);
|
||||
int vParent = find(v);
|
||||
if (uParent == vParent) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ranks[uParent] < ranks[vParent]) {
|
||||
parents[uParent] = vParent;
|
||||
} else if (ranks[uParent] > ranks[vParent]) {
|
||||
parents[vParent] = uParent;
|
||||
} else {
|
||||
parents[vParent] = uParent;
|
||||
ranks[uParent]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.baeldung.algorithms.caesarcipher;
|
||||
|
||||
import org.apache.commons.math3.stat.inference.ChiSquareTest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class CaesarCipher {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(CaesarCipher.class);
|
||||
|
||||
private static final char LETTER_A = 'a';
|
||||
private static final char LETTER_Z = 'z';
|
||||
private static final int ALPHABET_SIZE = LETTER_Z - LETTER_A + 1;
|
||||
private static final double[] ENGLISH_LETTERS_PROBABILITIES = {0.073, 0.009, 0.030, 0.044, 0.130, 0.028, 0.016, 0.035, 0.074, 0.002, 0.003, 0.035, 0.025, 0.078, 0.074, 0.027, 0.003, 0.077, 0.063, 0.093, 0.027, 0.013, 0.016, 0.005, 0.019, 0.001};
|
||||
|
||||
public String cipher(String message, int offset) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
for (char character : message.toCharArray()) {
|
||||
if (character != ' ') {
|
||||
int originalAlphabetPosition = character - LETTER_A;
|
||||
int newAlphabetPosition = (originalAlphabetPosition + offset) % ALPHABET_SIZE;
|
||||
char newCharacter = (char) (LETTER_A + newAlphabetPosition);
|
||||
result.append(newCharacter);
|
||||
} else {
|
||||
result.append(character);
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public String decipher(String message, int offset) {
|
||||
return cipher(message, ALPHABET_SIZE - (offset % ALPHABET_SIZE));
|
||||
}
|
||||
|
||||
public int breakCipher(String message) {
|
||||
return probableOffset(chiSquares(message));
|
||||
}
|
||||
|
||||
private double[] chiSquares(String message) {
|
||||
double[] expectedLettersFrequencies = expectedLettersFrequencies(message.length());
|
||||
|
||||
double[] chiSquares = new double[ALPHABET_SIZE];
|
||||
|
||||
for (int offset = 0; offset < chiSquares.length; offset++) {
|
||||
String decipheredMessage = decipher(message, offset);
|
||||
long[] lettersFrequencies = observedLettersFrequencies(decipheredMessage);
|
||||
double chiSquare = new ChiSquareTest().chiSquare(expectedLettersFrequencies, lettersFrequencies);
|
||||
chiSquares[offset] = chiSquare;
|
||||
}
|
||||
|
||||
return chiSquares;
|
||||
}
|
||||
|
||||
private long[] observedLettersFrequencies(String message) {
|
||||
return IntStream.rangeClosed(LETTER_A, LETTER_Z)
|
||||
.mapToLong(letter -> countLetter((char) letter, message))
|
||||
.toArray();
|
||||
}
|
||||
|
||||
private long countLetter(char letter, String message) {
|
||||
return message.chars()
|
||||
.filter(character -> character == letter)
|
||||
.count();
|
||||
}
|
||||
|
||||
private double[] expectedLettersFrequencies(int messageLength) {
|
||||
return Arrays.stream(ENGLISH_LETTERS_PROBABILITIES)
|
||||
.map(probability -> probability * messageLength)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
private int probableOffset(double[] chiSquares) {
|
||||
int probableOffset = 0;
|
||||
for (int offset = 0; offset < chiSquares.length; offset++) {
|
||||
log.debug(String.format("Chi-Square for offset %d: %.2f", offset, chiSquares[offset]));
|
||||
if (chiSquares[offset] < chiSquares[probableOffset]) {
|
||||
probableOffset = offset;
|
||||
}
|
||||
}
|
||||
|
||||
return probableOffset;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.algorithms.gradientdescent;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class GradientDescent {
|
||||
|
||||
private final double precision = 0.000001;
|
||||
|
||||
public double findLocalMinimum(Function<Double, Double> f, double initialX) {
|
||||
double stepCoefficient = 0.1;
|
||||
double previousStep = 1.0;
|
||||
double currentX = initialX;
|
||||
double previousX = initialX;
|
||||
double previousY = f.apply(previousX);
|
||||
int iter = 100;
|
||||
|
||||
currentX += stepCoefficient * previousY;
|
||||
|
||||
while (previousStep > precision && iter > 0) {
|
||||
iter--;
|
||||
double currentY = f.apply(currentX);
|
||||
if (currentY > previousY) {
|
||||
stepCoefficient = -stepCoefficient / 2;
|
||||
}
|
||||
previousX = currentX;
|
||||
currentX += stepCoefficient * previousY;
|
||||
previousY = currentY;
|
||||
previousStep = StrictMath.abs(currentX - previousX);
|
||||
}
|
||||
return currentX;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.greedy;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
public class Follower {
|
||||
|
||||
@Getter String username;
|
||||
@Getter long count;
|
||||
|
||||
public Follower(String username, long count) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User: " + username + ", Followers: " + count + "\n\r" ;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.algorithms.greedy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FollowersPath {
|
||||
|
||||
private List<Follower> accounts;
|
||||
private long count;
|
||||
|
||||
public FollowersPath() {
|
||||
super();
|
||||
this.accounts = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Follower> getAccounts() {
|
||||
return accounts;
|
||||
}
|
||||
public long getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void addFollower(String username, long count) {
|
||||
accounts.add(new Follower(username, count));
|
||||
}
|
||||
|
||||
public void addCount(long count) {
|
||||
this.count += count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String details = "";
|
||||
for(Follower a : accounts) {
|
||||
details+=a.toString() + ", ";
|
||||
}
|
||||
|
||||
return "Total: " + count + ", \n\r" +
|
||||
" Details: { " + "\n\r" +
|
||||
details + "\n\r" +
|
||||
" }";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.algorithms.greedy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GreedyAlgorithm {
|
||||
|
||||
int currentLevel = 0;
|
||||
final int maxLevel = 3;
|
||||
SocialConnector sc;
|
||||
FollowersPath fp;
|
||||
|
||||
public GreedyAlgorithm(SocialConnector sc) {
|
||||
super();
|
||||
this.sc = sc;
|
||||
this.fp = new FollowersPath();
|
||||
}
|
||||
|
||||
public long findMostFollowersPath(String account) {
|
||||
long max = 0;
|
||||
SocialUser toFollow = null;
|
||||
|
||||
List<SocialUser> followers = sc.getFollowers(account);
|
||||
for (SocialUser el : followers) {
|
||||
long followersCount = el.getFollowersCount();
|
||||
if (followersCount > max) {
|
||||
toFollow = el;
|
||||
max = followersCount;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentLevel < maxLevel - 1) {
|
||||
currentLevel++;
|
||||
max += findMostFollowersPath(toFollow.getUsername());
|
||||
return max;
|
||||
} else {
|
||||
return max;
|
||||
}
|
||||
}
|
||||
|
||||
public FollowersPath getFollowers() {
|
||||
return fp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.algorithms.greedy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class NonGreedyAlgorithm {
|
||||
|
||||
int currentLevel = 0;
|
||||
final int maxLevel = 3;
|
||||
SocialConnector tc;
|
||||
|
||||
public NonGreedyAlgorithm(SocialConnector tc, int level) {
|
||||
super();
|
||||
this.tc = tc;
|
||||
this.currentLevel = level;
|
||||
}
|
||||
|
||||
public long findMostFollowersPath(String account) {
|
||||
List<SocialUser> followers = tc.getFollowers(account);
|
||||
long total = currentLevel > 0 ? followers.size() : 0;
|
||||
|
||||
if (currentLevel < maxLevel ) {
|
||||
currentLevel++;
|
||||
|
||||
long[] count = new long[followers.size()];
|
||||
int i = 0;
|
||||
for (SocialUser el : followers) {
|
||||
NonGreedyAlgorithm sub = new NonGreedyAlgorithm(tc, currentLevel);
|
||||
count[i] = sub.findMostFollowersPath(el.getUsername());
|
||||
i++;
|
||||
}
|
||||
|
||||
long max = 0;
|
||||
for (; i > 0; i--) {
|
||||
if (count[i-1] > max )
|
||||
max = count[i-1];
|
||||
}
|
||||
|
||||
return total + max;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.algorithms.greedy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class SocialConnector {
|
||||
private boolean isCounterEnabled = true;
|
||||
private int counter = 4;
|
||||
@Getter @Setter List<SocialUser> users;
|
||||
|
||||
public SocialConnector() {
|
||||
users = new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean switchCounter() {
|
||||
this.isCounterEnabled = !this.isCounterEnabled;
|
||||
return this.isCounterEnabled;
|
||||
}
|
||||
|
||||
public List<SocialUser> getFollowers(String account) {
|
||||
if (counter < 0)
|
||||
throw new IllegalStateException ("API limit reached");
|
||||
else {
|
||||
if(this.isCounterEnabled) counter--;
|
||||
for(SocialUser user : users) {
|
||||
if (user.getUsername().equals(account)) {
|
||||
return user.getFollowers();
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.algorithms.greedy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
public class SocialUser {
|
||||
|
||||
@Getter private String username;
|
||||
@Getter private List<SocialUser> followers;
|
||||
|
||||
public SocialUser(String username) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.followers = new ArrayList<>();
|
||||
}
|
||||
|
||||
public SocialUser(String username, List<SocialUser> followers) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.followers = followers;
|
||||
}
|
||||
|
||||
public long getFollowersCount() {
|
||||
return followers.size();
|
||||
}
|
||||
|
||||
public void addFollowers(List<SocialUser> followers) {
|
||||
this.followers.addAll(followers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return ((SocialUser) obj).getUsername().equals(username);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.algorithms.kruskal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CycleDetector {
|
||||
|
||||
List<DisjointSetInfo> nodes;
|
||||
|
||||
public CycleDetector(int totalNodes) {
|
||||
initDisjointSets(totalNodes);
|
||||
}
|
||||
|
||||
public boolean detectCycle(Integer u, Integer v) {
|
||||
Integer rootU = pathCompressionFind(u);
|
||||
Integer rootV = pathCompressionFind(v);
|
||||
if (rootU.equals(rootV)) {
|
||||
return true;
|
||||
}
|
||||
unionByRank(rootU, rootV);
|
||||
return false;
|
||||
}
|
||||
|
||||
private void initDisjointSets(int totalNodes) {
|
||||
nodes = new ArrayList<>(totalNodes);
|
||||
for (int i = 0; i < totalNodes; i++) {
|
||||
nodes.add(new DisjointSetInfo(i));
|
||||
}
|
||||
}
|
||||
|
||||
private Integer find(Integer node) {
|
||||
Integer parent = nodes.get(node).getParentNode();
|
||||
if (parent.equals(node)) {
|
||||
return node;
|
||||
} else {
|
||||
return find(parent);
|
||||
}
|
||||
}
|
||||
|
||||
private Integer pathCompressionFind(Integer node) {
|
||||
DisjointSetInfo setInfo = nodes.get(node);
|
||||
Integer parent = setInfo.getParentNode();
|
||||
if (parent.equals(node)) {
|
||||
return node;
|
||||
} else {
|
||||
Integer parentNode = find(parent);
|
||||
setInfo.setParentNode(parentNode);
|
||||
return parentNode;
|
||||
}
|
||||
}
|
||||
|
||||
private void union(Integer rootU, Integer rootV) {
|
||||
DisjointSetInfo setInfoU = nodes.get(rootU);
|
||||
setInfoU.setParentNode(rootV);
|
||||
}
|
||||
|
||||
private void unionByRank(int rootU, int rootV) {
|
||||
DisjointSetInfo setInfoU = nodes.get(rootU);
|
||||
DisjointSetInfo setInfoV = nodes.get(rootV);
|
||||
int rankU = setInfoU.getRank();
|
||||
int rankV = setInfoV.getRank();
|
||||
if (rankU < rankV) {
|
||||
setInfoU.setParentNode(rootV);
|
||||
} else {
|
||||
setInfoV.setParentNode(rootU);
|
||||
if (rankU == rankV) {
|
||||
setInfoU.setRank(rankU + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.algorithms.kruskal;
|
||||
|
||||
public class DisjointSetInfo {
|
||||
|
||||
private Integer parentNode;
|
||||
private int rank;
|
||||
|
||||
DisjointSetInfo(Integer nodeNumber) {
|
||||
setParentNode(nodeNumber);
|
||||
setRank(1);
|
||||
}
|
||||
|
||||
public Integer getParentNode() {
|
||||
return parentNode;
|
||||
}
|
||||
|
||||
public void setParentNode(Integer parentNode) {
|
||||
this.parentNode = parentNode;
|
||||
}
|
||||
|
||||
public int getRank() {
|
||||
return rank;
|
||||
}
|
||||
|
||||
public void setRank(int rank) {
|
||||
this.rank = rank;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.algorithms.kruskal;
|
||||
|
||||
import com.google.common.graph.EndpointPair;
|
||||
import com.google.common.graph.MutableValueGraph;
|
||||
import com.google.common.graph.ValueGraph;
|
||||
import com.google.common.graph.ValueGraphBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class Kruskal {
|
||||
|
||||
public ValueGraph<Integer, Double> minSpanningTree(ValueGraph<Integer, Double> graph) {
|
||||
|
||||
return spanningTree(graph, true);
|
||||
}
|
||||
|
||||
public ValueGraph<Integer, Double> maxSpanningTree(ValueGraph<Integer, Double> graph) {
|
||||
return spanningTree(graph, false);
|
||||
}
|
||||
|
||||
private ValueGraph<Integer, Double> spanningTree(ValueGraph<Integer, Double> graph, boolean minSpanningTree) {
|
||||
Set<EndpointPair<Integer>> edges = graph.edges();
|
||||
List<EndpointPair<Integer>> edgeList = new ArrayList<>(edges);
|
||||
|
||||
if (minSpanningTree) {
|
||||
edgeList.sort(Comparator.comparing(e -> graph.edgeValue(e).get()));
|
||||
} else {
|
||||
edgeList.sort(Collections.reverseOrder(Comparator.comparing(e -> graph.edgeValue(e).get())));
|
||||
}
|
||||
|
||||
int totalNodes = graph.nodes().size();
|
||||
CycleDetector cycleDetector = new CycleDetector(totalNodes);
|
||||
int edgeCount = 0;
|
||||
|
||||
MutableValueGraph<Integer, Double> spanningTree = ValueGraphBuilder.undirected().build();
|
||||
for (EndpointPair<Integer> edge : edgeList) {
|
||||
if (cycleDetector.detectCycle(edge.nodeU(), edge.nodeV())) {
|
||||
continue;
|
||||
}
|
||||
spanningTree.putEdgeValue(edge.nodeU(), edge.nodeV(), graph.edgeValue(edge).get());
|
||||
edgeCount++;
|
||||
if (edgeCount == totalNodes - 1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return spanningTree;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
public class LinkedListReversal {
|
||||
|
||||
ListNode reverseList(ListNode head) {
|
||||
ListNode previous = null;
|
||||
ListNode current = head;
|
||||
while (current != null) {
|
||||
ListNode nextElement = current.getNext();
|
||||
current.setNext(previous);
|
||||
previous = current;
|
||||
current = nextElement;
|
||||
}
|
||||
return previous;
|
||||
}
|
||||
|
||||
ListNode reverseListRecursive(ListNode head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
if (head.getNext() == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode node = reverseListRecursive(head.getNext());
|
||||
head.getNext().setNext(head);
|
||||
head.setNext(null);
|
||||
return node;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
public class ListNode {
|
||||
|
||||
private int data;
|
||||
private ListNode next;
|
||||
|
||||
ListNode(int data) {
|
||||
this.data = data;
|
||||
this.next = null;
|
||||
}
|
||||
|
||||
public int getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public ListNode getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setData(int data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setNext(ListNode next) {
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class HeapNode {
|
||||
|
||||
int element;
|
||||
int arrayIndex;
|
||||
int nextElementIndex = 1;
|
||||
|
||||
public HeapNode(int element, int arrayIndex) {
|
||||
this.element = element;
|
||||
this.arrayIndex = arrayIndex;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class MinHeap {
|
||||
|
||||
HeapNode[] heapNodes;
|
||||
|
||||
public MinHeap(HeapNode heapNodes[]) {
|
||||
this.heapNodes = heapNodes;
|
||||
heapifyFromLastLeafsParent();
|
||||
}
|
||||
|
||||
void heapifyFromLastLeafsParent() {
|
||||
int lastLeafsParentIndex = getParentNodeIndex(heapNodes.length);
|
||||
while (lastLeafsParentIndex >= 0) {
|
||||
heapify(lastLeafsParentIndex);
|
||||
lastLeafsParentIndex--;
|
||||
}
|
||||
}
|
||||
|
||||
void heapify(int index) {
|
||||
int leftNodeIndex = getLeftNodeIndex(index);
|
||||
int rightNodeIndex = getRightNodeIndex(index);
|
||||
int smallestElementIndex = index;
|
||||
if (leftNodeIndex < heapNodes.length && heapNodes[leftNodeIndex].element < heapNodes[index].element) {
|
||||
smallestElementIndex = leftNodeIndex;
|
||||
}
|
||||
if (rightNodeIndex < heapNodes.length && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element) {
|
||||
smallestElementIndex = rightNodeIndex;
|
||||
}
|
||||
if (smallestElementIndex != index) {
|
||||
swap(index, smallestElementIndex);
|
||||
heapify(smallestElementIndex);
|
||||
}
|
||||
}
|
||||
|
||||
int getParentNodeIndex(int index) {
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
|
||||
int getLeftNodeIndex(int index) {
|
||||
return (2 * index + 1);
|
||||
}
|
||||
|
||||
int getRightNodeIndex(int index) {
|
||||
return (2 * index + 2);
|
||||
}
|
||||
|
||||
HeapNode getRootNode() {
|
||||
return heapNodes[0];
|
||||
}
|
||||
|
||||
void heapifyFromRoot() {
|
||||
heapify(0);
|
||||
}
|
||||
|
||||
void swap(int i, int j) {
|
||||
HeapNode temp = heapNodes[i];
|
||||
heapNodes[i] = heapNodes[j];
|
||||
heapNodes[j] = temp;
|
||||
}
|
||||
|
||||
static int[] merge(int[][] array) {
|
||||
HeapNode[] heapNodes = new HeapNode[array.length];
|
||||
int resultingArraySize = 0;
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
HeapNode node = new HeapNode(array[i][0], i);
|
||||
heapNodes[i] = node;
|
||||
resultingArraySize += array[i].length;
|
||||
}
|
||||
|
||||
MinHeap minHeap = new MinHeap(heapNodes);
|
||||
int[] resultingArray = new int[resultingArraySize];
|
||||
|
||||
for (int i = 0; i < resultingArraySize; i++) {
|
||||
HeapNode root = minHeap.getRootNode();
|
||||
resultingArray[i] = root.element;
|
||||
|
||||
if (root.nextElementIndex < array[root.arrayIndex].length) {
|
||||
root.element = array[root.arrayIndex][root.nextElementIndex++];
|
||||
} else {
|
||||
root.element = Integer.MAX_VALUE;
|
||||
}
|
||||
minHeap.heapifyFromRoot();
|
||||
}
|
||||
return resultingArray;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Board {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Board.class);
|
||||
|
||||
private final int[][] board;
|
||||
|
||||
private final int score;
|
||||
|
||||
public Board(int size) {
|
||||
assert(size > 0);
|
||||
|
||||
this.board = new int[size][];
|
||||
this.score = 0;
|
||||
|
||||
for (int x = 0; x < size; ++x) {
|
||||
this.board[x] = new int[size];
|
||||
for (int y = 0; y < size; ++y) {
|
||||
board[x][y] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Board(int[][] board, int score) {
|
||||
this.score = score;
|
||||
this.board = new int[board.length][];
|
||||
|
||||
for (int x = 0; x < board.length; ++x) {
|
||||
this.board[x] = Arrays.copyOf(board[x], board[x].length);
|
||||
}
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return board.length;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public int getCell(Cell cell) {
|
||||
int x = cell.getX();
|
||||
int y = cell.getY();
|
||||
assert(x >= 0 && x < board.length);
|
||||
assert(y >= 0 && y < board.length);
|
||||
|
||||
return board[x][y];
|
||||
}
|
||||
|
||||
public boolean isEmpty(Cell cell) {
|
||||
return getCell(cell) == 0;
|
||||
}
|
||||
|
||||
public List<Cell> emptyCells() {
|
||||
List<Cell> result = new ArrayList<>();
|
||||
for (int x = 0; x < board.length; ++x) {
|
||||
for (int y = 0; y < board[x].length; ++y) {
|
||||
Cell cell = new Cell(x, y);
|
||||
if (isEmpty(cell)) {
|
||||
result.add(cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Board placeTile(Cell cell, int number) {
|
||||
if (!isEmpty(cell)) {
|
||||
throw new IllegalArgumentException("That cell is not empty");
|
||||
}
|
||||
|
||||
Board result = new Board(this.board, this.score);
|
||||
result.board[cell.getX()][cell.getY()] = number;
|
||||
return result;
|
||||
}
|
||||
|
||||
public Board move(Move move) {
|
||||
// Clone the board
|
||||
int[][] tiles = new int[this.board.length][];
|
||||
for (int x = 0; x < this.board.length; ++x) {
|
||||
tiles[x] = Arrays.copyOf(this.board[x], this.board[x].length);
|
||||
}
|
||||
|
||||
LOG.debug("Before move: {}", Arrays.deepToString(tiles));
|
||||
// If we're doing an Left/Right move then transpose the board to make it a Up/Down move
|
||||
if (move == Move.LEFT || move == Move.RIGHT) {
|
||||
tiles = transpose(tiles);
|
||||
LOG.debug("After transpose: {}", Arrays.deepToString(tiles));
|
||||
}
|
||||
// If we're doing a Right/Down move then reverse the board.
|
||||
// With the above we're now always doing an Up move
|
||||
if (move == Move.DOWN || move == Move.RIGHT) {
|
||||
tiles = reverse(tiles);
|
||||
LOG.debug("After reverse: {}", Arrays.deepToString(tiles));
|
||||
}
|
||||
LOG.debug("Ready to move: {}", Arrays.deepToString(tiles));
|
||||
|
||||
// Shift everything up
|
||||
int[][] result = new int[tiles.length][];
|
||||
int newScore = 0;
|
||||
for (int x = 0; x < tiles.length; ++x) {
|
||||
LinkedList<Integer> thisRow = new LinkedList<>();
|
||||
for (int y = 0; y < tiles[0].length; ++y) {
|
||||
if (tiles[x][y] > 0) {
|
||||
thisRow.add(tiles[x][y]);
|
||||
}
|
||||
}
|
||||
|
||||
LOG.debug("Unmerged row: {}", thisRow);
|
||||
LinkedList<Integer> newRow = new LinkedList<>();
|
||||
while (thisRow.size() >= 2) {
|
||||
int first = thisRow.pop();
|
||||
int second = thisRow.peek();
|
||||
LOG.debug("Looking at numbers {} and {}", first, second);
|
||||
if (second == first) {
|
||||
LOG.debug("Numbers match, combining");
|
||||
int newNumber = first * 2;
|
||||
newRow.add(newNumber);
|
||||
newScore += newNumber;
|
||||
thisRow.pop();
|
||||
} else {
|
||||
LOG.debug("Numbers don't match");
|
||||
newRow.add(first);
|
||||
}
|
||||
}
|
||||
newRow.addAll(thisRow);
|
||||
LOG.debug("Merged row: {}", newRow);
|
||||
|
||||
result[x] = new int[tiles[0].length];
|
||||
for (int y = 0; y < tiles[0].length; ++y) {
|
||||
if (newRow.isEmpty()) {
|
||||
result[x][y] = 0;
|
||||
} else {
|
||||
result[x][y] = newRow.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG.debug("After moves: {}", Arrays.deepToString(result));
|
||||
|
||||
// Un-reverse the board
|
||||
if (move == Move.DOWN || move == Move.RIGHT) {
|
||||
result = reverse(result);
|
||||
LOG.debug("After reverse: {}", Arrays.deepToString(result));
|
||||
}
|
||||
// Un-transpose the board
|
||||
if (move == Move.LEFT || move == Move.RIGHT) {
|
||||
result = transpose(result);
|
||||
LOG.debug("After transpose: {}", Arrays.deepToString(result));
|
||||
}
|
||||
return new Board(result, this.score + newScore);
|
||||
}
|
||||
|
||||
private static int[][] transpose(int[][] input) {
|
||||
int[][] result = new int[input.length][];
|
||||
|
||||
for (int x = 0; x < input.length; ++x) {
|
||||
result[x] = new int[input[0].length];
|
||||
for (int y = 0; y < input[0].length; ++y) {
|
||||
result[x][y] = input[y][x];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int[][] reverse(int[][] input) {
|
||||
int[][] result = new int[input.length][];
|
||||
|
||||
for (int x = 0; x < input.length; ++x) {
|
||||
result[x] = new int[input[0].length];
|
||||
for (int y = 0; y < input[0].length; ++y) {
|
||||
result[x][y] = input[x][input.length - y - 1];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Arrays.deepToString(board);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Board board1 = (Board) o;
|
||||
return Arrays.deepEquals(board, board1.board);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.deepHashCode(board);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class Cell {
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Cell(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Cell.class.getSimpleName() + "[", "]").add("x=" + x).add("y=" + y).toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Computer {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Computer.class);
|
||||
|
||||
private final SecureRandom rng = new SecureRandom();
|
||||
|
||||
public Board makeMove(Board input) {
|
||||
List<Cell> emptyCells = input.emptyCells();
|
||||
LOG.info("Number of empty cells: {}", emptyCells.size());
|
||||
|
||||
double numberToPlace = rng.nextDouble();
|
||||
LOG.info("New number probability: {}", numberToPlace);
|
||||
|
||||
int indexToPlace = rng.nextInt(emptyCells.size());
|
||||
Cell cellToPlace = emptyCells.get(indexToPlace);
|
||||
LOG.info("Placing number into empty cell: {}", cellToPlace);
|
||||
|
||||
return input.placeTile(cellToPlace, numberToPlace >= 0.9 ? 4 : 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.math3.util.Pair;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Human {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Human.class);
|
||||
|
||||
public Board makeMove(Board input) {
|
||||
// For each move in MOVE
|
||||
// Generate board from move
|
||||
// Generate Score for Board
|
||||
// Return board with the best score
|
||||
//
|
||||
// Generate Score
|
||||
// If Depth Limit
|
||||
// Return Final Score
|
||||
// Total Score = 0
|
||||
// For every empty square in new board
|
||||
// Generate board with "2" in square
|
||||
// Calculate Score
|
||||
// Total Score += (Score * 0.9)
|
||||
// Generate board with "4" in square
|
||||
// Calculate Score
|
||||
// Total Score += (Score * 0.1)
|
||||
//
|
||||
// Calculate Score
|
||||
// For each move in MOVE
|
||||
// Generate board from move
|
||||
// Generate score for board
|
||||
// Return the best generated score
|
||||
|
||||
return Arrays.stream(Move.values())
|
||||
.parallel()
|
||||
.map(input::move)
|
||||
.filter(board -> !board.equals(input))
|
||||
.max(Comparator.comparingInt(board -> generateScore(board, 0)))
|
||||
.orElse(input);
|
||||
}
|
||||
|
||||
private int generateScore(Board board, int depth) {
|
||||
if (depth >= 3) {
|
||||
int finalScore = calculateFinalScore(board);
|
||||
LOG.debug("Final score for board {}: {}", board,finalScore);
|
||||
return finalScore;
|
||||
}
|
||||
|
||||
return board.emptyCells().stream()
|
||||
.parallel()
|
||||
.flatMap(cell -> Stream.of(new Pair<>(cell, 2), new Pair<>(cell, 4)))
|
||||
.mapToInt(move -> {
|
||||
LOG.debug("Simulating move {} at depth {}", move, depth);
|
||||
Board newBoard = board.placeTile(move.getFirst(), move.getSecond());
|
||||
int boardScore = calculateScore(newBoard, depth + 1);
|
||||
int calculatedScore = (int) (boardScore * (move.getSecond() == 2 ? 0.9 : 0.1));
|
||||
LOG.debug("Calculated score for board {} and move {} at depth {}: {}", newBoard, move, depth, calculatedScore);
|
||||
return calculatedScore;
|
||||
})
|
||||
.sum();
|
||||
}
|
||||
|
||||
private int calculateScore(Board board, int depth) {
|
||||
return Arrays.stream(Move.values())
|
||||
.parallel()
|
||||
.map(board::move)
|
||||
.filter(moved -> !moved.equals(board))
|
||||
.mapToInt(newBoard -> generateScore(newBoard, depth))
|
||||
.max()
|
||||
.orElse(0);
|
||||
}
|
||||
|
||||
private int calculateFinalScore(Board board) {
|
||||
List<List<Integer>> rowsToScore = new ArrayList<>();
|
||||
for (int i = 0; i < board.getSize(); ++i) {
|
||||
List<Integer> row = new ArrayList<>();
|
||||
List<Integer> col = new ArrayList<>();
|
||||
|
||||
for (int j = 0; j < board.getSize(); ++j) {
|
||||
row.add(board.getCell(new Cell(i, j)));
|
||||
col.add(board.getCell(new Cell(j, i)));
|
||||
}
|
||||
|
||||
rowsToScore.add(row);
|
||||
rowsToScore.add(col);
|
||||
}
|
||||
|
||||
return rowsToScore.stream()
|
||||
.parallel()
|
||||
.mapToInt(row -> {
|
||||
List<Integer> preMerged = row.stream()
|
||||
.filter(value -> value != 0)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
int numMerges = 0;
|
||||
int monotonicityLeft = 0;
|
||||
int monotonicityRight = 0;
|
||||
for (int i = 0; i < preMerged.size() - 1; ++i) {
|
||||
Integer first = preMerged.get(i);
|
||||
Integer second = preMerged.get(i + 1);
|
||||
if (first.equals(second)) {
|
||||
++numMerges;
|
||||
} else if (first > second) {
|
||||
monotonicityLeft += first - second;
|
||||
} else {
|
||||
monotonicityRight += second - first;
|
||||
}
|
||||
}
|
||||
|
||||
int score = 1000;
|
||||
score += 250 * row.stream().filter(value -> value == 0).count();
|
||||
score += 750 * numMerges;
|
||||
score -= 10 * row.stream().mapToInt(value -> value).sum();
|
||||
score -= 50 * Math.min(monotonicityLeft, monotonicityRight);
|
||||
return score;
|
||||
})
|
||||
.sum();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
public enum Move {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
public class Play2048 {
|
||||
private static final int SIZE = 4;
|
||||
private static final int INITIAL_NUMBERS = 2;
|
||||
|
||||
public static void main(String[] args) {
|
||||
// The board and players
|
||||
Board board = new Board(SIZE);
|
||||
Computer computer = new Computer();
|
||||
Human human = new Human();
|
||||
|
||||
// The computer has two moves first
|
||||
System.out.println("Setup");
|
||||
System.out.println("=====");
|
||||
for (int i = 0; i < INITIAL_NUMBERS; ++i) {
|
||||
board = computer.makeMove(board);
|
||||
}
|
||||
|
||||
printBoard(board);
|
||||
do {
|
||||
board = human.makeMove(board);
|
||||
System.out.println("Human move");
|
||||
System.out.println("==========");
|
||||
printBoard(board);
|
||||
|
||||
board = computer.makeMove(board);
|
||||
System.out.println("Computer move");
|
||||
System.out.println("=============");
|
||||
printBoard(board);
|
||||
} while (!board.emptyCells().isEmpty());
|
||||
|
||||
System.out.println("Final Score: " + board.getScore());
|
||||
|
||||
}
|
||||
|
||||
private static void printBoard(Board board) {
|
||||
StringBuilder topLines = new StringBuilder();
|
||||
StringBuilder midLines = new StringBuilder();
|
||||
for (int x = 0; x < board.getSize(); ++x) {
|
||||
topLines.append("+--------");
|
||||
midLines.append("| ");
|
||||
}
|
||||
topLines.append("+");
|
||||
midLines.append("|");
|
||||
|
||||
|
||||
for (int y = 0; y < board.getSize(); ++y) {
|
||||
System.out.println(topLines);
|
||||
System.out.println(midLines);
|
||||
for (int x = 0; x < board.getSize(); ++x) {
|
||||
Cell cell = new Cell(x, y);
|
||||
System.out.print("|");
|
||||
if (board.isEmpty(cell)) {
|
||||
System.out.print(" ");
|
||||
} else {
|
||||
StringBuilder output = new StringBuilder(Integer.toString(board.getCell(cell)));
|
||||
while (output.length() < 8) {
|
||||
output.append(" ");
|
||||
if (output.length() < 8) {
|
||||
output.insert(0, " ");
|
||||
}
|
||||
}
|
||||
System.out.print(output);
|
||||
}
|
||||
}
|
||||
System.out.println("|");
|
||||
System.out.println(midLines);
|
||||
}
|
||||
System.out.println(topLines);
|
||||
System.out.println("Score: " + board.getScore());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.topkelements;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BruteForceTopKElementsFinder implements TopKElementsFinder<Integer> {
|
||||
|
||||
public List<Integer> findTopK(List<Integer> input, int k) {
|
||||
List<Integer> array = new ArrayList<>(input);
|
||||
List<Integer> topKList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < k; i++) {
|
||||
int maxIndex = 0;
|
||||
|
||||
for (int j = 1; j < array.size(); j++) {
|
||||
if (array.get(j) > array.get(maxIndex)) {
|
||||
maxIndex = j;
|
||||
}
|
||||
}
|
||||
|
||||
topKList.add(array.remove(maxIndex));
|
||||
}
|
||||
|
||||
return topKList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.topkelements;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
public class MaxHeapTopKElementsFinder implements TopKElementsFinder<Integer> {
|
||||
|
||||
public List<Integer> findTopK(List<Integer> input, int k) {
|
||||
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
|
||||
|
||||
input.forEach(number -> {
|
||||
maxHeap.add(number);
|
||||
|
||||
if (maxHeap.size() > k) {
|
||||
maxHeap.poll();
|
||||
}
|
||||
});
|
||||
|
||||
List<Integer> topKList = new ArrayList<>(maxHeap);
|
||||
Collections.reverse(topKList);
|
||||
|
||||
return topKList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.algorithms.topkelements;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TopKElementsFinder<T extends Comparable<T>> {
|
||||
List<T> findTopK(List<T> input, int k);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.algorithms.topkelements;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TreeSetTopKElementsFinder implements TopKElementsFinder<Integer> {
|
||||
|
||||
public List<Integer> findTopK(List<Integer> input, int k) {
|
||||
Set<Integer> sortedSet = new TreeSet<>(Comparator.reverseOrder());
|
||||
sortedSet.addAll(input);
|
||||
|
||||
return sortedSet.stream().limit(k).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.algorithms.balancedbrackets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BalancedBracketsUsingDequeUnitTest {
|
||||
private BalancedBracketsUsingDeque balancedBracketsUsingDeque;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
balancedBracketsUsingDeque = new BalancedBracketsUsingDeque();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullInput_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced(null);
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyString_whenCheckingForBalance_shouldReturnTrue() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("");
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidCharacterString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("abc[](){}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOddLengthString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("{{[]()}}}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEvenLengthString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("{{[]()}}}}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("{[(])}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("{{}(");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("{[()]}");
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("{{[[(())]]}}");
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnotherBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("{{([])}}");
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnBalancedString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingDeque.isBalanced("{{)[](}}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.algorithms.balancedbrackets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BalancedBracketsUsingStringUnitTest {
|
||||
private BalancedBracketsUsingString balancedBracketsUsingString;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
balancedBracketsUsingString = new BalancedBracketsUsingString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullInput_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced(null);
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyString_whenCheckingForBalance_shouldReturnTrue() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("");
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidCharacterString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("abc[](){}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOddLengthString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("{{[]()}}}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEvenLengthString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("{{[]()}}}}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("{[(])}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("{{}(");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("{[()]}");
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("{{[[(())]]}}");
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnotherBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("{{([])}}");
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnBalancedString_whenCheckingForBalance_shouldReturnFalse() {
|
||||
boolean result = balancedBracketsUsingString.isBalanced("{{)[](}}");
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.graph.MutableValueGraph;
|
||||
import com.google.common.graph.ValueGraphBuilder;
|
||||
|
||||
public class BoruvkaUnitTest {
|
||||
|
||||
private MutableValueGraph<Integer, Integer> graph;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
graph = ValueGraphBuilder.undirected()
|
||||
.build();
|
||||
graph.putEdgeValue(0, 1, 8);
|
||||
graph.putEdgeValue(0, 2, 5);
|
||||
graph.putEdgeValue(1, 2, 9);
|
||||
graph.putEdgeValue(1, 3, 11);
|
||||
graph.putEdgeValue(2, 3, 15);
|
||||
graph.putEdgeValue(2, 4, 10);
|
||||
graph.putEdgeValue(3, 4, 7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputGraph_whenBoruvkaPerformed_thenMinimumSpanningTree() {
|
||||
BoruvkaMST boruvkaMST = new BoruvkaMST(graph);
|
||||
MutableValueGraph<Integer, Integer> mst = boruvkaMST.getMST();
|
||||
|
||||
assertEquals(30, boruvkaMST.getTotalWeight());
|
||||
assertEquals(4, mst.edges().size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.algorithms.caesarcipher;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class CaesarCipherUnitTest {
|
||||
private static final String SENTENCE = "he told me i could never teach a llama to drive";
|
||||
private static final String SENTENCE_SHIFTED_THREE = "kh wrog ph l frxog qhyhu whdfk d oodpd wr gulyh";
|
||||
private static final String SENTENCE_SHIFTED_TEN = "ro dyvn wo s myevn xofob dokmr k vvkwk dy nbsfo";
|
||||
|
||||
private CaesarCipher algorithm = new CaesarCipher();
|
||||
|
||||
@Test
|
||||
void givenSentenceAndShiftThree_whenCipher_thenCipheredMessageWithoutOverflow() {
|
||||
String cipheredSentence = algorithm.cipher(SENTENCE, 3);
|
||||
|
||||
assertThat(cipheredSentence)
|
||||
.isEqualTo(SENTENCE_SHIFTED_THREE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSentenceAndShiftTen_whenCipher_thenCipheredMessageWithOverflow() {
|
||||
String cipheredSentence = algorithm.cipher(SENTENCE, 10);
|
||||
|
||||
assertThat(cipheredSentence)
|
||||
.isEqualTo(SENTENCE_SHIFTED_TEN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSentenceAndShiftThirtySix_whenCipher_thenCipheredLikeTenMessageWithOverflow() {
|
||||
String cipheredSentence = algorithm.cipher(SENTENCE, 36);
|
||||
|
||||
assertThat(cipheredSentence)
|
||||
.isEqualTo(SENTENCE_SHIFTED_TEN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSentenceShiftedThreeAndShiftThree_whenDecipher_thenOriginalSentenceWithoutOverflow() {
|
||||
String decipheredSentence = algorithm.decipher(SENTENCE_SHIFTED_THREE, 3);
|
||||
|
||||
assertThat(decipheredSentence)
|
||||
.isEqualTo(SENTENCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSentenceShiftedTenAndShiftTen_whenDecipher_thenOriginalSentenceWithOverflow() {
|
||||
String decipheredSentence = algorithm.decipher(SENTENCE_SHIFTED_TEN, 10);
|
||||
|
||||
assertThat(decipheredSentence)
|
||||
.isEqualTo(SENTENCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSentenceShiftedTenAndShiftThirtySix_whenDecipher_thenOriginalSentenceWithOverflow() {
|
||||
String decipheredSentence = algorithm.decipher(SENTENCE_SHIFTED_TEN, 36);
|
||||
|
||||
assertThat(decipheredSentence)
|
||||
.isEqualTo(SENTENCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSentenceShiftedThree_whenBreakCipher_thenOriginalSentence() {
|
||||
int offset = algorithm.breakCipher(SENTENCE_SHIFTED_THREE);
|
||||
|
||||
assertThat(offset)
|
||||
.isEqualTo(3);
|
||||
|
||||
assertThat(algorithm.decipher(SENTENCE_SHIFTED_THREE, offset))
|
||||
.isEqualTo(SENTENCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSentenceShiftedTen_whenBreakCipher_thenOriginalSentence() {
|
||||
int offset = algorithm.breakCipher(SENTENCE_SHIFTED_TEN);
|
||||
|
||||
assertThat(offset)
|
||||
.isEqualTo(10);
|
||||
|
||||
assertThat(algorithm.decipher(SENTENCE_SHIFTED_TEN, offset))
|
||||
.isEqualTo(SENTENCE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.gradientdescent;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class GradientDescentUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenFunction_whenStartingPointIsOne_thenLocalMinimumIsFound() {
|
||||
Function<Double, Double> df = x ->
|
||||
StrictMath.abs(StrictMath.pow(x, 3)) - (3 * StrictMath.pow(x, 2)) + x;
|
||||
GradientDescent gd = new GradientDescent();
|
||||
double res = gd.findLocalMinimum(df, 1);
|
||||
assertTrue(res > 1.78);
|
||||
assertTrue(res < 1.84);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.algorithms.greedy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class GreedyAlgorithmUnitTest {
|
||||
|
||||
private SocialConnector prepareNetwork() {
|
||||
SocialConnector sc = new SocialConnector();
|
||||
SocialUser root = new SocialUser("root");
|
||||
SocialUser child1 = new SocialUser("child1");
|
||||
SocialUser child2 = new SocialUser("child2");
|
||||
SocialUser child3 = new SocialUser("child3");
|
||||
SocialUser child21 = new SocialUser("child21");
|
||||
SocialUser child211 = new SocialUser("child211");
|
||||
SocialUser child2111 = new SocialUser("child2111");
|
||||
SocialUser child31 = new SocialUser("child31");
|
||||
SocialUser child311 = new SocialUser("child311");
|
||||
SocialUser child3111 = new SocialUser("child3111");
|
||||
child211.addFollowers(Arrays.asList(new SocialUser[]{child2111}));
|
||||
child311.addFollowers(Arrays.asList(new SocialUser[]{child3111}));
|
||||
child21.addFollowers(Arrays.asList(new SocialUser[]{child211}));
|
||||
child31.addFollowers(Arrays.asList(new SocialUser[]{child311,
|
||||
new SocialUser("child312"), new SocialUser("child313"), new SocialUser("child314")}));
|
||||
child1.addFollowers(Arrays.asList(new SocialUser[]{new SocialUser("child11"), new SocialUser("child12")}));
|
||||
child2.addFollowers(Arrays.asList(new SocialUser[]{child21, new SocialUser("child22"), new SocialUser("child23")}));
|
||||
child3.addFollowers(Arrays.asList(new SocialUser[]{child31}));
|
||||
root.addFollowers(Arrays.asList(new SocialUser[]{child1, child2, child3}));
|
||||
sc.setUsers(Arrays.asList(new SocialUser[]{root, child1, child2, child3, child21, child31, child311, child211}));
|
||||
return sc;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void greedyAlgorithmTest() {
|
||||
GreedyAlgorithm ga = new GreedyAlgorithm(prepareNetwork());
|
||||
assertEquals(ga.findMostFollowersPath("root"), 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nongreedyAlgorithmTest() {
|
||||
NonGreedyAlgorithm nga = new NonGreedyAlgorithm(prepareNetwork(), 0);
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> {
|
||||
nga.findMostFollowersPath("root");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nongreedyAlgorithmUnboundedTest() {
|
||||
SocialConnector sc = prepareNetwork();
|
||||
sc.switchCounter();
|
||||
NonGreedyAlgorithm nga = new NonGreedyAlgorithm(sc, 0);
|
||||
assertEquals(nga.findMostFollowersPath("root"), 6);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.algorithms.kruskal;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import com.google.common.graph.MutableValueGraph;
|
||||
import com.google.common.graph.ValueGraph;
|
||||
import com.google.common.graph.ValueGraphBuilder;
|
||||
import com.baeldung.algorithms.kruskal.Kruskal;
|
||||
|
||||
public class KruskalUnitTest {
|
||||
|
||||
private MutableValueGraph<Integer, Double> graph;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
graph = ValueGraphBuilder.undirected().build();
|
||||
graph.putEdgeValue(0, 1, 8.0);
|
||||
graph.putEdgeValue(0, 2, 5.0);
|
||||
graph.putEdgeValue(1, 2, 9.0);
|
||||
graph.putEdgeValue(1, 3, 11.0);
|
||||
graph.putEdgeValue(2, 3, 15.0);
|
||||
graph.putEdgeValue(2, 4, 10.0);
|
||||
graph.putEdgeValue(3, 4, 7.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGraph_whenMinimumSpanningTree_thenOutputCorrectResult() {
|
||||
final Kruskal kruskal = new Kruskal();
|
||||
ValueGraph<Integer, Double> spanningTree = kruskal.minSpanningTree(graph);
|
||||
|
||||
assertTrue(spanningTree.hasEdgeConnecting(0, 1));
|
||||
assertTrue(spanningTree.hasEdgeConnecting(0, 2));
|
||||
assertTrue(spanningTree.hasEdgeConnecting(2, 4));
|
||||
assertTrue(spanningTree.hasEdgeConnecting(3, 4));
|
||||
assertEquals(graph.edgeValue(0, 1), spanningTree.edgeValue(0, 1));
|
||||
assertEquals(graph.edgeValue(0, 2), spanningTree.edgeValue(0, 2));
|
||||
assertEquals(graph.edgeValue(2, 4), spanningTree.edgeValue(2, 4));
|
||||
assertEquals(graph.edgeValue(3, 4), spanningTree.edgeValue(3, 4));
|
||||
|
||||
assertFalse(spanningTree.hasEdgeConnecting(1, 2));
|
||||
assertFalse(spanningTree.hasEdgeConnecting(1, 3));
|
||||
assertFalse(spanningTree.hasEdgeConnecting(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGraph_whenMaximumSpanningTree_thenOutputCorrectResult() {
|
||||
final Kruskal kruskal = new Kruskal();
|
||||
ValueGraph<Integer, Double> spanningTree = kruskal.maxSpanningTree(graph);
|
||||
|
||||
assertTrue(spanningTree.hasEdgeConnecting(0, 1));
|
||||
assertTrue(spanningTree.hasEdgeConnecting(1, 3));
|
||||
assertTrue(spanningTree.hasEdgeConnecting(2, 3));
|
||||
assertTrue(spanningTree.hasEdgeConnecting(2, 4));
|
||||
assertEquals(graph.edgeValue(0, 1), spanningTree.edgeValue(0, 1));
|
||||
assertEquals(graph.edgeValue(1, 3), spanningTree.edgeValue(1, 3));
|
||||
assertEquals(graph.edgeValue(2, 3), spanningTree.edgeValue(2, 3));
|
||||
assertEquals(graph.edgeValue(2, 4), spanningTree.edgeValue(2, 4));
|
||||
|
||||
assertFalse(spanningTree.hasEdgeConnecting(0, 2));
|
||||
assertFalse(spanningTree.hasEdgeConnecting(1, 2));
|
||||
assertFalse(spanningTree.hasEdgeConnecting(3, 4));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class LinkedListReversalUnitTest {
|
||||
@Test
|
||||
public void givenLinkedList_whenIterativeReverse_thenOutputCorrectResult() {
|
||||
ListNode head = constructLinkedList();
|
||||
ListNode node = head;
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
assertNotNull(node);
|
||||
assertEquals(i, node.getData());
|
||||
node = node.getNext();
|
||||
}
|
||||
LinkedListReversal reversal = new LinkedListReversal();
|
||||
node = reversal.reverseList(head);
|
||||
for (int i = 5; i >= 1; i--) {
|
||||
assertNotNull(node);
|
||||
assertEquals(i, node.getData());
|
||||
node = node.getNext();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedList_whenRecursiveReverse_thenOutputCorrectResult() {
|
||||
ListNode head = constructLinkedList();
|
||||
ListNode node = head;
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
assertNotNull(node);
|
||||
assertEquals(i, node.getData());
|
||||
node = node.getNext();
|
||||
}
|
||||
LinkedListReversal reversal = new LinkedListReversal();
|
||||
node = reversal.reverseListRecursive(head);
|
||||
for (int i = 5; i >= 1; i--) {
|
||||
assertNotNull(node);
|
||||
assertEquals(i, node.getData());
|
||||
node = node.getNext();
|
||||
}
|
||||
}
|
||||
|
||||
private ListNode constructLinkedList() {
|
||||
ListNode head = null;
|
||||
ListNode tail = null;
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
ListNode node = new ListNode(i);
|
||||
if (head == null) {
|
||||
head = node;
|
||||
} else {
|
||||
tail.setNext(node);
|
||||
}
|
||||
tail = node;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MinHeapUnitTest {
|
||||
|
||||
private final int[][] inputArray = { { 0, 6 }, { 1, 5, 10, 100 }, { 2, 4, 200, 650 } };
|
||||
private final int[] expectedArray = { 0, 1, 2, 4, 5, 6, 10, 100, 200, 650 };
|
||||
|
||||
@Test
|
||||
public void givenSortedArrays_whenMerged_thenShouldReturnASingleSortedarray() {
|
||||
int[] resultArray = MinHeap.merge(inputArray);
|
||||
|
||||
assertThat(resultArray.length, is(equalTo(10)));
|
||||
assertThat(resultArray, is(equalTo(expectedArray)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.algorithms.topkelements;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Java6Assertions.assertThat;
|
||||
|
||||
public class TopKElementsFinderUnitTest {
|
||||
private final TopKElementsFinder<Integer> bruteForceFinder = new BruteForceTopKElementsFinder();
|
||||
private final TopKElementsFinder<Integer> maxHeapFinder = new MaxHeapTopKElementsFinder();
|
||||
private final TopKElementsFinder<Integer> treeSetFinder = new TreeSetTopKElementsFinder();
|
||||
|
||||
private final int k = 4;
|
||||
private final List<Integer> distinctIntegers = Arrays.asList(1, 2, 3, 9, 7, 6, 12);
|
||||
private final List<Integer> distinctIntegersTopK = Arrays.asList(9, 7, 6, 12);
|
||||
private final List<Integer> nonDistinctIntegers = Arrays.asList(1, 2, 3, 3, 9, 9, 7, 6, 12);
|
||||
private final List<Integer> nonDistinctIntegersTopK = Arrays.asList(9, 9, 7, 12);
|
||||
|
||||
|
||||
@Test
|
||||
public void givenArrayDistinctIntegers_whenBruteForceFindTopK_thenReturnKLargest() {
|
||||
assertThat(bruteForceFinder.findTopK(distinctIntegers, k)).containsOnlyElementsOf(distinctIntegersTopK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayDistinctIntegers_whenMaxHeapFindTopK_thenReturnKLargest() {
|
||||
assertThat(maxHeapFinder.findTopK(distinctIntegers, k)).containsOnlyElementsOf(distinctIntegersTopK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayDistinctIntegers_whenTreeSetFindTopK_thenReturnKLargest() {
|
||||
assertThat(treeSetFinder.findTopK(distinctIntegers, k)).containsOnlyElementsOf(distinctIntegersTopK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayNonDistinctIntegers_whenBruteForceFindTopK_thenReturnKLargest() {
|
||||
assertThat(bruteForceFinder.findTopK(nonDistinctIntegers, k)).containsOnlyElementsOf(nonDistinctIntegersTopK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayNonDistinctIntegers_whenMaxHeapFindTopK_thenReturnKLargest() {
|
||||
assertThat(maxHeapFinder.findTopK(nonDistinctIntegers, k)).containsOnlyElementsOf(nonDistinctIntegersTopK);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user