Ejemplo n.º 1
0
  public void testEqualKeys() {
    PriorityQueue q = new MinHeap(N);
    Item[] items = new Item[20];
    int j = 0;

    for (int i = 0; i < 5; i++) {
      items[j] = new Item(5);
      q.insert(items[j]);
      j++;
    }
    for (int i = 0; i < 5; i++) {
      items[j] = new Item(3);
      q.insert(items[j]);
      j++;
    }
    for (int i = 0; i < 5; i++) {
      items[j] = new Item(4);
      q.insert(items[j]);
      j++;
    }
    for (int i = 0; i < 5; i++) {
      items[j] = new Item(7);
      q.insert(items[j]);
      j++;
    }

    assertEquals(20, q.size());
    for (int i = 0; i < items.length; i++) {
      assertTrue(q.contains(items[i]));
    }

    for (int i = 0; i < 5; i++) {
      QueueElement e = q.extractMin();
      assertTrue(q.contains(q.min()));
      assertEquals(3.0, e.getPriority());
    }
    for (int i = 0; i < 5; i++) {
      QueueElement e = q.extractMin();
      assertTrue(q.contains(q.min()));
      assertEquals(4.0, e.getPriority());
    }
    for (int i = 0; i < 5; i++) {
      QueueElement e = q.extractMin();
      assertTrue(q.contains(q.min()));
      assertEquals(5.0, e.getPriority());
    }
    for (int i = 0; i < 5; i++) {
      QueueElement e = q.extractMin();
      if (q.size() > 0) assertTrue(q.contains(q.min()));
      assertEquals(7.0, e.getPriority());
    }
  }
Ejemplo n.º 2
0
  public void printProofTree(Clause finalClause, LinkedList<Clause> clauseList) {
    PriorityQueue<Integer> proofTree =
        new PriorityQueue<
            Integer>(); // Will be used to order the ancestors of the finalClause for output
    LinkedList<Clause> treeQueue =
        new LinkedList<
            Clause>(); // Will take in the ancestors of the finalClause. Dequeue each element, add
                       // it to the proofTree, then add the parents to the queue
    int[] parentIDs;

    treeQueue.add(finalClause);
    while (!treeQueue.isEmpty()) {
      Clause polledClause = treeQueue.poll();

      if (proofTree.contains(
          polledClause
              .getClauseID())) // Skip this iteration if the clause has already been added to the
                               // proofTree
      {
        continue;
      }
      proofTree.add(polledClause.getClauseID());
      parentIDs = polledClause.getParentIDs();
      if (parentIDs[0]
          != -1) // if one parent exists, the other must exist and we add the parents to the queue
      {
        treeQueue.add(clauseList.get(parentIDs[0] - 1)); // add the first parent to the queue
        treeQueue.add(clauseList.get(parentIDs[1] - 1)); // add the second parent to the queue
      }
    }

    // output all the clauses in the proof tree
    while (proofTree.peek() != null) {
      clauseList.get(proofTree.poll() - 1).outputClause();
    }
  }
Ejemplo n.º 3
0
  public Route start(DateTime time) {
    this.startTime = time;
    ArrayList<FakeNode> neighbouringNodes;
    hScore(startNode);
    openSet.add(startNode);

    while (!openSet.isEmpty()) {
      currentNode = openSet.poll();
      neighbouringNodes = getNeighbours(currentNode.id);

      for (FakeNode neighbour : neighbouringNodes) {
        if (neighbour.visited == true) {
          continue;
        } else if (openSet.contains(neighbour)) {
          if (neighbour.g_scores > gScore(neighbour)) {
            neighbour.cameFrom = currentNode;
            calcScores(neighbour);
          }
          continue;
        } else {
          neighbour.cameFrom = currentNode;
        }

        if (endNode == neighbour) {
          calcScores(neighbour);
          return reconstructPath();
        }

        calcScores(neighbour);

        openSet.add(neighbour);
      }
      currentNode.visited = true;
    }
    return null;
  }