@Test public void test() throws Exception { Main.main(null); }
public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/dataWeka/asia.bn"); DAG dag = bn.getDAG(); System.out.println(dag.toString()); Variable var1 = dag.getVariables().getVariableByName("D"); Variable var2 = dag.getVariables().getVariableByName("A"); Map<Variable, Long> map = dag.getParentSet(var1).getParents().stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); while (!map.containsKey(var2) && !map.entrySet().isEmpty()) { map = map.entrySet().parallelStream() .flatMap(entry -> dag.getParentSet(entry.getKey()).getParents().stream().map(v -> new AbstractMap.SimpleEntry<Variable, Long>(v, 1 + entry.getValue()))) .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (a, b) -> (a < b) ? a : b)); } if (map.containsKey(var2)) { System.out.println("Distance from " + var2.getName() + " to " + var1.getName() + " is " + map.get(var2)); return; } }
Main { public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/dataWeka/asia.bn"); DAG dag = bn.getDAG(); System.out.println(dag.toString()); Variable var1 = dag.getVariables().getVariableByName("D"); Variable var2 = dag.getVariables().getVariableByName("A"); Map<Variable, Long> map = dag.getParentSet(var1).getParents().stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); while (!map.containsKey(var2) && !map.entrySet().isEmpty()) { map = map.entrySet().parallelStream() .flatMap(entry -> dag.getParentSet(entry.getKey()).getParents().stream().map(v -> new AbstractMap.SimpleEntry<Variable, Long>(v, 1 + entry.getValue()))) .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (a, b) -> (a < b) ? a : b)); } if (map.containsKey(var2)) { System.out.println("Distance from " + var2.getName() + " to " + var1.getName() + " is " + map.get(var2)); return; } } }
Main { public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/dataWeka/asia.bn"); DAG dag = bn.getDAG(); System.out.println(dag.toString()); Variable var1 = dag.getVariables().getVariableByName("D"); Variable var2 = dag.getVariables().getVariableByName("A"); Map<Variable, Long> map = dag.getParentSet(var1).getParents().stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); while (!map.containsKey(var2) && !map.entrySet().isEmpty()) { map = map.entrySet().parallelStream() .flatMap(entry -> dag.getParentSet(entry.getKey()).getParents().stream().map(v -> new AbstractMap.SimpleEntry<Variable, Long>(v, 1 + entry.getValue()))) .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (a, b) -> (a < b) ? a : b)); } if (map.containsKey(var2)) { System.out.println("Distance from " + var2.getName() + " to " + var1.getName() + " is " + map.get(var2)); return; } }  }
Main { public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/dataWeka/asia.bn"); DAG dag = bn.getDAG(); System.out.println(dag.toString()); Variable var1 = dag.getVariables().getVariableByName("D"); Variable var2 = dag.getVariables().getVariableByName("A"); Map<Variable, Long> map = dag.getParentSet(var1).getParents().stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); while (!map.containsKey(var2) && !map.entrySet().isEmpty()) { map = map.entrySet().parallelStream() .flatMap(entry -> dag.getParentSet(entry.getKey()).getParents().stream().map(v -> new AbstractMap.SimpleEntry<Variable, Long>(v, 1 + entry.getValue()))) .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (a, b) -> (a < b) ? a : b)); } if (map.containsKey(var2)) { System.out.println("Distance from " + var2.getName() + " to " + var1.getName() + " is " + map.get(var2)); return; } }  static void test1(String[] args); static void test2(String[] args); static void main(String[] args); }
Main { public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/dataWeka/asia.bn"); DAG dag = bn.getDAG(); System.out.println(dag.toString()); Variable var1 = dag.getVariables().getVariableByName("D"); Variable var2 = dag.getVariables().getVariableByName("A"); Map<Variable, Long> map = dag.getParentSet(var1).getParents().stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); while (!map.containsKey(var2) && !map.entrySet().isEmpty()) { map = map.entrySet().parallelStream() .flatMap(entry -> dag.getParentSet(entry.getKey()).getParents().stream().map(v -> new AbstractMap.SimpleEntry<Variable, Long>(v, 1 + entry.getValue()))) .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (a, b) -> (a < b) ? a : b)); } if (map.containsKey(var2)) { System.out.println("Distance from " + var2.getName() + " to " + var1.getName() + " is " + map.get(var2)); return; } }  static void test1(String[] args); static void test2(String[] args); static void main(String[] args);  }