Пример #1
1
 private Queue<SearchNode> GetSolution(Board initial) {
   Queue<SearchNode> queue = new Queue<SearchNode>();
   MinPQ<SearchNode> pq = new MinPQ<SearchNode>();
   SearchNode node = new SearchNode();
   node.brd = initial;
   node.moves = 0;
   // node.priority = initial.manhattan();
   node.prev = null;
   pq.insert(node);
   SearchNode curNode = null;
   while (!pq.isEmpty()) {
     curNode = pq.delMin();
     if (curNode.brd.isGoal()) break;
     Iterable<Board> qe = curNode.brd.neighbors();
     Iterator it = qe.iterator();
     while (it.hasNext()) {
       node.brd = (Board) it.next();
       node.moves = curNode.moves + 1;
       node.prev = curNode;
       // node.priority = node.brd.manhattan();
       queue.enqueue(node);
     }
   }
   if (queue.isEmpty()) return null;
   else {
     do {
       queue.enqueue(curNode);
       curNode = curNode.prev;
     } while (curNode != null);
     return queue;
   }
 }
Пример #2
0
 /** Unit tests the <tt>MinPQ</tt> data type. */
 public static void main(String[] args) {
   MinPQ<String> pq = new MinPQ<String>();
   while (!StdIn.isEmpty()) {
     String item = StdIn.readString();
     if (!item.equals("-")) pq.insert(item);
     else if (!pq.isEmpty()) StdOut.print(pq.delMin() + " ");
   }
   StdOut.println("(" + pq.size() + " left on pq)");
 }
Пример #3
0
 KruskalMST(EdgeWeightedGraph G) {
   MinPQ<Edge> pq = new MinPQ<Edge>(); // we dont need to pass a new comparator let it
   // follow the natural ordering of edges
   double temp_wt = 0;
   for (Edge e : G.edges()) pq.insert(e);
   UF set = new UF(G.V());
   while (!pq.isEmpty() && mst.size() < (G.V() - 1)) {
     Edge e = pq.delMin();
     int v = e.either();
     int w = e.other(v);
     if (!set.connected(v, w)) {
       set.union(v, w);
       mst.add(e);
       temp_wt += e.weight();
     }
   }
   this.weight = temp_wt;
 }
Пример #4
0
  // Kruskal's algorithm
  public KruskalMST(EdgeWeightedGraph G) {
    // more efficient to build heap by passing array of edges
    MinPQ<Edge> pq = new MinPQ<Edge>();
    for (Edge e : G.edges()) {
      pq.insert(e);
    }

    // run greedy algorithm
    UF uf = new UF(G.V());
    while (!pq.isEmpty() && mst.size() < G.V() - 1) {
      Edge e = pq.delMin();
      int v = e.either();
      int w = e.other(v);
      if (!uf.connected(v, w)) { // v-w does not create a cycle
        uf.union(v, w); // merge v and w components
        mst.enqueue(e); // add edge e to mst
        weight += e.weight();
      }
    }

    // check optimality conditions
    assert check(G);
  }
Пример #5
0
  public LazyPrimMST(EdgeWeightedGraph G) {
    pq = new MinPQ<Edge>();
    marked = new boolean[G.V()];
    mst = new Queue<Edge>();

    visit(G, 0); // assumes G is connected (see ex. 4.3.22)
    while (!pq.isEmpty()) {
      Edge e = pq.delMin(); // Get lowest-weight from pq.
      int v = e.either();
      int w = e.other(v);
      if (marked[v] && marked[w]) {
        continue; // Skip if ineligible
      }
      mst.enqueue(e); // Add edge to tree
      if (!marked[v]) { // Add vertex to tree (either v or w).
        visit(G, v);
      }

      if (!marked[w]) {
        visit(G, w);
      }
    }
  }
Пример #6
0
  /**
   * Dado un grafo no dirigido y con pesos, ejecuta el algoritmo de Kruskal para la obtención del su
   * árbol de cobertura mínima.
   *
   * @param G Grafo no dirigido con pesos al cual se le deesea obtener su arbol de cobertura mínima.
   */
  public void minimumSpanningTree(EdgeWeightedGraph G) // By Kruskal's algorithm
      {
    LinkedList<Edge> mst = new LinkedList<>();

    MinPQ<Edge> pq = new MinPQ<>(G.E());
    for (Edge edge : G.edges()) // Bag<Edge> != Comparator<Edge> :c
    pq.insert(edge);

    UF uf = new UF(G.V());

    while (!pq.isEmpty() && mst.size() < G.V() - 1) {
      Edge edge = pq.delMin();
      int v = edge.either(), w = edge.other(v);

      if (!uf.connected(v, w)) {
        uf.union(v, w);
        mst.add(edge);
      }
    }

    System.out.println("");
    for (Edge edge : mst) System.out.println(edge);
  }
Пример #7
0
  private void solveInternal(MinPQ<SearchNode> queue, boolean twin) {

    while (!queue.isEmpty() && solvable == null) {

      //            StdOut.println("Queue size = "+queue.size());
      SearchNode min = queue.delMin();
      //            StdOut.println("Manhatan + moves = "+(min.board.manhattan()+min.moves));
      if (min.board.isGoal()) {
        if (twin) {
          solvable = false;
        } else {
          solution = min;
          solvable = true;
        }
        return;
      } else {
        for (Board n : min.board.neighbors()) {
          if (min.previous == null || (!n.equals(min.previous.board))) {
            queue.insert(new SearchNode(n, min, min.moves + 1));
          }
        }
      }
    }
  }
Пример #8
0
 public boolean hasNext() {
   return !copy.isEmpty();
 }
Пример #9
0
 public boolean isEmpty() {
   return min.isEmpty() && max.isEmpty();
 }