Skip to content

Commit 692fe31

Browse files
parthkariamaibin
authored andcommitted
BAEL-815 Introduction to JGraphT (eugenp#2653)
* BAEL-243 Spring Batch using Partitioner * BAEL-1106 Introduction to javax.measure * Unnecessary Committed * BAEL-1106 Move code to libraries from core-java * BAEL-1106 Move code to libraries from core-java * BAEL-815 Introduction to JGraphT * BAEL-815 Introduction to JGraphT
1 parent d79fcae commit 692fe31

4 files changed

Lines changed: 181 additions & 1 deletion

File tree

‎libraries/pom.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,11 @@
585585
<artifactId>fugue</artifactId>
586586
<version>3.0.0-m007</version>
587587
</dependency>
588+
<dependency>
589+
<groupId>org.jgrapht</groupId>
590+
<artifactId>jgrapht-core</artifactId>
591+
<version>1.0.1</version>
592+
</dependency>
588593
</dependencies>
589594
<repositories>
590595
<repository>
@@ -660,4 +665,4 @@
660665
<cache.version>1.0.0</cache.version>
661666
<hazelcast.version>3.8.4</hazelcast.version>
662667
</properties>
663-
</project>
668+
</project>
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+
}

0 commit comments

Comments
 (0)