示例#1
0
  /**
   * Returns null if the belief network <tt>bn</tt> has no directed cycles. Otherwise returns an
   * <tt>Enumeration</tt> of variables, which begins at a variable contained in a directed cycle,
   * leads through the cycle (parent by parent), and ends at that same variable.
   */
  public static Enumeration has_directed_cycle(AbstractBeliefNetwork bn) throws RemoteException {
    // If is_ancestor returns true, then this stack contains a path from some variable all the
    // way back to that same variable. If is_ancestor returns false, then this stack remains empty.
    Stack path_stack = new Stack();

    AbstractVariable[] u = bn.get_variables();
    for (int i = 0; i < u.length; i++) {
      if (is_ancestor(u[i], u[i], path_stack)) {
        path_stack.push(u[i]);
        return path_stack.elements();
      }
    }

    return null;
  }
示例#2
0
  public static boolean is_descendent(
      AbstractVariable possible_descendent, AbstractVariable x, Stack path_stack)
      throws RemoteException {
    AbstractVariable[] children = x.get_children();
    for (int i = 0; i < children.length; i++) {
      if (children[i] == possible_descendent) {
        path_stack.push(children[i]);
        return true;
      } else if (is_descendent(possible_descendent, children[i], path_stack)) {
        path_stack.push(children[i]);
        return true;
      }
    }

    // If we didn't find the descendent, don't add anything to the path_stack.
    // This saves a lot of useless pushing and popping, since ordinarily few paths
    // will be directed cycles.
    return false;
  }
示例#3
0
  public static boolean is_ancestor(
      AbstractVariable possible_ancestor, AbstractVariable x, Stack path_stack)
      throws RemoteException {
    AbstractVariable[] parents = x.get_parents();
    for (int i = 0; i < parents.length; i++) {
      if (parents[i] == possible_ancestor) {
        path_stack.push(parents[i]);
        return true;
      } else if (is_ancestor(possible_ancestor, parents[i], path_stack)) {
        path_stack.push(parents[i]);
        return true;
      }
    }

    // If we didn't find the ancestor, don't add anything to the path_stack.
    // This saves a lot of useless pushing and popping, since ordinarily few paths
    // will be directed cycles.
    return false;
  }
示例#4
0
  public static void find_all_paths(
      AbstractVariable x, AbstractVariable end, Vector path_set, Stack path_stack)
      throws RemoteException {
    int i;

    path_stack.push(x);

    if (x == end) {
      // Construct a path from the beginning to the end, using what's on the stack.

      AbstractVariable[] path = new AbstractVariable[path_stack.size()];

      // System.err.println( "\tFound path: " );
      Enumeration e;
      for (i = 0, e = path_stack.elements(); e.hasMoreElements(); i++) {
        path[i] = (AbstractVariable) e.nextElement();
        // System.err.print( path[i].get_name()+" " );
      }
      // System.err.println("");

      path_set.addElement(path);
      path_stack.pop();
      return;
    }

    AbstractVariable[] parents = x.get_parents();
    for (i = 0; i < parents.length; i++) {
      Enumeration e = path_stack.elements();
      boolean is_on_stack = false;
      while (e.hasMoreElements())
        if (e.nextElement() == parents[i]) {
          is_on_stack = true;
          break;
        }

      if (!is_on_stack) find_all_paths(parents[i], end, path_set, path_stack);
    }

    AbstractVariable[] children = x.get_children();
    for (i = 0; i < children.length; i++) {
      Enumeration e = path_stack.elements();
      boolean is_on_stack = false;
      while (e.hasMoreElements())
        if (e.nextElement() == children[i]) {
          is_on_stack = true;
          break;
        }

      if (!is_on_stack) find_all_paths(children[i], end, path_set, path_stack);
    }

    path_stack.pop();
  }