JAVA-12097: renamed algorithms-module to algorithms-modules
This commit is contained in:
13
algorithms-modules/algorithms-miscellaneous-1/README.md
Normal file
13
algorithms-modules/algorithms-miscellaneous-1/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
## Algorithms - Miscellaneous
|
||||
|
||||
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
|
||||
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Validating Input With Finite Automata in Java](https://www.baeldung.com/java-finite-automata)
|
||||
- [Example of Hill Climbing Algorithm in Java](https://www.baeldung.com/java-hill-climbing-algorithm)
|
||||
- [Introduction to Minimax Algorithm with a Java Implementation](https://www.baeldung.com/java-minimax-algorithm)
|
||||
- [How to Calculate Levenshtein Distance in Java?](https://www.baeldung.com/java-levenshtein-distance)
|
||||
- [How to Find the Kth Largest Element in Java](https://www.baeldung.com/java-kth-largest-element)
|
||||
- More articles: [[next -->]](/algorithms-miscellaneous-2)
|
||||
72
algorithms-modules/algorithms-miscellaneous-1/pom.xml
Normal file
72
algorithms-modules/algorithms-miscellaneous-1/pom.xml
Normal file
@@ -0,0 +1,72 @@
|
||||
<?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-1</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-miscellaneous-1</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>algorithms-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.dpaukov</groupId>
|
||||
<artifactId>combinatoricslib3</artifactId>
|
||||
<version>${combinatoricslib3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>cobertura-maven-plugin</artifactId>
|
||||
<version>${cobertura.plugin.version}</version>
|
||||
<configuration>
|
||||
<instrumentation>
|
||||
<ignores>
|
||||
<ignore>com/baeldung/algorithms/dijkstra/*</ignore>
|
||||
</ignores>
|
||||
<excludes>
|
||||
<exclude>com/baeldung/algorithms/dijkstra/*</exclude>
|
||||
</excludes>
|
||||
</instrumentation>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
<properties>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<combinatoricslib3.version>3.3.0</combinatoricslib3.version>
|
||||
<cobertura.plugin.version>2.7</cobertura.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
/**
|
||||
* Finite state machine.
|
||||
*/
|
||||
public interface FiniteStateMachine {
|
||||
|
||||
/**
|
||||
* Follow a transition, switch the state of the machine.
|
||||
* @param c Char.
|
||||
* @return A new finite state machine with the new state.
|
||||
*/
|
||||
FiniteStateMachine switchState(final CharSequence c);
|
||||
|
||||
/**
|
||||
* Is the current state a final one?
|
||||
* @return true or false.
|
||||
*/
|
||||
boolean canStop();
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
/**
|
||||
* Default implementation of a finite state machine.
|
||||
* This class is immutable and thread-safe.
|
||||
*/
|
||||
public final class RtFiniteStateMachine implements FiniteStateMachine {
|
||||
|
||||
/**
|
||||
* Current state.
|
||||
*/
|
||||
private State current;
|
||||
|
||||
/**
|
||||
* Ctor.
|
||||
* @param initial Initial state of this machine.
|
||||
*/
|
||||
public RtFiniteStateMachine(final State initial) {
|
||||
this.current = initial;
|
||||
}
|
||||
|
||||
public FiniteStateMachine switchState(final CharSequence c) {
|
||||
return new RtFiniteStateMachine(this.current.transit(c));
|
||||
}
|
||||
|
||||
public boolean canStop() {
|
||||
return this.current.isFinal();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* State in a finite state machine.
|
||||
*/
|
||||
public final class RtState implements State {
|
||||
|
||||
private List<Transition> transitions;
|
||||
private boolean isFinal;
|
||||
|
||||
public RtState() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public RtState(final boolean isFinal) {
|
||||
this.transitions = new ArrayList<>();
|
||||
this.isFinal = isFinal;
|
||||
}
|
||||
|
||||
public State transit(final CharSequence c) {
|
||||
return transitions
|
||||
.stream()
|
||||
.filter(t -> t.isPossible(c))
|
||||
.map(Transition::state)
|
||||
.findAny()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Input not accepted: " + c));
|
||||
}
|
||||
|
||||
public boolean isFinal() {
|
||||
return this.isFinal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public State with(Transition tr) {
|
||||
this.transitions.add(tr);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
|
||||
/**
|
||||
* Transition in finite state machine.
|
||||
*/
|
||||
public final class RtTransition implements Transition {
|
||||
|
||||
private String rule;
|
||||
private State next;
|
||||
|
||||
/**
|
||||
* Ctor.
|
||||
* @param rule Rule that a character has to meet
|
||||
* in order to get to the next state.
|
||||
* @param next Next state.
|
||||
*/
|
||||
public RtTransition (String rule, State next) {
|
||||
this.rule = rule;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public State state() {
|
||||
return this.next;
|
||||
}
|
||||
|
||||
public boolean isPossible(CharSequence c) {
|
||||
return this.rule.equalsIgnoreCase(String.valueOf(c));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
/**
|
||||
* State. Part of a finite state machine.
|
||||
*/
|
||||
public interface State {
|
||||
|
||||
/**
|
||||
* Add a Transition to this state.
|
||||
* @param tr Given transition.
|
||||
* @return Modified State.
|
||||
*/
|
||||
State with(final Transition tr);
|
||||
|
||||
/**
|
||||
* Follow one of the transitions, to get
|
||||
* to the next state.
|
||||
* @param c Character.
|
||||
* @return State.
|
||||
* @throws IllegalStateException if the char is not accepted.
|
||||
*/
|
||||
State transit(final CharSequence c);
|
||||
|
||||
/**
|
||||
* Can the automaton stop on this state?
|
||||
* @return true or false
|
||||
*/
|
||||
boolean isFinal();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.automata;
|
||||
|
||||
/**
|
||||
* Transition in a finite State machine.
|
||||
*/
|
||||
public interface Transition {
|
||||
|
||||
/**
|
||||
* Is the transition possible with the given character?
|
||||
* @param c char.
|
||||
* @return true or false.
|
||||
*/
|
||||
boolean isPossible(final CharSequence c);
|
||||
|
||||
/**
|
||||
* The state to which this transition leads.
|
||||
* @return State.
|
||||
*/
|
||||
State state();
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package com.baeldung.algorithms.hillclimbing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Stack;
|
||||
|
||||
public class HillClimbing {
|
||||
public static void main(String[] args) {
|
||||
HillClimbing hillClimbing = new HillClimbing();
|
||||
String blockArr[] = { "B", "C", "D", "A" };
|
||||
Stack<String> startState = hillClimbing.getStackWithValues(blockArr);
|
||||
String goalBlockArr[] = { "A", "B", "C", "D" };
|
||||
Stack<String> goalState = hillClimbing.getStackWithValues(goalBlockArr);
|
||||
try {
|
||||
List<State> solutionSequence = hillClimbing.getRouteWithHillClimbing(startState, goalState);
|
||||
solutionSequence.forEach(HillClimbing::printEachStep);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printEachStep(State state) {
|
||||
List<Stack<String>> stackList = state.getState();
|
||||
System.out.println("----------------");
|
||||
stackList.forEach(stack -> {
|
||||
while (!stack.isEmpty()) {
|
||||
System.out.println(stack.pop());
|
||||
}
|
||||
System.out.println(" ");
|
||||
});
|
||||
}
|
||||
|
||||
private Stack<String> getStackWithValues(String[] blocks) {
|
||||
Stack<String> stack = new Stack<>();
|
||||
for (String block : blocks)
|
||||
stack.push(block);
|
||||
return stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method prepares path from init state to goal state
|
||||
*/
|
||||
public List<State> getRouteWithHillClimbing(Stack<String> initStateStack, Stack<String> goalStateStack) throws Exception {
|
||||
List<Stack<String>> initStateStackList = new ArrayList<>();
|
||||
initStateStackList.add(initStateStack);
|
||||
int initStateHeuristics = getHeuristicsValue(initStateStackList, goalStateStack);
|
||||
State initState = new State(initStateStackList, initStateHeuristics);
|
||||
|
||||
List<State> resultPath = new ArrayList<>();
|
||||
resultPath.add(new State(initState));
|
||||
|
||||
State currentState = initState;
|
||||
boolean noStateFound = false;
|
||||
while (!currentState.getState()
|
||||
.get(0)
|
||||
.equals(goalStateStack) || noStateFound) {
|
||||
noStateFound = true;
|
||||
State nextState = findNextState(currentState, goalStateStack);
|
||||
if (nextState != null) {
|
||||
noStateFound = false;
|
||||
currentState = nextState;
|
||||
resultPath.add(new State(nextState));
|
||||
}
|
||||
}
|
||||
|
||||
return resultPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method finds new state from current state based on goal and
|
||||
* heuristics
|
||||
*/
|
||||
public State findNextState(State currentState, Stack<String> goalStateStack) {
|
||||
List<Stack<String>> listOfStacks = currentState.getState();
|
||||
int currentStateHeuristics = currentState.getHeuristics();
|
||||
|
||||
return listOfStacks.stream()
|
||||
.map(stack -> {
|
||||
return applyOperationsOnState(listOfStacks, stack, currentStateHeuristics, goalStateStack);
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method applies operations on the current state to get a new state
|
||||
*/
|
||||
public State applyOperationsOnState(List<Stack<String>> listOfStacks, Stack<String> stack, int currentStateHeuristics, Stack<String> goalStateStack) {
|
||||
State tempState;
|
||||
List<Stack<String>> tempStackList = new ArrayList<>(listOfStacks);
|
||||
String block = stack.pop();
|
||||
if (stack.size() == 0)
|
||||
tempStackList.remove(stack);
|
||||
tempState = pushElementToNewStack(tempStackList, block, currentStateHeuristics, goalStateStack);
|
||||
if (tempState == null) {
|
||||
tempState = pushElementToExistingStacks(stack, tempStackList, block, currentStateHeuristics, goalStateStack);
|
||||
}
|
||||
if (tempState == null)
|
||||
stack.push(block);
|
||||
return tempState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation to be applied on a state in order to find new states. This
|
||||
* operation pushes an element into a new stack
|
||||
*/
|
||||
private State pushElementToNewStack(List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
|
||||
State newState = null;
|
||||
Stack<String> newStack = new Stack<>();
|
||||
newStack.push(block);
|
||||
|
||||
currentStackList.add(newStack);
|
||||
int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack);
|
||||
if (newStateHeuristics > currentStateHeuristics) {
|
||||
newState = new State(currentStackList, newStateHeuristics);
|
||||
} else {
|
||||
currentStackList.remove(newStack);
|
||||
}
|
||||
return newState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation to be applied on a state in order to find new states. This
|
||||
* operation pushes an element into one of the other stacks to explore new
|
||||
* states
|
||||
*/
|
||||
private State pushElementToExistingStacks(Stack currentStack, List<Stack<String>> currentStackList, String block, int currentStateHeuristics, Stack<String> goalStateStack) {
|
||||
|
||||
Optional<State> newState = currentStackList.stream()
|
||||
.filter(stack -> stack != currentStack)
|
||||
.map(stack -> {
|
||||
return pushElementToStack(stack, block, currentStackList, currentStateHeuristics, goalStateStack);
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.findFirst();
|
||||
|
||||
return newState.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method pushes a block to the stack and returns new state if its closer to goal
|
||||
*/
|
||||
private State pushElementToStack(Stack stack, String block, List<Stack<String>> currentStackList, int currentStateHeuristics, Stack<String> goalStateStack) {
|
||||
stack.push(block);
|
||||
int newStateHeuristics = getHeuristicsValue(currentStackList, goalStateStack);
|
||||
if (newStateHeuristics > currentStateHeuristics) {
|
||||
return new State(currentStackList, newStateHeuristics);
|
||||
}
|
||||
stack.pop();
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns heuristics value for given state with respect to goal
|
||||
* state
|
||||
*/
|
||||
public int getHeuristicsValue(List<Stack<String>> currentState, Stack<String> goalStateStack) {
|
||||
Integer heuristicValue;
|
||||
heuristicValue = currentState.stream()
|
||||
.mapToInt(stack -> {
|
||||
return getHeuristicsValueForStack(stack, currentState, goalStateStack);
|
||||
})
|
||||
.sum();
|
||||
return heuristicValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns heuristics value for a particular stack
|
||||
*/
|
||||
public int getHeuristicsValueForStack(Stack<String> stack, List<Stack<String>> currentState, Stack<String> goalStateStack) {
|
||||
int stackHeuristics = 0;
|
||||
boolean isPositioneCorrect = true;
|
||||
int goalStartIndex = 0;
|
||||
for (String currentBlock : stack) {
|
||||
if (isPositioneCorrect && currentBlock.equals(goalStateStack.get(goalStartIndex))) {
|
||||
stackHeuristics += goalStartIndex;
|
||||
} else {
|
||||
stackHeuristics -= goalStartIndex;
|
||||
isPositioneCorrect = false;
|
||||
}
|
||||
goalStartIndex++;
|
||||
}
|
||||
return stackHeuristics;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.algorithms.hillclimbing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
public class State {
|
||||
private List<Stack<String>> state;
|
||||
private int heuristics;
|
||||
|
||||
public State(List<Stack<String>> state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
State(List<Stack<String>> state, int heuristics) {
|
||||
this.state = state;
|
||||
this.heuristics = heuristics;
|
||||
}
|
||||
|
||||
State(State state) {
|
||||
if (state != null) {
|
||||
this.state = new ArrayList<>();
|
||||
for (Stack s : state.getState()) {
|
||||
Stack s1;
|
||||
s1 = (Stack) s.clone();
|
||||
this.state.add(s1);
|
||||
}
|
||||
this.heuristics = state.getHeuristics();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Stack<String>> getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public int getHeuristics() {
|
||||
return heuristics;
|
||||
}
|
||||
|
||||
public void setHeuristics(int heuristics) {
|
||||
this.heuristics = heuristics;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.baeldung.algorithms.kthlargest;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class FindKthLargest {
|
||||
|
||||
public int findKthLargestBySorting(Integer[] arr, int k) {
|
||||
Arrays.sort(arr);
|
||||
int targetIndex = arr.length - k;
|
||||
return arr[targetIndex];
|
||||
}
|
||||
|
||||
public int findKthLargestBySortingDesc(Integer[] arr, int k) {
|
||||
Arrays.sort(arr, Collections.reverseOrder());
|
||||
return arr[k - 1];
|
||||
}
|
||||
|
||||
public int findKthElementByQuickSelect(Integer[] arr, int left, int right, int k) {
|
||||
if (k >= 0 && k <= right - left + 1) {
|
||||
int pos = partition(arr, left, right);
|
||||
if (pos - left == k) {
|
||||
return arr[pos];
|
||||
}
|
||||
if (pos - left > k) {
|
||||
return findKthElementByQuickSelect(arr, left, pos - 1, k);
|
||||
}
|
||||
return findKthElementByQuickSelect(arr, pos + 1, right, k - pos + left - 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int findKthElementByQuickSelectWithIterativePartition(Integer[] arr, int left, int right, int k) {
|
||||
if (k >= 0 && k <= right - left + 1) {
|
||||
int pos = partitionIterative(arr, left, right);
|
||||
if (pos - left == k) {
|
||||
return arr[pos];
|
||||
}
|
||||
if (pos - left > k) {
|
||||
return findKthElementByQuickSelectWithIterativePartition(arr, left, pos - 1, k);
|
||||
}
|
||||
return findKthElementByQuickSelectWithIterativePartition(arr, pos + 1, right, k - pos + left - 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int partition(Integer[] arr, int left, int right) {
|
||||
int pivot = arr[right];
|
||||
Integer[] leftArr;
|
||||
Integer[] rightArr;
|
||||
|
||||
leftArr = IntStream.range(left, right)
|
||||
.filter(i -> arr[i] < pivot)
|
||||
.map(i -> arr[i])
|
||||
.boxed()
|
||||
.toArray(Integer[]::new);
|
||||
|
||||
rightArr = IntStream.range(left, right)
|
||||
.filter(i -> arr[i] > pivot)
|
||||
.map(i -> arr[i])
|
||||
.boxed()
|
||||
.toArray(Integer[]::new);
|
||||
|
||||
int leftArraySize = leftArr.length;
|
||||
System.arraycopy(leftArr, 0, arr, left, leftArraySize);
|
||||
arr[leftArraySize + left] = pivot;
|
||||
System.arraycopy(rightArr, 0, arr, left + leftArraySize + 1, rightArr.length);
|
||||
|
||||
return left + leftArraySize;
|
||||
}
|
||||
|
||||
private int partitionIterative(Integer[] arr, int left, int right) {
|
||||
int pivot = arr[right], i = left;
|
||||
for (int j = left; j <= right - 1; j++) {
|
||||
if (arr[j] <= pivot) {
|
||||
swap(arr, i, j);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
swap(arr, i, right);
|
||||
return i;
|
||||
}
|
||||
|
||||
public int findKthElementByRandomizedQuickSelect(Integer[] arr, int left, int right, int k) {
|
||||
if (k >= 0 && k <= right - left + 1) {
|
||||
int pos = randomPartition(arr, left, right);
|
||||
if (pos - left == k) {
|
||||
return arr[pos];
|
||||
}
|
||||
if (pos - left > k) {
|
||||
return findKthElementByRandomizedQuickSelect(arr, left, pos - 1, k);
|
||||
}
|
||||
return findKthElementByRandomizedQuickSelect(arr, pos + 1, right, k - pos + left - 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int randomPartition(Integer arr[], int left, int right) {
|
||||
int n = right - left + 1;
|
||||
int pivot = (int) (Math.random() * n);
|
||||
swap(arr, left + pivot, right);
|
||||
return partition(arr, left, right);
|
||||
}
|
||||
|
||||
private void swap(Integer[] arr, int n1, int n2) {
|
||||
int temp = arr[n2];
|
||||
arr[n2] = arr[n1];
|
||||
arr[n1] = temp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
class GameOfBones {
|
||||
static List<Integer> getPossibleStates(int noOfBonesInHeap) {
|
||||
return IntStream.rangeClosed(1, 3).boxed()
|
||||
.map(i -> noOfBonesInHeap - i)
|
||||
.filter(newHeapCount -> newHeapCount >= 0)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MiniMax {
|
||||
private Tree tree;
|
||||
|
||||
public Tree getTree() {
|
||||
return tree;
|
||||
}
|
||||
|
||||
public void constructTree(int noOfBones) {
|
||||
tree = new Tree();
|
||||
Node root = new Node(noOfBones, true);
|
||||
tree.setRoot(root);
|
||||
constructTree(root);
|
||||
}
|
||||
|
||||
private void constructTree(Node parentNode) {
|
||||
List<Integer> listofPossibleHeaps = GameOfBones.getPossibleStates(parentNode.getNoOfBones());
|
||||
boolean isChildMaxPlayer = !parentNode.isMaxPlayer();
|
||||
listofPossibleHeaps.forEach(n -> {
|
||||
Node newNode = new Node(n, isChildMaxPlayer);
|
||||
parentNode.addChild(newNode);
|
||||
if (newNode.getNoOfBones() > 0) {
|
||||
constructTree(newNode);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean checkWin() {
|
||||
Node root = tree.getRoot();
|
||||
checkWin(root);
|
||||
return root.getScore() == 1;
|
||||
}
|
||||
|
||||
private void checkWin(Node node) {
|
||||
List<Node> children = node.getChildren();
|
||||
boolean isMaxPlayer = node.isMaxPlayer();
|
||||
children.forEach(child -> {
|
||||
if (child.getNoOfBones() == 0) {
|
||||
child.setScore(isMaxPlayer ? 1 : -1);
|
||||
} else {
|
||||
checkWin(child);
|
||||
}
|
||||
});
|
||||
Node bestChild = findBestChild(isMaxPlayer, children);
|
||||
node.setScore(bestChild.getScore());
|
||||
}
|
||||
|
||||
private Node findBestChild(boolean isMaxPlayer, List<Node> children) {
|
||||
Comparator<Node> byScoreComparator = Comparator.comparing(Node::getScore);
|
||||
|
||||
return children.stream()
|
||||
.max(isMaxPlayer ? byScoreComparator : byScoreComparator.reversed())
|
||||
.orElseThrow(NoSuchElementException::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Node {
|
||||
private int noOfBones;
|
||||
private boolean isMaxPlayer;
|
||||
private int score;
|
||||
private List<Node> children;
|
||||
|
||||
public Node(int noOfBones, boolean isMaxPlayer) {
|
||||
this.noOfBones = noOfBones;
|
||||
this.isMaxPlayer = isMaxPlayer;
|
||||
children = new ArrayList<>();
|
||||
}
|
||||
|
||||
int getNoOfBones() {
|
||||
return noOfBones;
|
||||
}
|
||||
|
||||
boolean isMaxPlayer() {
|
||||
return isMaxPlayer;
|
||||
}
|
||||
|
||||
int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
List<Node> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
void addChild(Node newNode) {
|
||||
children.add(newNode);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
public class Tree {
|
||||
private Node root;
|
||||
|
||||
Tree() {
|
||||
}
|
||||
|
||||
Node getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
void setRoot(Node root) {
|
||||
this.root = root;
|
||||
}
|
||||
}
|
||||
@@ -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,58 @@
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
import com.baeldung.algorithms.hillclimbing.HillClimbing;
|
||||
import com.baeldung.algorithms.hillclimbing.State;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class HillClimbingAlgorithmUnitTest {
|
||||
private Stack<String> initStack;
|
||||
private Stack<String> goalStack;
|
||||
|
||||
@Before
|
||||
public void initStacks() {
|
||||
String blockArr[] = { "B", "C", "D", "A" };
|
||||
String goalBlockArr[] = { "A", "B", "C", "D" };
|
||||
initStack = new Stack<>();
|
||||
for (String block : blockArr)
|
||||
initStack.push(block);
|
||||
goalStack = new Stack<>();
|
||||
for (String block : goalBlockArr)
|
||||
goalStack.push(block);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInitAndGoalState_whenGetPathWithHillClimbing_thenPathFound() {
|
||||
HillClimbing hillClimbing = new HillClimbing();
|
||||
|
||||
List<State> path;
|
||||
try {
|
||||
path = hillClimbing.getRouteWithHillClimbing(initStack, goalStack);
|
||||
assertNotNull(path);
|
||||
assertEquals(path.get(path.size() - 1)
|
||||
.getState()
|
||||
.get(0), goalStack);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
|
||||
HillClimbing hillClimbing = new HillClimbing();
|
||||
List<Stack<String>> initList = new ArrayList<>();
|
||||
initList.add(initStack);
|
||||
State currentState = new State(initList);
|
||||
currentState.setHeuristics(hillClimbing.getHeuristicsValue(initList, goalStack));
|
||||
State nextState = hillClimbing.findNextState(currentState, goalStack);
|
||||
assertTrue(nextState.getHeuristics() > currentState.getHeuristics());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
import com.baeldung.algorithms.automata.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public final class RtFiniteStateMachineLongRunningUnitTest {
|
||||
|
||||
@Test
|
||||
public void acceptsSimplePair() {
|
||||
String json = "{\"key\":\"value\"}";
|
||||
FiniteStateMachine machine = this.buildJsonStateMachine();
|
||||
for (int i = 0; i < json.length(); i++) {
|
||||
machine = machine.switchState(String.valueOf(json.charAt(i)));
|
||||
}
|
||||
assertTrue(machine.canStop());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptsMorePairs() {
|
||||
String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
|
||||
FiniteStateMachine machine = this.buildJsonStateMachine();
|
||||
for (int i = 0; i < json.length(); i++) {
|
||||
machine = machine.switchState(String.valueOf(json.charAt(i)));
|
||||
}
|
||||
assertTrue(machine.canStop());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void missingColon() {
|
||||
String json = "{\"key\"\"value\"}";
|
||||
FiniteStateMachine machine = this.buildJsonStateMachine();
|
||||
for (int i = 0; i < json.length(); i++) {
|
||||
machine = machine.switchState(String.valueOf(json.charAt(i)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a finite state machine to validate a simple
|
||||
* Json object.
|
||||
* @return
|
||||
*/
|
||||
private FiniteStateMachine buildJsonStateMachine() {
|
||||
State first = new RtState();
|
||||
State second = new RtState();
|
||||
State third = new RtState();
|
||||
State fourth = new RtState();
|
||||
State fifth = new RtState();
|
||||
State sixth = new RtState();
|
||||
State seventh = new RtState();
|
||||
State eighth = new RtState(true);
|
||||
|
||||
first.with(new RtTransition("{", second));
|
||||
second.with(new RtTransition("\"", third));
|
||||
//Add transitions with chars 0-9 and a-z
|
||||
for (int i = 0; i < 26; i++) {
|
||||
if (i < 10) {
|
||||
third = third.with(new RtTransition(String.valueOf(i), third));
|
||||
sixth = sixth.with(new RtTransition(String.valueOf(i), sixth));
|
||||
}
|
||||
third = third.with(new RtTransition(String.valueOf((char) ('a' + i)), third));
|
||||
sixth = sixth.with(new RtTransition(String.valueOf((char) ('a' + i)), sixth));
|
||||
}
|
||||
third.with(new RtTransition("\"", fourth));
|
||||
fourth.with(new RtTransition(":", fifth));
|
||||
fifth.with(new RtTransition("\"", sixth));
|
||||
sixth.with(new RtTransition("\"", seventh));
|
||||
seventh.with(new RtTransition(",", second));
|
||||
seventh.with(new RtTransition("}", eighth));
|
||||
return new RtFiniteStateMachine(first);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.algorithms.kthlargest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FindKthLargestUnitTest {
|
||||
|
||||
private FindKthLargest findKthLargest;
|
||||
private Integer[] arr = { 3, 7, 1, 2, 8, 10, 4, 5, 6, 9 };
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
findKthLargest = new FindKthLargest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntArray_whenFindKthLargestBySorting_thenGetResult() {
|
||||
int k = 3;
|
||||
assertThat(findKthLargest.findKthLargestBySorting(arr, k)).isEqualTo(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntArray_whenFindKthLargestBySortingDesc_thenGetResult() {
|
||||
int k = 3;
|
||||
assertThat(findKthLargest.findKthLargestBySortingDesc(arr, k)).isEqualTo(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntArray_whenFindKthLargestByQuickSelect_thenGetResult() {
|
||||
int k = 3;
|
||||
int kthLargest = arr.length - k;
|
||||
assertThat(findKthLargest.findKthElementByQuickSelect(arr, 0, arr.length - 1, kthLargest)).isEqualTo(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntArray_whenFindKthElementByQuickSelectIterative_thenGetResult() {
|
||||
int k = 3;
|
||||
int kthLargest = arr.length - k;
|
||||
assertThat(findKthLargest.findKthElementByQuickSelectWithIterativePartition(arr, 0, arr.length - 1, kthLargest)).isEqualTo(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntArray_whenFindKthSmallestByQuickSelect_thenGetResult() {
|
||||
int k = 3;
|
||||
assertThat(findKthLargest.findKthElementByQuickSelect(arr, 0, arr.length - 1, k - 1)).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntArray_whenFindKthLargestByRandomizedQuickSelect_thenGetResult() {
|
||||
int k = 3;
|
||||
int kthLargest = arr.length - k;
|
||||
assertThat(findKthLargest.findKthElementByRandomizedQuickSelect(arr, 0, arr.length - 1, kthLargest)).isEqualTo(8);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import com.baeldung.algorithms.minimax.MiniMax;
|
||||
import com.baeldung.algorithms.minimax.Tree;
|
||||
|
||||
public class MinimaxUnitTest {
|
||||
private Tree gameTree;
|
||||
private MiniMax miniMax;
|
||||
|
||||
@Before
|
||||
public void initMiniMaxUtility() {
|
||||
miniMax = new MiniMax();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMiniMax_whenConstructTree_thenNotNullTree() {
|
||||
assertNull(gameTree);
|
||||
miniMax.constructTree(6);
|
||||
gameTree = miniMax.getTree();
|
||||
assertNotNull(gameTree);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMiniMax_whenCheckWin_thenComputeOptimal() {
|
||||
miniMax.constructTree(6);
|
||||
boolean result = miniMax.checkWin();
|
||||
assertTrue(result);
|
||||
miniMax.constructTree(8);
|
||||
result = miniMax.checkWin();
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user