public static Dag markovBlanketDag(Node target,Graph dag){
  if (dag.getNode(target.getName()) == null) {
    throw new NullPointerException("Target node not in graph: " + target);
  }
  Graph blanket=new EdgeListGraph();
  blanket.addNode(target);
  List<Node> parents=dag.getParents(target);
  for (  Object parent1 : parents) {
    Node parent=(Node)parent1;
    blanket.addNode(parent);
    blanket.addDirectedEdge(parent,target);
  }
  List<Node> children=dag.getChildren(target);
  List<Node> parentsOfChildren=new LinkedList<>();
  for (  Object aChildren : children) {
    Node child=(Node)aChildren;
    if (!blanket.containsNode(child)) {
      blanket.addNode(child);
    }
    blanket.addDirectedEdge(target,child);
    List<Node> parentsOfChild=dag.getParents(child);
    parentsOfChild.remove(target);
    for (    Object aParentsOfChild : parentsOfChild) {
      Node parentOfChild=(Node)aParentsOfChild;
      if (!parentsOfChildren.contains(parentOfChild)) {
        parentsOfChildren.add(parentOfChild);
      }
      if (!blanket.containsNode(parentOfChild)) {
        blanket.addNode(parentOfChild);
      }
      blanket.addDirectedEdge(parentOfChild,child);
    }
  }
  parentsOfChildren.removeAll(parents);
  for (  Object parent2 : parents) {
    Node parent=(Node)parent2;
    for (    Object aParentsOfChildren : parentsOfChildren) {
      Node parentOfChild=(Node)aParentsOfChildren;
      Edge edge1=dag.getEdge(parent,parentOfChild);
      Edge edge2=blanket.getEdge(parent,parentOfChild);
      if (edge1 != null && edge2 == null) {
        Edge newEdge=new Edge(parent,parentOfChild,edge1.getProximalEndpoint(parent),edge1.getProximalEndpoint(parentOfChild));
        blanket.addEdge(newEdge);
      }
    }
  }
  for (  Object aChildren1 : children) {
    Node child=(Node)aChildren1;
    for (    Object aParentsOfChildren : parentsOfChildren) {
      Node parentOfChild=(Node)aParentsOfChildren;
      Edge edge1=dag.getEdge(child,parentOfChild);
      Edge edge2=blanket.getEdge(child,parentOfChild);
      if (edge1 != null && edge2 == null) {
        Edge newEdge=new Edge(child,parentOfChild,edge1.getProximalEndpoint(child),edge1.getProximalEndpoint(parentOfChild));
        blanket.addEdge(newEdge);
      }
    }
  }
  return new Dag(blanket);
}

public static Dag markovBlanketDag ( Node target , Graph dag ) { if ( dag . getNode ( target . getName ( ) ) == null ) { throw new NullPointerException ( "Target node not in graph: " + target ) ; } Graph blanket = new EdgeListGraph ( ) ; blanket . addNode ( target ) ; List < Node > parents = dag . getParents ( target ) ; for ( Object parent1 : parents ) { Node parent = ( Node ) parent1 ; blanket . addNode ( parent ) ; blanket . addDirectedEdge ( parent , target ) ; } List < Node > children = dag . getChildren ( target ) ; List < Node > parentsOfChildren = new LinkedList < > ( ) ; for ( Object aChildren : children ) { Node child = ( Node ) aChildren ; if ( ! blanket . containsNode ( child ) ) { blanket . addNode ( child ) ; } blanket . addDirectedEdge ( target , child ) ; List < Node > parentsOfChild = dag . getParents ( child ) ; parentsOfChild . remove ( target ) ; for ( Object aParentsOfChild : parentsOfChild ) { Node parentOfChild = ( Node ) aParentsOfChild ; if ( ! parentsOfChildren . contains ( parentOfChild ) ) { parentsOfChildren . add ( parentOfChild ) ; } if ( ! blanket . containsNode ( parentOfChild ) ) { blanket . addNode ( parentOfChild ) ; } blanket . addDirectedEdge ( parentOfChild , child ) ; } } parentsOfChildren . removeAll ( parents ) ; for ( Object parent2 : parents ) { Node parent = ( Node ) parent2 ; for ( Object aParentsOfChildren : parentsOfChildren ) { Node parentOfChild = ( Node ) aParentsOfChildren ; Edge edge1 = dag . getEdge ( parent , parentOfChild ) ; Edge edge2 = blanket . getEdge ( parent , parentOfChild ) ; if ( edge1 != null && edge2 == null ) { Edge newEdge = new Edge ( parent , parentOfChild , edge1 . getProximalEndpoint ( parent ) , edge1 . getProximalEndpoint ( parentOfChild ) ) ; blanket . addEdge ( newEdge ) ; } } } for ( Object aChildren1 : children ) { Node child = ( Node ) aChildren1 ; for ( Object aParentsOfChildren : parentsOfChildren ) { Node parentOfChild = ( Node ) aParentsOfChildren ; Edge edge1 = dag . getEdge ( child , parentOfChild ) ; Edge edge2 = blanket . getEdge ( child , parentOfChild ) ; if ( edge1 != null && edge2 == null ) { Edge newEdge = new Edge ( child , parentOfChild , edge1 . getProximalEndpoint ( child ) , edge1 . getProximalEndpoint ( parentOfChild ) ) ; blanket . addEdge ( newEdge ) ; } } } return new Dag ( blanket ) ; }
calculates the markov blanket of a target in a dag . this includes the target , the parents of the target , the children of the target , the parents of the children of the target , edges from parents to target , target to children , parents of children to children , and parent to parents of children . ( edges among children are implied by the inclusion of edges from parents of children to children . ) edges among parents and among parents of children not explicitly included above are not included . ( joseph ramsey 8 / 6 / 04 )
