Merge branch 'master' of https://github.com/eugenp/tutorials
Conflicts: spring-websockets/src/main/resources/public/index.html
This commit is contained in:
+6
-6
@@ -73,8 +73,6 @@ ninja/devDb.mv.db
|
||||
**/out-tsc
|
||||
**/nbproject/
|
||||
**/nb-configuration.xml
|
||||
core-scala/.cache-main
|
||||
core-scala/.cache-tests
|
||||
|
||||
|
||||
persistence-modules/hibernate5/transaction.log
|
||||
@@ -87,7 +85,9 @@ transaction.log
|
||||
*-shell.log
|
||||
|
||||
apache-cxf/cxf-aegis/baeldung.xml
|
||||
apache-fop/src/test/resources/input.xml
|
||||
apache-fop/src/test/resources/output_herold.pdf
|
||||
apache-fop/src/test/resources/output_html2fo.pdf
|
||||
apache-fop/src/test/resources/output_jtidy.pdf
|
||||
testing-modules/report-*.json
|
||||
|
||||
libraries-2/*.db
|
||||
|
||||
# SDKMan
|
||||
.sdkmanrc
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
**UPDATE**: The price of "Learn Spring Security OAuth" will permanently change on the 11th of December, along with the upcoming OAuth2 material: http://bit.ly/github-lss
|
||||
|
||||
The Courses
|
||||
==============================
|
||||
|
||||
@@ -22,10 +20,38 @@ This project is **a collection of small and focused tutorials** - each covering
|
||||
A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Security.
|
||||
In additional to Spring, the modules here are covering a number of aspects in Java.
|
||||
|
||||
Profile based segregation
|
||||
====================
|
||||
|
||||
We are using maven build profiles to segregate the huge list of individual projects we have in our repository.
|
||||
|
||||
The projects are broadly divided into 3 list: first, second and heavy.
|
||||
|
||||
Next, they are segregated further on the basis of tests that we want to execute.
|
||||
|
||||
Therefore, we have a total of 6 profiles:
|
||||
|
||||
| Profile | Includes | Type of test enabled |
|
||||
| ----------------------- | --------------------------- | -------------------- |
|
||||
| default-first | First set of projects | *UnitTest |
|
||||
| integration-lite-first | First set of projects | *IntegrationTest |
|
||||
| default-second | Second set of projects | *UnitTest |
|
||||
| integration-lite-second | Second set of projects | *IntegrationTest |
|
||||
| default-heavy | Heavy/long running projects | *UnitTest |
|
||||
| integration-heavy | Heavy/long running projects | *IntegrationTest |
|
||||
|
||||
Building the project
|
||||
====================
|
||||
To do the full build, do: `mvn clean install`
|
||||
|
||||
Though it should not be needed often to build the entire repository at once because we are usually concerned with a specific module.
|
||||
|
||||
But if we want to, we can invoke the below command from the root of the repository if we want to build the entire repository with only Unit Tests enabled:
|
||||
|
||||
`mvn clean install -Pdefault-first,default-second,default-heavy`
|
||||
|
||||
or if we want to build the entire repository with Integration Tests enabled, we can do:
|
||||
|
||||
`mvn clean install -Pintegration-lite-first,integration-lite-second,integration-heavy`
|
||||
|
||||
|
||||
Building a single module
|
||||
@@ -46,8 +72,18 @@ When you're working with an individual module, there's no need to import all of
|
||||
|
||||
Running Tests
|
||||
=============
|
||||
The command `mvn clean install` will run the unit tests in a module.
|
||||
To run the integration tests, use the command `mvn clean install -Pintegration-lite-first`
|
||||
The command `mvn clean install` from within a module will run the unit tests in that module.
|
||||
For Spring modules this will also run the `SpringContextTest` if present.
|
||||
|
||||
To run the integration tests, use the command:
|
||||
|
||||
`mvn clean install -Pintegration-lite-first` or
|
||||
|
||||
`mvn clean install -Pintegration-lite-second` or
|
||||
|
||||
`mvn clean install -Pintegration-heavy`
|
||||
|
||||
depending on the list where our module exists
|
||||
|
||||
|
||||
|
||||
|
||||
+4
-3
@@ -1,7 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>akka-http</artifactId>
|
||||
<name>akka-http</name>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?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">
|
||||
<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>akka-streams</artifactId>
|
||||
<name>akka-streams</name>
|
||||
|
||||
@@ -5,6 +5,6 @@ This module contains articles about genetic algorithms.
|
||||
### Relevant articles:
|
||||
|
||||
- [Introduction to Jenetics Library](https://www.baeldung.com/jenetics)
|
||||
- [Ant Colony Optimization](https://www.baeldung.com/java-ant-colony-optimization)
|
||||
- [Ant Colony Optimization with a Java Example](https://www.baeldung.com/java-ant-colony-optimization)
|
||||
- [Design a Genetic Algorithm in Java](https://www.baeldung.com/java-genetic-algorithm)
|
||||
- [The Traveling Salesman Problem in Java](https://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?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">
|
||||
<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-genetic</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
@@ -6,8 +6,8 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
### Relevant articles:
|
||||
|
||||
- [Validating Input With Finite Automata in Java](https://www.baeldung.com/java-finite-automata)
|
||||
- [Example of Hill Climbing Algorithm](https://www.baeldung.com/java-hill-climbing-algorithm)
|
||||
- [Introduction to Minimax Algorithm](https://www.baeldung.com/java-minimax-algorithm)
|
||||
- [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)
|
||||
- More articles: [[next -->]](/algorithms-miscellaneous-2)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?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">
|
||||
<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>
|
||||
|
||||
@@ -14,4 +14,4 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
- [Displaying Money Amounts in Words](https://www.baeldung.com/java-money-into-words)
|
||||
- [A Collaborative Filtering Recommendation System in Java](https://www.baeldung.com/java-collaborative-filtering-recommendations)
|
||||
- [Implementing A* Pathfinding in Java](https://www.baeldung.com/java-a-star-pathfinding)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-1) [[next -->]](/../algorithms-miscellaneous-3)
|
||||
- More articles: [[<-- prev]](/algorithms-miscellaneous-1) [[next -->]](/algorithms-miscellaneous-3)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?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">
|
||||
<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-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
+13
-2
@@ -1,16 +1,21 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.jgrapht.ext.JGraphXAdapter;
|
||||
import org.jgrapht.graph.DefaultDirectedGraph;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mxgraph.layout.mxCircleLayout;
|
||||
import com.mxgraph.layout.mxIGraphLayout;
|
||||
import com.mxgraph.util.mxCellRenderer;
|
||||
@@ -20,7 +25,7 @@ public class GraphImageGenerationUnitTest {
|
||||
|
||||
@Before
|
||||
public void createGraph() throws IOException {
|
||||
File imgFile = new File("src/test/resources/graph.png");
|
||||
File imgFile = new File("src/test/resources/graph1.png");
|
||||
imgFile.createNewFile();
|
||||
g = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
|
||||
String x1 = "x1";
|
||||
@@ -34,12 +39,18 @@ public class GraphImageGenerationUnitTest {
|
||||
g.addEdge(x3, x1);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
File imgFile = new File("src/test/resources/graph1.png");
|
||||
imgFile.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAdaptedGraph_whenWriteBufferedImage_ThenFileShouldExist() throws IOException {
|
||||
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<String, DefaultEdge>(g);
|
||||
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
|
||||
layout.execute(graphAdapter.getDefaultParent());
|
||||
File imgFile = new File("src/test/resources/graph.png");
|
||||
File imgFile = new File("src/test/resources/graph1.png");
|
||||
BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
|
||||
ImageIO.write(image, "PNG", imgFile);
|
||||
assertTrue(imgFile.exists());
|
||||
|
||||
@@ -13,6 +13,5 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
- [Checking if a Java Graph has a Cycle](https://www.baeldung.com/java-graph-has-a-cycle)
|
||||
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
|
||||
- [Creating a Triangle with for Loops in Java](https://www.baeldung.com/java-print-triangle)
|
||||
- [Efficient Word Frequency Calculator in Java](https://www.baeldung.com/java-word-frequency)
|
||||
- [The K-Means Clustering Algorithm in Java](https://www.baeldung.com/java-k-means-clustering-algorithm)
|
||||
- More articles: [[<-- prev]](/algorithms-miscellaneous-2) [[next -->]](/algorithms-miscellaneous-4)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?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">
|
||||
<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-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
@@ -5,8 +5,8 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
### Relevant articles:
|
||||
|
||||
- [Multi-Swarm Optimization Algorithm in Java](https://www.baeldung.com/java-multi-swarm-algorithm)
|
||||
- [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters)
|
||||
- [Find the Middle Element of a Linked List](https://www.baeldung.com/java-linked-list-middle-element)
|
||||
- [Check If a String Contains All The Letters of The Alphabet with Java](https://www.baeldung.com/java-string-contains-all-letters)
|
||||
- [Find the Middle Element of a Linked List in Java](https://www.baeldung.com/java-linked-list-middle-element)
|
||||
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
|
||||
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
|
||||
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?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">
|
||||
<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-4</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
@@ -9,14 +9,11 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
- [Reversing a Binary Tree in Java](https://www.baeldung.com/java-reversing-a-binary-tree)
|
||||
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
|
||||
- [Knapsack Problem Implementation in Java](https://www.baeldung.com/java-knapsack)
|
||||
- [How to Determine if a Binary Tree is Balanced](https://www.baeldung.com/java-balanced-binary-tree)
|
||||
- [How to Determine if a Binary Tree is Balanced in Java](https://www.baeldung.com/java-balanced-binary-tree)
|
||||
- [Overview of Combinatorial Problems in Java](https://www.baeldung.com/java-combinatorial-algorithms)
|
||||
- [Prim’s Algorithm](https://www.baeldung.com/java-prim-algorithm)
|
||||
- [Maximum Subarray Problem](https://www.baeldung.com/java-maximum-subarray)
|
||||
- [How to Merge Two Sorted Arrays](https://www.baeldung.com/java-merge-sorted-arrays)
|
||||
- [Median of Stream of Integers using Heap](https://www.baeldung.com/java-stream-integers-median-using-heap)
|
||||
- [Kruskal’s Algorithm for Spanning Trees](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)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-4)
|
||||
- [Prim’s Algorithm with a Java Implementation](https://www.baeldung.com/java-prim-algorithm)
|
||||
- [Maximum Subarray Problem in Java](https://www.baeldung.com/java-maximum-subarray)
|
||||
- [How to Merge Two Sorted Arrays in Java](https://www.baeldung.com/java-merge-sorted-arrays)
|
||||
- [Median of Stream of Integers using Heap in Java](https://www.baeldung.com/java-stream-integers-median-using-heap)
|
||||
- More articles: [[<-- prev]](/algorithms-miscellaneous-4) [[next -->]](/algorithms-miscellaneous-6)
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?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">
|
||||
<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-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
@@ -23,12 +25,6 @@
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>pl.allegro.finance</groupId>
|
||||
<artifactId>tradukisto</artifactId>
|
||||
@@ -39,11 +35,6 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-commons</artifactId>
|
||||
@@ -76,7 +67,6 @@
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<guava.version>28.1-jre</guava.version>
|
||||
<jackson.version>2.10.2</jackson.version>
|
||||
<junit.platform.version>1.6.0</junit.platform.version>
|
||||
</properties>
|
||||
|
||||
|
||||
-61
@@ -1,61 +0,0 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
public class BoruvkaMST {
|
||||
|
||||
private static Tree mst = new Tree();
|
||||
private static int totalWeight;
|
||||
|
||||
public BoruvkaMST(Graph graph) {
|
||||
DisjointSet dSet = new DisjointSet(graph.getNodes());
|
||||
|
||||
// repeat at most log N times or until we have N-1 edges
|
||||
for (int t = 1; t < graph.getNodes() && mst.getEdgeCount() < graph.getNodes() - 1; t = t + t) {
|
||||
|
||||
// foreach tree in forest, find closest edge
|
||||
Edge[] closestEdgeArray = new Edge[graph.getNodes()];
|
||||
for (Edge edge : graph.getAllEdges()) {
|
||||
int first = edge.getFirst();
|
||||
int second = edge.getSecond();
|
||||
int firstParent = dSet.getParent(first);
|
||||
int secondParent = dSet.getParent(second);
|
||||
if (firstParent == secondParent) {
|
||||
continue; // same tree
|
||||
}
|
||||
if (closestEdgeArray[firstParent] == null || edge.getWeight() < closestEdgeArray[firstParent].getWeight()) {
|
||||
closestEdgeArray[firstParent] = edge;
|
||||
}
|
||||
if (closestEdgeArray[secondParent] == null || edge.getWeight() < closestEdgeArray[secondParent].getWeight()) {
|
||||
closestEdgeArray[secondParent] = edge;
|
||||
}
|
||||
}
|
||||
|
||||
// add newly discovered edges to MST
|
||||
for (int i = 0; i < graph.getNodes(); i++) {
|
||||
Edge edge = closestEdgeArray[i];
|
||||
if (edge != null) {
|
||||
int first = edge.getFirst();
|
||||
int second = edge.getSecond();
|
||||
// don't add the same edge twice
|
||||
if (dSet.getParent(first) != dSet.getParent(second)) {
|
||||
mst.addEdge(edge);
|
||||
totalWeight += edge.getWeight();
|
||||
dSet.union(first, second);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterable<Edge> getMST() {
|
||||
return mst;
|
||||
}
|
||||
|
||||
public int getTotalWeight() {
|
||||
return totalWeight;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "MST: " + mst.toString() + " | Total Weight: " + totalWeight;
|
||||
}
|
||||
|
||||
}
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class DisjointSet {
|
||||
|
||||
private int[] nodeParents;
|
||||
private int[] nodeRanks;
|
||||
|
||||
public DisjointSet(int n) {
|
||||
nodeParents = new int[n];
|
||||
nodeRanks = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodeParents[i] = i;
|
||||
nodeRanks[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getParent(int node) {
|
||||
while (node != nodeParents[node]) {
|
||||
node = nodeParents[node];
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
public void union(int node1, int node2) {
|
||||
int node1Parent = getParent(node1);
|
||||
int node2Parent = getParent(node2);
|
||||
if (node1Parent == node2Parent) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (nodeRanks[node1Parent] < nodeRanks[node2Parent]) {
|
||||
nodeParents[node1Parent] = node2Parent;
|
||||
}
|
||||
else if (nodeRanks[node1Parent] > nodeRanks[node2Parent]) {
|
||||
nodeParents[node2Parent] = node1Parent;
|
||||
}
|
||||
else {
|
||||
nodeParents[node2Parent] = node1Parent;
|
||||
nodeRanks[node1Parent]++;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Parent: " + Arrays.toString(nodeParents) + "Rank: " + Arrays.toString(nodeRanks);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
public class Edge {
|
||||
|
||||
private final int first;
|
||||
private final int second;
|
||||
private final int weight;
|
||||
|
||||
public Edge(int first, int second, int weight) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public int getFirst() {
|
||||
return first;
|
||||
}
|
||||
|
||||
public int getSecond() {
|
||||
return second;
|
||||
}
|
||||
|
||||
public int getOtherNode(int firstNode) {
|
||||
int secondNode = 0;
|
||||
if (firstNode == first)
|
||||
secondNode = second;
|
||||
else if (firstNode == second)
|
||||
secondNode = first;
|
||||
return secondNode;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format("%d-%d %d", first, second, weight);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
|
||||
public class Graph {
|
||||
|
||||
private int nodes;
|
||||
private int edges;
|
||||
private Tree[] trees;
|
||||
|
||||
public Graph(Input jsonGraph) throws JsonParseException, JsonMappingException, IOException {
|
||||
nodes = jsonGraph.getNodes();
|
||||
trees = (Tree[]) new Tree[nodes];
|
||||
for (int i = 0; i < nodes; i++) {
|
||||
trees[i] = new Tree();
|
||||
}
|
||||
|
||||
int edgesFromInput = jsonGraph.getEdges();
|
||||
for (int i = 0; i < edgesFromInput; i++) {
|
||||
int first = jsonGraph.getEdgeList()
|
||||
.get(i)
|
||||
.getFirst();
|
||||
int second = jsonGraph.getEdgeList()
|
||||
.get(i)
|
||||
.getSecond();
|
||||
int weight = jsonGraph.getEdgeList()
|
||||
.get(i)
|
||||
.getWeight();
|
||||
Edge edge = new Edge(first, second, weight);
|
||||
|
||||
trees[first].addEdge(edge);
|
||||
trees[second].addEdge(edge);
|
||||
edges++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int getNodes() {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
public int getEdges() {
|
||||
return edges;
|
||||
}
|
||||
|
||||
public Iterable<Edge> iterableTree(int i) {
|
||||
return trees[i];
|
||||
}
|
||||
|
||||
public Iterable<Edge> getAllEdges() {
|
||||
Iterable<Edge> list = new Tree();
|
||||
for (int i = 0; i < nodes; i++) {
|
||||
for (Edge edge : iterableTree(i)) {
|
||||
if (edge.getOtherNode(i) > i) {
|
||||
((Tree) list).addEdge(edge);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Input {
|
||||
private int nodes;
|
||||
private int edges;
|
||||
private List<E> edgeList;
|
||||
|
||||
public int getNodes() {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
public void setNodes(int nodes) {
|
||||
this.nodes = nodes;
|
||||
}
|
||||
|
||||
public int getEdges() {
|
||||
return edges;
|
||||
}
|
||||
|
||||
public void setEdges(int edges) {
|
||||
this.edges = edges;
|
||||
}
|
||||
|
||||
public List<E> getEdgeList() {
|
||||
return edgeList;
|
||||
}
|
||||
|
||||
public void setEdgeList(List<E> edgeList) {
|
||||
this.edgeList = edgeList;
|
||||
}
|
||||
|
||||
static class E {
|
||||
private int first;
|
||||
private int second;
|
||||
private int weight;
|
||||
|
||||
public int getFirst() {
|
||||
return first;
|
||||
}
|
||||
|
||||
public void setFirst(int first) {
|
||||
this.first = first;
|
||||
}
|
||||
|
||||
public int getSecond() {
|
||||
return second;
|
||||
}
|
||||
|
||||
public void setSecond(int second) {
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(int weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Tree implements Iterable<Edge> {
|
||||
private Node root;
|
||||
private int edgeCount;
|
||||
|
||||
private static class Node {
|
||||
private Edge edge;
|
||||
private Node next;
|
||||
|
||||
public String toString() {
|
||||
String nextStr = next != null ? next.toString() : "";
|
||||
return edge.toString() + " | " + nextStr;
|
||||
}
|
||||
}
|
||||
|
||||
public Tree() {
|
||||
root = null;
|
||||
edgeCount = 0;
|
||||
}
|
||||
|
||||
public int getEdgeCount() {
|
||||
return edgeCount;
|
||||
}
|
||||
|
||||
public void addEdge(Edge edge) {
|
||||
Node oldRoot = root;
|
||||
root = new Node();
|
||||
root.edge = edge;
|
||||
root.next = oldRoot;
|
||||
edgeCount++;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String rootStr = root != null ? root.toString() : "";
|
||||
return "Tree: " + rootStr + "Size: " + edgeCount;
|
||||
}
|
||||
|
||||
public Iterator<Edge> iterator() {
|
||||
return new LinkedIterator(root);
|
||||
}
|
||||
|
||||
private class LinkedIterator implements Iterator<Edge> {
|
||||
private Node current;
|
||||
|
||||
public LinkedIterator(Node root) {
|
||||
current = root;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return current != null;
|
||||
}
|
||||
|
||||
public Edge next() {
|
||||
Edge edge = current.edge;
|
||||
current = current.next;
|
||||
return edge;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
package com.baeldung.algorithms.boruvka;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class BoruvkaUnitTest {
|
||||
|
||||
private Input input;
|
||||
private static String INPUT_JSON = "/input.json";
|
||||
|
||||
@Before
|
||||
public void convertInputJsonToObject() throws JsonParseException, JsonMappingException, IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
StringBuilder jsonStr = new StringBuilder();
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BoruvkaMST.class.getResourceAsStream(INPUT_JSON)))) {
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
jsonStr.append(line)
|
||||
.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
input = mapper.readValue(jsonStr.toString(), Input.class);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputGraph_whenBoruvkaPerformed_thenMinimumSpanningTree() throws JsonParseException, JsonMappingException, IOException {
|
||||
Graph graph = new Graph(input);
|
||||
BoruvkaMST boruvkaMST = new BoruvkaMST(graph);
|
||||
|
||||
Tree mst = (Tree) boruvkaMST.getMST();
|
||||
|
||||
assertEquals(30, boruvkaMST.getTotalWeight());
|
||||
assertEquals(4, mst.getEdgeCount());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
@@ -0,0 +1,52 @@
|
||||
<?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>parent-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.junit.platform</groupId>
|
||||
<artifactId>junit-platform-commons</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${org.assertj.core.version}</version>
|
||||
<scope>test</scope>
|
||||
</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>
|
||||
<guava.version>28.1-jre</guava.version>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<junit.platform.version>1.6.0</junit.platform.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+84
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -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]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user