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(); }
/** * 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; }