forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'central/master'
- Loading branch information
Showing
115 changed files
with
3,270 additions
and
954 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
algorithms/src/test/java/com/baeldung/jgrapht/CompleteGraphTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, DefaultEdge> completeGraph; | ||
static int size = 10; | ||
|
||
@Before | ||
public void createCompleteGraph() { | ||
completeGraph = new SimpleWeightedGraph<>(DefaultEdge.class); | ||
CompleteGraphGenerator<String, DefaultEdge> completeGenerator = new CompleteGraphGenerator<String, DefaultEdge>(size); | ||
VertexFactory<String> vFactory = new VertexFactory<String>() { | ||
private int id = 0; | ||
public String createVertex() { | ||
return "v" + id++; | ||
} | ||
}; | ||
completeGenerator.generateGraph(completeGraph, vFactory, null); | ||
} | ||
|
||
@Test | ||
public void givenCompleteGraph_whenGetHamiltonianCyclePath_thenGetVerticeListInSequence() { | ||
List<String> verticeList = HamiltonianCycle.getApproximateOptimalForCompleteGraph(completeGraph); | ||
assertEquals(verticeList.size(), completeGraph.vertexSet().size()); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
algorithms/src/test/java/com/baeldung/jgrapht/DirectedGraphTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, DefaultEdge> directedGraph; | ||
|
||
@Before | ||
public void createDirectedGraph() { | ||
directedGraph = new DefaultDirectedGraph<String, DefaultEdge>(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<String, DefaultEdge> scAlg = new KosarajuStrongConnectivityInspector<>(directedGraph); | ||
List<DirectedSubgraph<String, DefaultEdge>> stronglyConnectedSubgraphs = scAlg.stronglyConnectedSubgraphs(); | ||
List<String> stronglyConnectedVertices = new ArrayList<>(stronglyConnectedSubgraphs.get(3).vertexSet()); | ||
|
||
String randomVertex1 = stronglyConnectedVertices.get(0); | ||
String randomVertex2 = stronglyConnectedVertices.get(3); | ||
AllDirectedPaths<String, DefaultEdge> allDirectedPaths = new AllDirectedPaths<>(directedGraph); | ||
|
||
List<GraphPath<String, DefaultEdge>> possiblePathList = allDirectedPaths.getAllPaths(randomVertex1, randomVertex2, false, stronglyConnectedVertices.size()); | ||
assertTrue(possiblePathList.size() > 0); | ||
} | ||
|
||
@Test | ||
public void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() { | ||
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(directedGraph); | ||
assertTrue(cycleDetector.detectCycles()); | ||
Set<String> 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<String> shortestPath = dijkstraShortestPath.getPath("v1", "v4").getVertexList(); | ||
assertNotNull(shortestPath); | ||
} | ||
|
||
@Test | ||
public void givenDirectedGraph_whenGetBellmanFordShortestPath_thenGetNotNullPath() { | ||
BellmanFordShortestPath bellmanFordShortestPath = new BellmanFordShortestPath(directedGraph); | ||
List<String> shortestPath = bellmanFordShortestPath.getPath("v1", "v4").getVertexList(); | ||
assertNotNull(shortestPath); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
algorithms/src/test/java/com/baeldung/jgrapht/EulerianCircuitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, DefaultEdge> 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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<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/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>animal-sniffer-mvn-plugin</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>example-animal-sniffer-mvn-plugin</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.7.0</version> | ||
<configuration> | ||
<source>1.6</source> | ||
<target>1.6</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>animal-sniffer-maven-plugin</artifactId> | ||
<version>1.16</version> | ||
<configuration> | ||
<signature> | ||
<groupId>org.codehaus.mojo.signature</groupId> | ||
<artifactId>java16</artifactId> | ||
<version>1.0</version> | ||
</signature> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>animal-sniffer</id> | ||
<phase>verify</phase> | ||
<goals> | ||
<goal>check</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
|
||
</build> | ||
</project> |
16 changes: 16 additions & 0 deletions
16
animal-sniffer-mvn-plugin/src/main/java/com/baeldung/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung; | ||
|
||
//import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Hello world! | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
System.out.println( "Hello World!" ); | ||
//System.out.println(StandardCharsets.UTF_8.name()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
animal-sniffer-mvn-plugin/src/test/java/com/baeldung/AppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.baeldung; | ||
|
||
import junit.framework.Test; | ||
import junit.framework.TestCase; | ||
import junit.framework.TestSuite; | ||
|
||
/** | ||
* Unit test for simple App. | ||
*/ | ||
public class AppTest | ||
extends TestCase | ||
{ | ||
/** | ||
* Create the test case | ||
* | ||
* @param testName name of the test case | ||
*/ | ||
public AppTest( String testName ) | ||
{ | ||
super( testName ); | ||
} | ||
|
||
/** | ||
* @return the suite of tests being tested | ||
*/ | ||
public static Test suite() | ||
{ | ||
return new TestSuite( AppTest.class ); | ||
} | ||
|
||
/** | ||
* Rigourous Test :-) | ||
*/ | ||
public void testApp() | ||
{ | ||
|
||
assertTrue( true ); | ||
|
||
} | ||
} |
Oops, something went wrong.