Search in sources :

Example 1 with ConnectedComponents

use of com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents in project graphhopper by graphhopper.

the class EdgeBasedTarjanSCCTest method smallGraphWithLoops.

@Test
public void smallGraphWithLoops() {
    GraphHopperStorage g = new GraphBuilder(em).create();
    // 3<-0->2-1o
    // o
    // edge-keys 0,1
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 0).setDistance(1));
    // edge-keys 2,3
    GHUtility.setSpeed(60, true, false, encoder, g.edge(0, 2).setDistance(1));
    // edge-keys 4,5
    GHUtility.setSpeed(60, true, false, encoder, g.edge(0, 3).setDistance(1));
    // edge-keys 6,7
    GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 1).setDistance(1));
    // edge-keys 8,9
    GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 1).setDistance(1));
    ConnectedComponents result = EdgeBasedTarjanSCC.findComponentsRecursive(g, fwdAccessFilter, false);
    assertEquals(10, result.getEdgeKeys());
    assertEquals(6, result.getTotalComponents());
    assertEquals(2, result.getComponents().size());
    assertEquals(result.getComponents().get(0), result.getBiggestComponent());
    assertEquals(IntArrayList.from(7, 9, 8, 6), result.getComponents().get(0));
    assertEquals(IntArrayList.from(1, 0), result.getComponents().get(1));
    assertEquals(4, result.getSingleEdgeComponents().cardinality());
    for (IntCursor c : IntArrayList.from(2, 3, 4, 5)) {
        assertTrue(result.getSingleEdgeComponents().get(c.value));
    }
}
Also used : ConnectedComponents(com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectedComponents

use of com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents in project graphhopper by graphhopper.

the class EdgeBasedTarjanSCCTest method withStartEdges_comparison.

@RepeatedTest(20)
public void withStartEdges_comparison() {
    // we test the case where we specify all start edges (in this case the behavior should be the same for both methods)
    GraphHopperStorage g = new GraphBuilder(em).create();
    long seed = System.nanoTime();
    Random rnd = new Random(seed);
    GHUtility.buildRandomGraph(g, rnd, 500, 2, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), 60d, 0.8, 0.7, 0);
    ConnectedComponents components = EdgeBasedTarjanSCC.findComponents(g, fwdAccessFilter, true);
    IntArrayList edges = new IntArrayList();
    AllEdgesIterator iter = g.getAllEdges();
    while (iter.next()) edges.add(iter.getEdge());
    ConnectedComponents componentsForStartEdges = EdgeBasedTarjanSCC.findComponentsForStartEdges(g, fwdAccessFilter, edges);
    compareResults(g, seed, components, componentsForStartEdges);
}
Also used : Random(java.util.Random) ConnectedComponents(com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents) GraphBuilder(com.graphhopper.storage.GraphBuilder) IntArrayList(com.carrotsearch.hppc.IntArrayList) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 3 with ConnectedComponents

use of com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents in project graphhopper by graphhopper.

the class EdgeBasedTarjanSCCTest method withStartEdges_simple.

@Test
public void withStartEdges_simple() {
    // 0 - 1   4 - 5 - 6 - 7
    // |   |
    // 3 - 2   8 - 9
    GraphHopperStorage g = new GraphBuilder(em).create();
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 2).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 3).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(3, 0).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(4, 5).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(5, 6).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(6, 7).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(8, 9).setDistance(10));
    // just the left island
    ConnectedComponents components = EdgeBasedTarjanSCC.findComponentsForStartEdges(g, (prev, edge) -> true, IntArrayList.from(0));
    assertEquals(8, components.getEdgeKeys());
    assertEquals(1, components.getComponents().size());
    // all islands
    components = EdgeBasedTarjanSCC.findComponentsForStartEdges(g, (prev, edge) -> true, IntArrayList.from(0, 4, 7));
    assertEquals(16, components.getEdgeKeys());
    assertEquals(3, components.getComponents().size());
    // here we initialize as for all islands but the filter still prevents some edges to be found
    components = EdgeBasedTarjanSCC.findComponentsForStartEdges(g, (prev, edge) -> edge.getEdge() > 3 && edge.getEdge() < 7, IntArrayList.from(0, 4, 7));
    assertEquals(6, components.getEdgeKeys());
    assertEquals(1, components.getComponents().size());
}
Also used : RepeatedTest(org.junit.jupiter.api.RepeatedTest) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) TarjanSCCTest.buildComponentSet(com.graphhopper.routing.subnetwork.TarjanSCCTest.buildComponentSet) ConnectedComponents(com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) Set(java.util.Set) Random(java.util.Random) GHUtility(com.graphhopper.util.GHUtility) EncodingManager(com.graphhopper.routing.util.EncodingManager) GraphBuilder(com.graphhopper.storage.GraphBuilder) Test(org.junit.jupiter.api.Test) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) IntWithArray(com.graphhopper.routing.subnetwork.TarjanSCCTest.IntWithArray) IntArrayList(com.carrotsearch.hppc.IntArrayList) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) com.graphhopper.routing.util(com.graphhopper.routing.util) ConnectedComponents(com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents) GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectedComponents

use of com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents in project graphhopper by graphhopper.

the class EdgeBasedTarjanSCCTest method tree.

@Test
public void tree() {
    GraphHopperStorage g = new GraphBuilder(em).create();
    // 0 - 1 - 2 - 4 - 5
    // |    \- 6 - 7
    // 3        \- 8
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 2).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 3).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 4).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 6).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(4, 5).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(6, 7).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(6, 8).setDistance(1));
    ConnectedComponents result = EdgeBasedTarjanSCC.findComponentsRecursive(g, fwdAccessFilter, false);
    assertEquals(16, result.getEdgeKeys());
    assertEquals(1, result.getTotalComponents());
    assertEquals(1, result.getComponents().size());
    assertTrue(result.getSingleEdgeComponents().isEmpty());
    assertEquals(result.getComponents().get(0), result.getBiggestComponent());
    assertEquals(IntArrayList.from(1, 3, 7, 11, 10, 6, 9, 13, 12, 15, 14, 8, 2, 5, 4, 0), result.getComponents().get(0));
}
Also used : ConnectedComponents(com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents) GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 5 with ConnectedComponents

use of com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents in project graphhopper by graphhopper.

the class EdgeBasedTarjanSCCTest method withTurnRestriction.

@Test
public void withTurnRestriction() {
    GraphHopperStorage g = new GraphBuilder(em).create();
    // here 0-1-2-3 would be a circle and thus belong to same connected component. but if there is a
    // turn restriction for going 0->2->3 this splits the graph into multiple components
    // 0->1
    // |  |
    // 3<-2->4
    // edge-keys 0,1
    GHUtility.setSpeed(60, true, false, encoder, g.edge(0, 1).setDistance(1));
    // edge-keys 2,3
    GHUtility.setSpeed(60, true, false, encoder, g.edge(1, 2).setDistance(1));
    // edge-keys 4,5
    GHUtility.setSpeed(60, true, false, encoder, g.edge(2, 3).setDistance(1));
    // edge-keys 6,7
    GHUtility.setSpeed(60, true, false, encoder, g.edge(3, 0).setDistance(1));
    // edge-keys 8,9
    GHUtility.setSpeed(60, true, false, encoder, g.edge(2, 4).setDistance(1));
    // first lets check what happens without turn costs
    ConnectedComponents result = EdgeBasedTarjanSCC.findComponentsRecursive(g, fwdAccessFilter, false);
    assertEquals(7, result.getTotalComponents());
    assertEquals(1, result.getComponents().size());
    assertEquals(IntArrayList.from(6, 4, 2, 0), result.getBiggestComponent());
    assertEquals(6, result.getSingleEdgeComponents().cardinality());
    for (IntCursor c : IntArrayList.from(1, 3, 5, 7, 8, 9)) {
        assertTrue(result.getSingleEdgeComponents().get(c.value));
    }
    // now lets try with a restricted turn
    result = EdgeBasedTarjanSCC.findComponentsRecursive(g, (prev, edge) -> fwdAccessFilter.accept(prev, edge) && !(prev == 1 && edge.getBaseNode() == 2 && edge.getEdge() == 2), false);
    // none of the edges are strongly connected anymore!
    assertEquals(10, result.getTotalComponents());
    assertEquals(0, result.getComponents().size());
    assertEquals(IntArrayList.from(), result.getBiggestComponent());
    assertEquals(10, result.getSingleEdgeComponents().cardinality());
    for (IntCursor c : IntArrayList.from(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) {
        assertTrue(result.getSingleEdgeComponents().get(c.value));
    }
}
Also used : RepeatedTest(org.junit.jupiter.api.RepeatedTest) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) TarjanSCCTest.buildComponentSet(com.graphhopper.routing.subnetwork.TarjanSCCTest.buildComponentSet) ConnectedComponents(com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) Set(java.util.Set) Random(java.util.Random) GHUtility(com.graphhopper.util.GHUtility) EncodingManager(com.graphhopper.routing.util.EncodingManager) GraphBuilder(com.graphhopper.storage.GraphBuilder) Test(org.junit.jupiter.api.Test) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) IntWithArray(com.graphhopper.routing.subnetwork.TarjanSCCTest.IntWithArray) IntArrayList(com.carrotsearch.hppc.IntArrayList) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) com.graphhopper.routing.util(com.graphhopper.routing.util) ConnectedComponents(com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectedComponents (com.graphhopper.routing.subnetwork.EdgeBasedTarjanSCC.ConnectedComponents)12 GraphBuilder (com.graphhopper.storage.GraphBuilder)12 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)12 RepeatedTest (org.junit.jupiter.api.RepeatedTest)11 Test (org.junit.jupiter.api.Test)10 IntCursor (com.carrotsearch.hppc.cursors.IntCursor)4 Random (java.util.Random)4 IntArrayList (com.carrotsearch.hppc.IntArrayList)3 IntWithArray (com.graphhopper.routing.subnetwork.TarjanSCCTest.IntWithArray)2 TarjanSCCTest.buildComponentSet (com.graphhopper.routing.subnetwork.TarjanSCCTest.buildComponentSet)2 com.graphhopper.routing.util (com.graphhopper.routing.util)2 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)2 EncodingManager (com.graphhopper.routing.util.EncodingManager)2 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)2 GHUtility (com.graphhopper.util.GHUtility)2 Set (java.util.Set)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2