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(); }
public static boolean contains_descendent(Vector evidence, AbstractVariable a) throws RemoteException { AbstractVariable[] children = a.get_children(); if (children == null) return false; for (int i = 0; i < children.length; i++) { if (evidence.contains(children[i])) return true; else if (contains_descendent(evidence, children[i])) return true; } return false; }
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; }