Skip to content

Commit 6c0d731

Browse files
committed
Merge remote-tracking branch 'central/master'
2 parents 2db8ba4 + c1933c6 commit 6c0d731

File tree

115 files changed

+3270
-954
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+3270
-954
lines changed

algorithms/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<artifactId>jenetics</artifactId>
3535
<version>3.7.0</version>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.jgrapht</groupId>
39+
<artifactId>jgrapht-core</artifactId>
40+
<version>1.0.1</version>
41+
</dependency>
3742
</dependencies>
3843

3944
<build>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.jgrapht;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.List;
6+
7+
import org.jgrapht.VertexFactory;
8+
import org.jgrapht.alg.HamiltonianCycle;
9+
import org.jgrapht.generate.CompleteGraphGenerator;
10+
import org.jgrapht.graph.DefaultEdge;
11+
import org.jgrapht.graph.SimpleWeightedGraph;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
public class CompleteGraphTest {
16+
17+
static SimpleWeightedGraph<String, DefaultEdge> completeGraph;
18+
static int size = 10;
19+
20+
@Before
21+
public void createCompleteGraph() {
22+
completeGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
23+
CompleteGraphGenerator<String, DefaultEdge> completeGenerator = new CompleteGraphGenerator<String, DefaultEdge>(size);
24+
VertexFactory<String> vFactory = new VertexFactory<String>() {
25+
private int id = 0;
26+
public String createVertex() {
27+
return "v" + id++;
28+
}
29+
};
30+
completeGenerator.generateGraph(completeGraph, vFactory, null);
31+
}
32+
33+
@Test
34+
public void givenCompleteGraph_whenGetHamiltonianCyclePath_thenGetVerticeListInSequence() {
35+
List<String> verticeList = HamiltonianCycle.getApproximateOptimalForCompleteGraph(completeGraph);
36+
assertEquals(verticeList.size(), completeGraph.vertexSet().size());
37+
}
38+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.jgrapht;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Set;
9+
import java.util.stream.IntStream;
10+
11+
import org.jgrapht.DirectedGraph;
12+
import org.jgrapht.GraphPath;
13+
import org.jgrapht.alg.CycleDetector;
14+
import org.jgrapht.alg.KosarajuStrongConnectivityInspector;
15+
import org.jgrapht.alg.interfaces.StrongConnectivityAlgorithm;
16+
import org.jgrapht.alg.shortestpath.AllDirectedPaths;
17+
import org.jgrapht.alg.shortestpath.BellmanFordShortestPath;
18+
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
19+
import org.jgrapht.graph.DefaultDirectedGraph;
20+
import org.jgrapht.graph.DefaultEdge;
21+
import org.jgrapht.graph.DirectedSubgraph;
22+
import org.jgrapht.traverse.BreadthFirstIterator;
23+
import org.jgrapht.traverse.DepthFirstIterator;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
public class DirectedGraphTests {
28+
DirectedGraph<String, DefaultEdge> directedGraph;
29+
30+
@Before
31+
public void createDirectedGraph() {
32+
directedGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
33+
IntStream.range(1, 10).forEach(i -> {
34+
directedGraph.addVertex("v" + i);
35+
});
36+
directedGraph.addEdge("v1", "v2");
37+
directedGraph.addEdge("v2", "v4");
38+
directedGraph.addEdge("v4", "v3");
39+
directedGraph.addEdge("v3", "v1");
40+
directedGraph.addEdge("v5", "v4");
41+
directedGraph.addEdge("v5", "v6");
42+
directedGraph.addEdge("v6", "v7");
43+
directedGraph.addEdge("v7", "v5");
44+
directedGraph.addEdge("v8", "v5");
45+
directedGraph.addEdge("v9", "v8");
46+
}
47+
48+
@Test
49+
public void givenDirectedGraph_whenGetStronglyConnectedSubgraphs_thenPathExistsBetweenStronglyconnectedVertices() {
50+
StrongConnectivityAlgorithm<String, DefaultEdge> scAlg = new KosarajuStrongConnectivityInspector<>(directedGraph);
51+
List<DirectedSubgraph<String, DefaultEdge>> stronglyConnectedSubgraphs = scAlg.stronglyConnectedSubgraphs();
52+
List<String> stronglyConnectedVertices = new ArrayList<>(stronglyConnectedSubgraphs.get(3).vertexSet());
53+
54+
String randomVertex1 = stronglyConnectedVertices.get(0);
55+
String randomVertex2 = stronglyConnectedVertices.get(3);
56+
AllDirectedPaths<String, DefaultEdge> allDirectedPaths = new AllDirectedPaths<>(directedGraph);
57+
58+
List<GraphPath<String, DefaultEdge>> possiblePathList = allDirectedPaths.getAllPaths(randomVertex1, randomVertex2, false, stronglyConnectedVertices.size());
59+
assertTrue(possiblePathList.size() > 0);
60+
}
61+
62+
@Test
63+
public void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() {
64+
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(directedGraph);
65+
assertTrue(cycleDetector.detectCycles());
66+
Set<String> cycleVertices = cycleDetector.findCycles();
67+
assertTrue(cycleVertices.size() > 0);
68+
}
69+
70+
@Test
71+
public void givenDirectedGraph_whenCreateInstanceDepthFirstIterator_thenGetIterator() {
72+
DepthFirstIterator depthFirstIterator = new DepthFirstIterator<>(directedGraph);
73+
assertNotNull(depthFirstIterator);
74+
}
75+
76+
@Test
77+
public void givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator() {
78+
BreadthFirstIterator breadthFirstIterator = new BreadthFirstIterator<>(directedGraph);
79+
assertNotNull(breadthFirstIterator);
80+
}
81+
82+
@Test
83+
public void givenDirectedGraph_whenGetDijkstraShortestPath_thenGetNotNullPath() {
84+
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(directedGraph);
85+
List<String> shortestPath = dijkstraShortestPath.getPath("v1", "v4").getVertexList();
86+
assertNotNull(shortestPath);
87+
}
88+
89+
@Test
90+
public void givenDirectedGraph_whenGetBellmanFordShortestPath_thenGetNotNullPath() {
91+
BellmanFordShortestPath bellmanFordShortestPath = new BellmanFordShortestPath(directedGraph);
92+
List<String> shortestPath = bellmanFordShortestPath.getPath("v1", "v4").getVertexList();
93+
assertNotNull(shortestPath);
94+
}
95+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.jgrapht;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.stream.IntStream;
7+
8+
import org.jgrapht.GraphPath;
9+
import org.jgrapht.alg.cycle.HierholzerEulerianCycle;
10+
import org.jgrapht.graph.DefaultEdge;
11+
import org.jgrapht.graph.SimpleWeightedGraph;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
public class EulerianCircuitTest {
16+
SimpleWeightedGraph<String, DefaultEdge> simpleGraph;
17+
18+
@Before
19+
public void createGraphWithEulerianCircuit() {
20+
simpleGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
21+
IntStream.range(1, 6).forEach(i -> {
22+
simpleGraph.addVertex("v" + i);
23+
});
24+
IntStream.range(1, 6).forEach(i -> {
25+
int endVertexNo = (i + 1) > 5 ? 1 : i + 1;
26+
simpleGraph.addEdge("v" + i, "v" + endVertexNo);
27+
});
28+
}
29+
30+
@Test
31+
public void givenGraph_whenCheckEluerianCycle_thenGetResult() {
32+
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
33+
assertTrue(eulerianCycle.isEulerian(simpleGraph));
34+
}
35+
36+
@Test
37+
public void givenGraphWithEulerianCircuit_whenGetEulerianCycle_thenGetGraphPath() {
38+
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
39+
GraphPath path = eulerianCycle.getEulerianCycle(simpleGraph);
40+
assertTrue(path.getEdgeList().containsAll(simpleGraph.edgeSet()));
41+
}
42+
}

animal-sniffer-mvn-plugin/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>animal-sniffer-mvn-plugin</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>example-animal-sniffer-mvn-plugin</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<artifactId>maven-compiler-plugin</artifactId>
27+
<version>3.7.0</version>
28+
<configuration>
29+
<source>1.6</source>
30+
<target>1.6</target>
31+
</configuration>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.codehaus.mojo</groupId>
35+
<artifactId>animal-sniffer-maven-plugin</artifactId>
36+
<version>1.16</version>
37+
<configuration>
38+
<signature>
39+
<groupId>org.codehaus.mojo.signature</groupId>
40+
<artifactId>java16</artifactId>
41+
<version>1.0</version>
42+
</signature>
43+
</configuration>
44+
<executions>
45+
<execution>
46+
<id>animal-sniffer</id>
47+
<phase>verify</phase>
48+
<goals>
49+
<goal>check</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
56+
</build>
57+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung;
2+
3+
//import java.nio.charset.StandardCharsets;
4+
5+
/**
6+
* Hello world!
7+
*
8+
*/
9+
public class App
10+
{
11+
public static void main( String[] args )
12+
{
13+
System.out.println( "Hello World!" );
14+
//System.out.println(StandardCharsets.UTF_8.name());
15+
}
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
extends TestCase
12+
{
13+
/**
14+
* Create the test case
15+
*
16+
* @param testName name of the test case
17+
*/
18+
public AppTest( String testName )
19+
{
20+
super( testName );
21+
}
22+
23+
/**
24+
* @return the suite of tests being tested
25+
*/
26+
public static Test suite()
27+
{
28+
return new TestSuite( AppTest.class );
29+
}
30+
31+
/**
32+
* Rigourous Test :-)
33+
*/
34+
public void testApp()
35+
{
36+
37+
assertTrue( true );
38+
39+
}
40+
}

0 commit comments

Comments
 (0)