aboutsummaryrefslogtreecommitdiff
path: root/Practical1/src/nl/camilstaps/cs/graphs/Node.java
diff options
context:
space:
mode:
authorCamil Staps2015-10-23 12:35:46 +0200
committerCamil Staps2015-10-23 12:35:46 +0200
commit78c8b3bd7dd49700c8607093304c599e2313f346 (patch)
treecefbaec6a4ad0a96edd303f023c69b3aa3b9a077 /Practical1/src/nl/camilstaps/cs/graphs/Node.java
parentRobson's algorithm; added test outputs; more test cases; tester bugfix (diff)
Practical1: comments; Java8 fancy stuff; enhancements tester
Diffstat (limited to 'Practical1/src/nl/camilstaps/cs/graphs/Node.java')
-rw-r--r--Practical1/src/nl/camilstaps/cs/graphs/Node.java59
1 files changed, 44 insertions, 15 deletions
diff --git a/Practical1/src/nl/camilstaps/cs/graphs/Node.java b/Practical1/src/nl/camilstaps/cs/graphs/Node.java
index 4edc666..b9d078e 100644
--- a/Practical1/src/nl/camilstaps/cs/graphs/Node.java
+++ b/Practical1/src/nl/camilstaps/cs/graphs/Node.java
@@ -2,6 +2,7 @@ package nl.camilstaps.cs.graphs;
import java.util.ArrayList;
import java.util.List;
+import java.util.stream.Collectors;
/**
* A Node is a simple point in a graph, with references to its neighbours. It holds an integer 'content' which can act
@@ -17,14 +18,23 @@ public class Node implements Comparable<Node> {
this.content = content;
}
+ /**
+ * @return the number of neighbours
+ */
public int getDegree() {
return neighbours.size();
}
+ /**
+ * @return the neighbours
+ */
public List<Node> getNeighbourhood() {
return neighbours;
}
+ /**
+ * @return the neighbours with the Node itself
+ */
public List<Node> getInclusiveNeighbourhood() {
List<Node> nb = new ArrayList<>();
nb.add(this);
@@ -32,16 +42,22 @@ public class Node implements Comparable<Node> {
return nb;
}
+ /**
+ * @return the neighbours of the neighbours, without this Node itself
+ */
public List<Node> getSecondNeighbourhood() {
List<Node> nb = new ArrayList<>();
for (Node n : getNeighbourhood())
- for (Node n2 : n.getNeighbourhood())
- if (!nb.contains(n2))
- nb.add(n2);
+ n.getNeighbourhood().stream().filter(n2 -> !nb.contains(n2)).forEach(nb::add);
nb.remove(this);
return nb;
}
+ /**
+ * Node a dominates a Node b if the inclusive neighbourhood of a is a subset of the inclusive neighbourhood of b.
+ * @param b the Node to compare to
+ * @return true iff this Node dominates Node b
+ */
public boolean dominates(Node b) {
for (Node nb : getInclusiveNeighbourhood())
if (!b.getInclusiveNeighbourhood().contains(nb))
@@ -49,34 +65,47 @@ public class Node implements Comparable<Node> {
return true;
}
+ /**
+ * @param b the Node to compare to
+ * @return true iff the neighbourhood of this Node is disjoint with the neighbourhood of Node b
+ */
public boolean neighbourhoodsDisjoint(Node b) {
return neighbourhoodIntersection(b).isEmpty();
}
+ /**
+ * @param b another Node
+ * @return the intersection of the neighbourhood of this Node and the neighbourhood of Node b
+ */
public List<Node> neighbourhoodIntersection(Node b) {
- List<Node> intersection = new ArrayList<>();
- for (Node n : neighbours)
- if (b.getNeighbourhood().contains(n))
- intersection.add(n);
- return intersection;
+ return neighbours.stream().filter(n -> b.getNeighbourhood().contains(n)).collect(Collectors.toList());
}
+ /**
+ * @return a String representation of the Node
+ */
public String toString() {
return "<" + content + ">";
}
/**
* Nodes are equal if their identifiers are equal
- * @param another
- * @return
+ * @param b the Node to compare to
+ * @return true iff this Node is equivalent to Node b
*/
- public boolean equals(Object another) {
- return !(another == null || another.getClass() != this.getClass()) &&
- content == (((Node) another).content);
+ public boolean equals(Object b) {
+ return !(b == null || b.getClass() != this.getClass()) &&
+ content == (((Node) b).content);
}
+ /**
+ * This method allows sorting of Nodes by their degrees.
+ * @param b the Node to compare to
+ * @return 1 if d(this) > d(b), -1 if d(this) < d(b), 0 otherwise
+ */
@Override
- public int compareTo(Node node) {
- return node == null ? 1 : Integer.compare(neighbours.size(), node.neighbours.size());
+ @SuppressWarnings("NullableProblems")
+ public int compareTo(Node b) {
+ return Integer.compare(neighbours.size(), b.neighbours.size());
}
}