All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.vangj.jbayes.inf.exact.graph.pptc.Clique Maven / Gradle / Ivy

The newest version!
package com.github.vangj.jbayes.inf.exact.graph.pptc;

import com.github.vangj.jbayes.inf.exact.graph.Node;
import com.github.vangj.jbayes.inf.exact.graph.util.NodeUtil;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * A clique; contains a set of nodes. Used as a node in a join tree.
 */
public class Clique {

  protected boolean marked = false;
  protected Map nodes;

  public Clique() {
    nodes = new LinkedHashMap<>();
  }

  public Clique(Node node, List nodes) {
    this.nodes = new LinkedHashMap<>();
    for (Node n : nodes) {
      this.nodes.put(n.getId(), n);
    }
    this.nodes.put(node.getId(), node);
  }

  public Clique(Node... nodes) {
    this.nodes = new LinkedHashMap<>();
    for (Node n : nodes) {
      this.nodes.put(n.getId(), n);
    }
  }

  /**
   * Checks if this clique is marked.
   *
   * @return Boolean.
   */
  public boolean isMarked() {
    return marked;
  }

  /**
   * Marks this clique.
   */
  public void mark() {
    marked = true;
  }

  /**
   * Unmarks this clique.
   */
  public void unmark() {
    marked = false;
  }

  /**
   * Gets all the nodes in this clique minuse the ones specified by the list of nodes passed in.
   *
   * @param nodes List of nodes.
   * @return List of nodes.
   */
  public List nodesMinus(List nodes) {
    return nodes().stream()
        .filter(node -> {
          return !nodes.contains(node);
        })
        .collect(Collectors.toList());
  }

  /**
   * Checks if this clique is a superset of the specified clique passed in.
   *
   * @param that Clique.
   * @return Boolean.
   */
  public boolean isSuperset(Clique that) {
    Set set1 = new LinkedHashSet<>(this.nodes.values());
    Set set2 = new LinkedHashSet<>(that.nodes.values());
    set1.retainAll(set2);
    return set1.size() == set2.size();
  }

  @Override
  public int hashCode() {
    return toString().hashCode();
  }

  @Override
  public boolean equals(Object object) {
    if (null == object || !(object instanceof Clique)) {
      return false;
    }
    Clique that = (Clique) object;
    return this.hashCode() == that.hashCode();
  }

  /**
   * Weight is defined as product of the number of values for each node.
   *
   * @return Weight.
   */
  public int weight() {
    int weight = 1;
    for (Map.Entry entry : nodes.entrySet()) {
      weight *= entry.getValue().weight();
    }
    return weight;
  }

  /**
   * Gets the nodes.
   *
   * @return List of nodes
   */
  public List nodes() {
    return nodes.values().stream().collect(Collectors.toList());
  }

  /**
   * Checks if this clique contains the node associated with the specified id.
   *
   * @param id Id.
   * @return Boolean.
   */
  public boolean contains(String id) {
    return nodes.containsKey(id);
  }

  /**
   * Creates a separation set from this clique and the clique passed in. The separation set should
   * be the intersection of the nodes between this clique and the one passed in.
   *
   * @param that Clique.
   * @return Separation set.
   */
  public SepSet sepSet(Clique that) {
    return new SepSet(this, that);
  }

  /**
   * Gets the id of this node. Composed of the lexicographically ordered ids of the nodes in this
   * clique.
   *
   * @return Id.
   */
  public String id() {
    return NodeUtil.id(nodes());
  }

  @Override
  public String toString() {
    return id();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy