/**
   * Returns the weight of the clique that would be added to a graph if a given vertex would be
   * removed in the triangulation procedure. The return value is the number of edges in the
   * elimination clique of V that are not already present.
   */
  private int weightRequired(UndirectedGraph mdl, Variable v) {
    int rating = 1;

    for (Iterator it1 = neighborsIterator(mdl, v); it1.hasNext(); ) {
      Variable neighbor = (Variable) it1.next();
      rating *= neighbor.getNumOutcomes();
    }

    //		System.out.println(v+" = "+rating);

    return rating;
  }
  /** Adds edges to graph until it is triangulated. */
  private void triangulate(final UndirectedGraph mdl) {
    UndirectedGraph mdl2 = dupGraph(mdl);
    ArrayList<Variable> vars = new ArrayList<Variable>(mdl.vertexSet());
    Alphabet<Variable> varMap = makeVertexMap(vars);
    cliques = new ArrayList();

    // debug
    if (logger.isLoggable(Level.FINER)) {
      logger.finer("Triangulating model: " + mdl);
      String ret = "";
      for (int i = 0; i < vars.size(); i++) {
        Variable next = (Variable) vars.get(i);
        ret += next.toString() + "\n"; // " (" + mdl.getIndex(next) + ")\n  ";
      }
      logger.finer(ret);
    }

    while (!vars.isEmpty()) {
      Variable v = (Variable) pickVertexToRemove(mdl2, vars);
      logger.finer("Triangulating vertex " + v);

      VarSet varSet = new BitVarSet(v.getUniverse(), GraphHelper.neighborListOf(mdl2, v));
      varSet.add(v);
      if (!findSuperClique(cliques, varSet)) {
        cliques.add(varSet);
        if (logger.isLoggable(Level.FINER)) {
          logger.finer(
              "  Elim clique " + varSet + " size " + varSet.size() + " weight " + varSet.weight());
        }
      }

      // must remove V from graph first, because adding the edges
      //  will change the rating of other vertices

      connectNeighbors(mdl2, v);
      vars.remove(v);
      mdl2.removeVertex(v);
    }

    if (logger.isLoggable(Level.FINE)) {
      logger.fine("Triangulation done. Cliques are: ");
      int totSize = 0, totWeight = 0, maxSize = 0, maxWeight = 0;
      for (Iterator it = cliques.iterator(); it.hasNext(); ) {
        VarSet c = (VarSet) it.next();
        logger.finer(c.toString());
        totSize += c.size();
        maxSize = Math.max(c.size(), maxSize);
        totWeight += c.weight();
        maxWeight = Math.max(c.weight(), maxWeight);
      }
      double sz = cliques.size();
      logger.fine(
          "Jt created "
              + sz
              + " cliques. Size: avg "
              + (totSize / sz)
              + " max "
              + (maxSize)
              + " Weight: avg "
              + (totWeight / sz)
              + " max "
              + (maxWeight));
    }
  }