package nl.camilstaps.cs.graphs; import java.util.ArrayList; import java.util.List; /** * A Node is a simple point in a graph, with references to its neighbours. It holds an integer 'content' which can act * as an identifier. * * @author Camil Staps */ public class Node implements Comparable { private final int content; private final List neighbours = new ArrayList<>(); public Node(int content) { this.content = content; } public int getDegree() { return neighbours.size(); } public List getNeighbourhood() { return neighbours; } public List getInclusiveNeighbourhood() { List nb = new ArrayList<>(); nb.add(this); nb.addAll(neighbours); return nb; } public List getSecondNeighbourhood() { List nb = new ArrayList<>(); for (Node n : getNeighbourhood()) for (Node n2 : n.getNeighbourhood()) if (!nb.contains(n2)) nb.add(n2); nb.remove(this); return nb; } public boolean dominates(Node b) { for (Node nb : getInclusiveNeighbourhood()) if (!b.getInclusiveNeighbourhood().contains(nb)) return false; return true; } public boolean neighbourhoodsDisjoint(Node b) { return neighbourhoodIntersection(b).isEmpty(); } public List neighbourhoodIntersection(Node b) { List intersection = new ArrayList<>(); for (Node n : neighbours) if (b.getNeighbourhood().contains(n)) intersection.add(n); return intersection; } public String toString() { return "<" + content + ">"; } /** * Nodes are equal if their identifiers are equal * @param another * @return */ public boolean equals(Object another) { return !(another == null || another.getClass() != this.getClass()) && content == (((Node) another).content); } @Override public int compareTo(Node node) { return node == null ? 1 : Integer.compare(neighbours.size(), node.neighbours.size()); } }