Package org.jamesii.core.util.graph

Examples of org.jamesii.core.util.graph.SimpleGraph


  @Override
  public ISimpleGraph analyzeModel(IModel model) {

    if (!(model instanceof ICAGrid)) {
      modelGraph = new SimpleGraph(1);
      return modelGraph;
    }

    ICAGrid caModel = (ICAGrid) model;

    int vertexCount = countNodes(caModel);

    modelGraph = new SimpleGraph(vertexCount);

    createEdges(caModel);

    // Setting calculation costs equally
    for (int i = 0; i < vertexCount; i++) {
View Full Code Here


  /**
   * Tests the cycle detection.
   */
  public void testCycleDetection() {
    SimpleGraph simpleGraph = new SimpleGraph(5);
    simpleGraph.addEdge(new AnnotatedEdge<Integer, Double, Object>(0, 1));
    simpleGraph.addEdge(new AnnotatedEdge<Integer, Double, Object>(1, 2));
    simpleGraph.addEdge(new AnnotatedEdge<Integer, Double, Object>(2, 3));
    simpleGraph.addEdge(new AnnotatedEdge<Integer, Double, Object>(3, 4));
    List<Integer> cycle = CycleDetection.detectCycle(simpleGraph);
    assertTrue("There is no cycle in the given graph.", cycle.isEmpty());
    simpleGraph.addEdge(new AnnotatedEdge<Integer, Double, Object>(4, 2));
    cycle = CycleDetection.detectCycle(simpleGraph);
    assertTrue(
        "There is a cycle (2,3,4) in the given graph.",
        cycle.size() == 3 && cycle.contains(2) && cycle.contains(3)
            && cycle.contains(4));
View Full Code Here

  /**
   * Simple test of Dijkstra algorithm.
   */
  public void testSimpleExample() {
    SimpleGraph graph = createTestGraph();

    Map<Integer, Double> pathLengths = dijkstra.compute(graph, 0);
    addInformation("Calculated result:" + Strings.dispMap(pathLengths));

    assertEquals("There should be a path length for each vertex.",
        graph.getVertexCount(), pathLengths.size());
    for (Integer vertex : graph.getVertices()) {
      assertNotNull(pathLengths.get(vertex));
      assertFalse(Double.isInfinite(vertex));
    }

    // Check true solution:
View Full Code Here

  /**
   * Test unconnected graph. All distances (except to the start node) should be
   * infinite.
   */
  public void testUnconnectedGraph() {
    SimpleGraph graph = new SimpleGraph(NUM_OF_VERTICES_EMPTY_GRAPH);
    Map<Integer, Double> pathLengths = dijkstra.compute(graph, 0);
    for (int i = 1; i < NUM_OF_VERTICES_EMPTY_GRAPH; i++) {
      assertTrue(Double.isInfinite(pathLengths.get(i)));
    }
  }
View Full Code Here

   * href="http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">Wikipedia</a>.
   *
   * @return the graph
   */
  private SimpleGraph createTestGraph() {
    SimpleGraph testGraph = new SimpleGraph(6);

    testGraph.addEdge(new AnnotatedEdge<>(0, 1, 7.));
    testGraph.addEdge(new AnnotatedEdge<>(0, 2, 9.));
    testGraph.addEdge(new AnnotatedEdge<>(0, 5, 14.));

    testGraph.addEdge(new AnnotatedEdge<>(1, 2, 10.));
    testGraph.addEdge(new AnnotatedEdge<>(1, 3, 15.));

    testGraph.addEdge(new AnnotatedEdge<>(2, 3, 11.));
    testGraph.addEdge(new AnnotatedEdge<>(2, 5, 2.));

    testGraph.addEdge(new AnnotatedEdge<>(3, 4, 6.));

    testGraph.addEdge(new AnnotatedEdge<>(4, 5, 9.));
    return testGraph;
  }
View Full Code Here

   *          the number of nodes
   *
   * @return the random simple graph
   */
  public ISimpleGraph getRandomSimpleGraph(int numberOfNodes) {
    return getRandomSimpleGraph(new SimpleGraph(numberOfNodes));
  }
View Full Code Here

    String line = file.readLine();
    String[] header = line.split(" ");

    int numOfVertices = Integer.parseInt(header[0]);

    SimpleGraph graph = new SimpleGraph(numOfVertices);

    line = file.readLine();
    int actualVertex = 0;
    while (line != null) {

      graph.setLabel(actualVertex, 1.0);
      String[] neighbours = line.split(" ");

      for (String neighbour : neighbours) {
        if (neighbour.length() > 0) {
          graph.addEdge(new SimpleEdge(actualVertex, Integer
              .parseInt(neighbour) - 1, 1.0));
        }
      }

      line = file.readLine();
View Full Code Here

  public SimpleGraph generateRandomGraph(int numOfNodes,
      double approxNumberOfNeighbours, boolean simpleGraph,
      IObjectCreator objectCreator) {

    // Initializing...
    SimpleGraph graph = new SimpleGraph(numOfNodes);

    if (simpleGraph) {
      graph.setSimple(true);
    }

    int minNumberOfEdges =
        (int) Math.floor(numOfNodes * approxNumberOfNeighbours / 2) + 1;

    int edgeCount = 0;
    List<List<Integer>> connectedSets = new ArrayList<>();

    // Initialize component list, set random vertex labels
    for (int i = 0; i < numOfNodes; i++) {
      ArrayList<Integer> component = new ArrayList<>();
      component.add(i);
      connectedSets.add(component);
      graph.setLabel(i, rand.nextDouble() * UPPER_BOUND_RANDOM_NODE_WEIGHT);
    }

    while (edgeCount < minNumberOfEdges || connectedSets.size() > 1) {
      int vertexA = (int) Math.floor(rand.nextFloat() * numOfNodes);
      int vertexB = (int) Math.floor(rand.nextFloat() * numOfNodes);
 
View Full Code Here

TOP

Related Classes of org.jamesii.core.util.graph.SimpleGraph

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.