|
| 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 | +} |
0 commit comments