/*
   * Recover the maximizing variables going back through the
   * maximizing bucket_tree; the variables are returned as an array
   * of markers (non-explanation variables get INVALID_INDEX).
   */
  private int[] backward_maximization() {
    int i, j;
    int bi = bucket_tree.length - 1;
    DiscreteFunction back_df;
    Bucket b = bucket_tree[bi];

    // If there are no explanation variables in the BayesNet, return null
    if (b.backward_pointers == null) return (null);

    // Initialize the markers for backward pointers with INVALID_INDEX
    int backward_markers[] = new int[bn.number_variables()];
    for (i = 0; i < backward_markers.length; i++) backward_markers[i] = BayesNet.INVALID_INDEX;

    // Initialize the marker for the last bucket
    backward_markers[b.variable.get_index()] = (int) (b.backward_pointers.get_value(0) + 0.5);

    // Go backwards through the bucket_tree
    for (i = (bi - 1); i >= 0; i--) {
      if (!bucket_tree[i].is_explanation()) break;
      back_df = bucket_tree[i].backward_pointers;
      // Skip null pointers (caused by evidence)
      if (back_df == null) continue;
      // Special treatment for bucket with only one value,
      // since it can be a bucket with only the bucket variable left
      if (back_df.number_values() == 1) {
        backward_markers[bucket_tree[i].variable.get_index()] = (int) (back_df.get_value(0) + 0.5);
        continue;
      }
      // Process the bucket
      j = back_df.get_position_from_indexes(bn.get_probability_variables(), backward_markers);
      backward_markers[bucket_tree[i].variable.get_index()] = (int) (back_df.get_value(j) + 0.5);
    }

    return (backward_markers);
  }
 /*
  * Put a DiscreteFunction into the BucketTree beyond the current
  * active_bucket. If was_first_variable_cancelled_by_evidence is true,
  * then mark the bucket accordingly.
  */
 private void insert(DiscreteFunction df, boolean was_first_variable_cancelled_by_evidence) {
   int i, index;
   Bucket b;
   for (i = active_bucket; i < bucket_tree.length; i++) {
     index = bucket_tree[i].variable.get_index();
     if (df.memberOf(index)) {
       bucket_tree[i].discrete_functions.addElement(df);
       // If the function is a ProbabilityFunction, store its
       // first variable appropriately (assuming for now that
       // the first variable is the only possible non-conditioning variable).
       if ((df instanceof ProbabilityFunction) && (!was_first_variable_cancelled_by_evidence)) {
         bucket_tree[i].non_conditioning_variables.addElement(df.get_variable(0));
       }
       return;
     }
   }
 }
 /** Print method for BucketTree. */
 public void print(PrintStream out) {
   out.println("BucketTree:" + "\n\tActive Bucket is " + active_bucket + ".");
   for (int i = 0; i < bucket_tree.length; i++) bucket_tree[i].print(out);
   out.println("Bucket result: ");
   unnormalized_result.print(out);
 }