diff --git a/libraries/pom.xml b/libraries/pom.xml
index b0fdbea2f6..a15ffa6dfc 100644
--- a/libraries/pom.xml
+++ b/libraries/pom.xml
@@ -590,6 +590,11 @@
jsr-275
${javax-measure.version}
+
+ org.jgrapht
+ jgrapht-core
+ 1.0.1
+
diff --git a/libraries/src/main/java/com/baeldung/javax/measure/WaterTank.java b/libraries/src/main/java/com/baeldung/javax/measure/WaterTank.java
deleted file mode 100644
index 1ab9eee53f..0000000000
--- a/libraries/src/main/java/com/baeldung/javax/measure/WaterTank.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.baeldung.javax.measure;
-
-import javax.measure.Measure;
-import javax.measure.quantity.Volume;
-
-public class WaterTank {
-
- private Measure capacityMeasure;
- private double capacityDouble;
-
- public void setCapacityMeasure(Measure capacityMeasure) {
- this.capacityMeasure = capacityMeasure;
- }
-
- public void setCapacityDouble(double capacityDouble) {
- this.capacityDouble = capacityDouble;
- }
-
- public Measure getCapacityMeasure() {
- return capacityMeasure;
- }
-
- public double getCapacityDouble() {
- return capacityDouble;
- }
-}
diff --git a/libraries/src/test/java/com/baeldung/javax/measure/WaterTankTests.java b/libraries/src/test/java/com/baeldung/javax/measure/WaterTankTests.java
deleted file mode 100644
index ef54035353..0000000000
--- a/libraries/src/test/java/com/baeldung/javax/measure/WaterTankTests.java
+++ /dev/null
@@ -1,68 +0,0 @@
-package com.baeldung.javax.measure;
-
-import javax.measure.Measure;
-import javax.measure.converter.UnitConverter;
-import javax.measure.quantity.Duration;
-import javax.measure.quantity.Length;
-import javax.measure.quantity.Pressure;
-import javax.measure.quantity.Volume;
-import static javax.measure.unit.NonSI.HOUR;
-import static javax.measure.unit.NonSI.LITRE;
-import static javax.measure.unit.NonSI.MILE;
-import static javax.measure.unit.NonSI.MINUTE;
-import javax.measure.unit.SI;
-import static javax.measure.unit.SI.KILO;
-import static javax.measure.unit.SI.METER;
-import static javax.measure.unit.SI.NEWTON;
-import static javax.measure.unit.SI.SECOND;
-import javax.measure.unit.Unit;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import org.junit.Test;
-
-public class WaterTankTests {
-
- @Test
- public void givenMeasure_whenGetUnitAndConvertValue_thenSuccess() {
- WaterTank waterTank = new WaterTank();
- waterTank.setCapacityMeasure(Measure.valueOf(9.2, LITRE));
- assertEquals(LITRE, waterTank.getCapacityMeasure().getUnit());
-
- Measure waterCapacity = waterTank.getCapacityMeasure();
- double volumeInLitre = waterCapacity.getValue().doubleValue();
- assertEquals(9.2, volumeInLitre, 0.0f);
-
- double volumeInMilliLitre = waterCapacity.doubleValue(SI.MILLI(LITRE));
- assertEquals(9200.0, volumeInMilliLitre, 0.0f);
-
- Unit Kilometer = SI.KILO(METER);
- Unit Centimeter = SI.CENTI(METER);
- }
-
- @Test
- public void givenMeasure_whenAlternateMeasure_ThenGetAlternateMeasure() {
- Unit PASCAL = NEWTON.divide(METER.pow(2)).alternate("Pa");
- assertTrue(Unit.valueOf("Pa").equals(PASCAL));
- }
-
- @Test
- public void givenMeasure_whenCompoundMeasure_ThenGetCompoundMeasure() {
- Unit HOUR_MINUTE_SECOND = HOUR.compound(MINUTE).compound(SECOND);
- Measure duration = Measure.valueOf(12345, SECOND);
- assertEquals("3h25min45s", duration.to(HOUR_MINUTE_SECOND).toString());
- }
-
- @Test
- public void givenMiles_whenConvertToKilometer_ThenConverted() {
- double distanceInMiles = 50.0;
- UnitConverter mileToKilometer = MILE.getConverterTo(KILO(METER));
- double distanceInKilometers = mileToKilometer.convert(distanceInMiles);
- assertEquals(80.4672, distanceInKilometers, 0.00f);
- }
-
- @Test
- public void givenSymbol_WhenCompareToSystemUnit_ThenSuccess() {
- assertTrue(Unit.valueOf("kW").equals(SI.KILO(SI.WATT)));
- assertTrue(Unit.valueOf("ms").equals(SI.SECOND.divide(1000)));
- }
-}
diff --git a/libraries/src/test/java/com/baeldung/jgrapht/CompleteGraphTest.java b/libraries/src/test/java/com/baeldung/jgrapht/CompleteGraphTest.java
new file mode 100644
index 0000000000..c085d54689
--- /dev/null
+++ b/libraries/src/test/java/com/baeldung/jgrapht/CompleteGraphTest.java
@@ -0,0 +1,38 @@
+package com.baeldung.jgrapht;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.jgrapht.VertexFactory;
+import org.jgrapht.alg.HamiltonianCycle;
+import org.jgrapht.generate.CompleteGraphGenerator;
+import org.jgrapht.graph.DefaultEdge;
+import org.jgrapht.graph.SimpleWeightedGraph;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CompleteGraphTest {
+
+ static SimpleWeightedGraph completeGraph;
+ static int size = 10;
+
+ @Before
+ public void createCompleteGraph() {
+ completeGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
+ CompleteGraphGenerator completeGenerator = new CompleteGraphGenerator(size);
+ VertexFactory vFactory = new VertexFactory() {
+ private int id = 0;
+ public String createVertex() {
+ return "v" + id++;
+ }
+ };
+ completeGenerator.generateGraph(completeGraph, vFactory, null);
+ }
+
+ @Test
+ public void givenCompleteGraph_whenGetHamiltonianCyclePath_thenGetVerticeListInSequence() {
+ List verticeList = HamiltonianCycle.getApproximateOptimalForCompleteGraph(completeGraph);
+ assertEquals(verticeList.size(), completeGraph.vertexSet().size());
+ }
+}
diff --git a/libraries/src/test/java/com/baeldung/jgrapht/DirectedGraphTests.java b/libraries/src/test/java/com/baeldung/jgrapht/DirectedGraphTests.java
new file mode 100644
index 0000000000..7f4cc99715
--- /dev/null
+++ b/libraries/src/test/java/com/baeldung/jgrapht/DirectedGraphTests.java
@@ -0,0 +1,95 @@
+package com.baeldung.jgrapht;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.IntStream;
+
+import org.jgrapht.DirectedGraph;
+import org.jgrapht.GraphPath;
+import org.jgrapht.alg.CycleDetector;
+import org.jgrapht.alg.KosarajuStrongConnectivityInspector;
+import org.jgrapht.alg.interfaces.StrongConnectivityAlgorithm;
+import org.jgrapht.alg.shortestpath.AllDirectedPaths;
+import org.jgrapht.alg.shortestpath.BellmanFordShortestPath;
+import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
+import org.jgrapht.graph.DefaultDirectedGraph;
+import org.jgrapht.graph.DefaultEdge;
+import org.jgrapht.graph.DirectedSubgraph;
+import org.jgrapht.traverse.BreadthFirstIterator;
+import org.jgrapht.traverse.DepthFirstIterator;
+import org.junit.Before;
+import org.junit.Test;
+
+public class DirectedGraphTests {
+ DirectedGraph directedGraph;
+
+ @Before
+ public void createDirectedGraph() {
+ directedGraph = new DefaultDirectedGraph(DefaultEdge.class);
+ IntStream.range(1, 10).forEach(i -> {
+ directedGraph.addVertex("v" + i);
+ });
+ directedGraph.addEdge("v1", "v2");
+ directedGraph.addEdge("v2", "v4");
+ directedGraph.addEdge("v4", "v3");
+ directedGraph.addEdge("v3", "v1");
+ directedGraph.addEdge("v5", "v4");
+ directedGraph.addEdge("v5", "v6");
+ directedGraph.addEdge("v6", "v7");
+ directedGraph.addEdge("v7", "v5");
+ directedGraph.addEdge("v8", "v5");
+ directedGraph.addEdge("v9", "v8");
+ }
+
+ @Test
+ public void givenDirectedGraph_whenGetStronglyConnectedSubgraphs_thenPathExistsBetweenStronglyconnectedVertices() {
+ StrongConnectivityAlgorithm scAlg = new KosarajuStrongConnectivityInspector<>(directedGraph);
+ List> stronglyConnectedSubgraphs = scAlg.stronglyConnectedSubgraphs();
+ List stronglyConnectedVertices = new ArrayList<>(stronglyConnectedSubgraphs.get(3).vertexSet());
+
+ String randomVertex1 = stronglyConnectedVertices.get(0);
+ String randomVertex2 = stronglyConnectedVertices.get(3);
+ AllDirectedPaths allDirectedPaths = new AllDirectedPaths<>(directedGraph);
+
+ List> possiblePathList = allDirectedPaths.getAllPaths(randomVertex1, randomVertex2, false, stronglyConnectedVertices.size());
+ assertTrue(possiblePathList.size() > 0);
+ }
+
+ @Test
+ public void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() {
+ CycleDetector cycleDetector = new CycleDetector(directedGraph);
+ assertTrue(cycleDetector.detectCycles());
+ Set cycleVertices = cycleDetector.findCycles();
+ assertTrue(cycleVertices.size() > 0);
+ }
+
+ @Test
+ public void givenDirectedGraph_whenCreateInstanceDepthFirstIterator_thenGetIterator() {
+ DepthFirstIterator depthFirstIterator = new DepthFirstIterator<>(directedGraph);
+ assertNotNull(depthFirstIterator);
+ }
+
+ @Test
+ public void givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator() {
+ BreadthFirstIterator breadthFirstIterator = new BreadthFirstIterator<>(directedGraph);
+ assertNotNull(breadthFirstIterator);
+ }
+
+ @Test
+ public void givenDirectedGraph_whenGetDijkstraShortestPath_thenGetNotNullPath() {
+ DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(directedGraph);
+ List shortestPath = dijkstraShortestPath.getPath("v1", "v4").getVertexList();
+ assertNotNull(shortestPath);
+ }
+
+ @Test
+ public void givenDirectedGraph_whenGetBellmanFordShortestPath_thenGetNotNullPath() {
+ BellmanFordShortestPath bellmanFordShortestPath = new BellmanFordShortestPath(directedGraph);
+ List shortestPath = bellmanFordShortestPath.getPath("v1", "v4").getVertexList();
+ assertNotNull(shortestPath);
+ }
+}
diff --git a/libraries/src/test/java/com/baeldung/jgrapht/EulerianCircuitTest.java b/libraries/src/test/java/com/baeldung/jgrapht/EulerianCircuitTest.java
new file mode 100644
index 0000000000..6f0fb92ab7
--- /dev/null
+++ b/libraries/src/test/java/com/baeldung/jgrapht/EulerianCircuitTest.java
@@ -0,0 +1,42 @@
+package com.baeldung.jgrapht;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.stream.IntStream;
+
+import org.jgrapht.GraphPath;
+import org.jgrapht.alg.cycle.HierholzerEulerianCycle;
+import org.jgrapht.graph.DefaultEdge;
+import org.jgrapht.graph.SimpleWeightedGraph;
+import org.junit.Before;
+import org.junit.Test;
+
+public class EulerianCircuitTest {
+ SimpleWeightedGraph simpleGraph;
+
+ @Before
+ public void createGraphWithEulerianCircuit() {
+ simpleGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
+ IntStream.range(1, 6).forEach(i -> {
+ simpleGraph.addVertex("v" + i);
+ });
+ IntStream.range(1, 6).forEach(i -> {
+ int endVertexNo = (i + 1) > 5 ? 1 : i + 1;
+ simpleGraph.addEdge("v" + i, "v" + endVertexNo);
+ });
+ }
+
+ @Test
+ public void givenGraph_whenCheckEluerianCycle_thenGetResult() {
+ HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
+ assertTrue(eulerianCycle.isEulerian(simpleGraph));
+ }
+
+ @Test
+ public void givenGraphWithEulerianCircuit_whenGetEulerianCycle_thenGetGraphPath() {
+ HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
+ GraphPath path = eulerianCycle.getEulerianCycle(simpleGraph);
+ assertTrue(path.getEdgeList().containsAll(simpleGraph.edgeSet()));
+ }
+}