Package org.jamesii.core.util.graph.trees

Examples of org.jamesii.core.util.graph.trees.SimpleTree


   * @return labelled graph that is a tree
   */
  public SimpleTree generateRandomTree(int numOfNodes,
      int approxNumberOfChildren) {

    SimpleTree tree = new SimpleTree(numOfNodes);
    tree.turnTreeCheckOff();

    int actualNodeNum = 1;
    double randomNumber = 0.0;

    ArrayList<Integer> nodesOnLevel = new ArrayList<>();
    nodesOnLevel.add(0);

    while (actualNodeNum < numOfNodes && nodesOnLevel.size() > 0) {

      ArrayList<Integer> oldLevel = nodesOnLevel;
      nodesOnLevel = new ArrayList<>();
      int randomParent =
          ((Double) (Math.floor(rand.nextDouble() * (oldLevel.size()))))
              .intValue();
      addEdge(tree, actualNodeNum, oldLevel.get(randomParent));
      nodesOnLevel.add(actualNodeNum);
      actualNodeNum++;
      AbstractNormalDistribution normDist =
          new NormalDistribution(rand, 0., 1.);

      for (int i : oldLevel) {

        randomNumber =
            approxNumberOfChildren + 0.25 * approxNumberOfChildren
                * normDist.getRandomNumber();

        while (randomNumber > 0.5 && actualNodeNum < numOfNodes) {
          addEdge(tree, actualNodeNum, i);
          nodesOnLevel.add(actualNodeNum);
          actualNodeNum++;
          randomNumber--;
        }
      }

    }

    for (int i = 0; i < numOfNodes; i++) {
      tree.setLabel(i, rand.nextDouble() * 10);
    }

    if (!tree.turnTreeCheckOn()) {
      throw new IllegalStateException("Problem with random tree creation");
    }

    tree.setTreeRoot(0);

    return tree;
  }
View Full Code Here


  /**
   * Tests {@link BreadthFirstSearch}.
   */
  public void testBFS() {
    BreadthFirstSearch<Integer> bfs = new BreadthFirstSearch<>();
    SimpleTree tree = new SimpleTree(4);
    tree.setTreeRoot(0);
    tree.addEdge(new AnnotatedEdge<Integer, Double, Object>(1, 0));
    tree.addEdge(new AnnotatedEdge<Integer, Double, Object>(2, 0));
    tree.addEdge(new AnnotatedEdge<Integer, Double, Object>(3, 0));
    ListCallBack lcb = new ListCallBack();
    bfs.traverse(tree, lcb);
    assertEquals(4, lcb.getVisited().size());
    for (int i = 0; i < 4; i++) {
      assertEquals(i, lcb.getVisited().get(i).intValue());
    }
    assertEquals(4, tree.getVertexCount());
  }
View Full Code Here

      boolean fixedLabel) {

    // create tree structure while holding the nodes associated to their depth
    // level
    List<List<Integer>> nodesOnLevel = new ArrayList<>();
    SimpleTree tree =
        createRandomTreeStructure(numOfNodes, approxNumberOfChildren,
            nodesOnLevel);

    assignRandomWeightsToLeaves(fixedLabel, nodesOnLevel, tree);
View Full Code Here

   * @return the simple tree
   */
  private SimpleTree createRandomTreeStructure(int numOfNodes,
      int approxNumberOfChildren, List<List<Integer>> nodesOnLevel) {

    SimpleTree tree = new SimpleTree(numOfNodes);
    tree.setTreeRoot(0);
    int currentNodeNumber = 0;
    int numOfNodesToGenerate = numOfNodes - 1;

    nodesOnLevel.add(new ArrayList<Integer>());
    nodesOnLevel.get(0).add(0);

    tree.turnTreeCheckOff();

    while (currentNodeNumber < numOfNodesToGenerate
        && nodesOnLevel.get(nodesOnLevel.size() - 1).size() > 0) {
      currentNodeNumber =
          createAdditionalTreeLevel(tree, nodesOnLevel, approxNumberOfChildren,
              currentNodeNumber, numOfNodesToGenerate);
    }
    if (!tree.turnTreeCheckOn()) {
      throw new IllegalStateException("Random tree generation failed!");
    }
    return tree;
  }
View Full Code Here

   */
  public SimpleTree generateRandomTree(int numOfNodes,
      int approxNumberOfChildren, IObjectCreator objectCreator) {

    List<List<Integer>> nodesOnLevel = new ArrayList<>();
    SimpleTree tree =
        createRandomTreeStructure(numOfNodes, approxNumberOfChildren,
            nodesOnLevel);

    for (int i = 0; i < numOfNodes + 1; i++) {
      tree.setLabel(i, rand.nextDouble() * UPPER_BOUND_RANDOM_NODE_WEIGHT);
    }

    if (objectCreator != null) {
      objectCreator.createObjects(tree);
    }
View Full Code Here

TOP

Related Classes of org.jamesii.core.util.graph.trees.SimpleTree

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.