aboutsummaryrefslogtreecommitdiff
path: root/Practical1/src/nl/camilstaps/cs/graphs
diff options
context:
space:
mode:
Diffstat (limited to 'Practical1/src/nl/camilstaps/cs/graphs')
-rw-r--r--Practical1/src/nl/camilstaps/cs/graphs/Graph.java68
-rw-r--r--Practical1/src/nl/camilstaps/cs/graphs/Node.java46
2 files changed, 114 insertions, 0 deletions
diff --git a/Practical1/src/nl/camilstaps/cs/graphs/Graph.java b/Practical1/src/nl/camilstaps/cs/graphs/Graph.java
new file mode 100644
index 0000000..ef89258
--- /dev/null
+++ b/Practical1/src/nl/camilstaps/cs/graphs/Graph.java
@@ -0,0 +1,68 @@
+package nl.camilstaps.cs.graphs;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * A Graph is a list of Nodes.
+ *
+ * @author Camil Staps
+ */
+public class Graph {
+ protected final List<Node> nodes;
+
+ public Graph() {
+ this.nodes = new ArrayList<>();
+ }
+
+ /**
+ * Create a copy of another Graph, also copying all the Nodes
+ * @param g
+ */
+ public Graph(Graph g) {
+ this();
+ this.nodes.addAll(g.nodes.stream().map(Node::new).collect(Collectors.toList()));
+ }
+
+ /**
+ * Add a Node
+ * @param node
+ */
+ public void addNode(Node node) {
+ this.nodes.add(node);
+ }
+
+ /**
+ * Get a Node
+ * @param i
+ * @return
+ */
+ public Node getNode(int i) {
+ return this.nodes.get(i);
+ }
+
+ /**
+ * Remove a node
+ * @param n
+ */
+ public void removeNode(Node n) {
+ nodes.remove(n);
+ }
+
+ /**
+ * Remove a list of nodes, and update all references
+ * @param ns
+ */
+ public void removeNodes(Collection<Node> ns) {
+ nodes.removeAll(ns);
+ }
+
+ public String toString() {
+ StringBuilder sb = new StringBuilder("Graph:");
+ for (Node n : nodes) {
+ sb.append("\n ").append(n).append(": ");
+ for (Node n2 : n.getNeighbours()) sb.append(" - ").append(n2);
+ }
+ return sb.toString();
+ }
+}
diff --git a/Practical1/src/nl/camilstaps/cs/graphs/Node.java b/Practical1/src/nl/camilstaps/cs/graphs/Node.java
new file mode 100644
index 0000000..87471e8
--- /dev/null
+++ b/Practical1/src/nl/camilstaps/cs/graphs/Node.java
@@ -0,0 +1,46 @@
+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 {
+ private final int content;
+ private final List<Node> neighbours = new ArrayList<>();
+
+ public Node(int content) {
+ this.content = content;
+ }
+
+ /**
+ * Create a copy of another Node
+ * @param copy
+ */
+ public Node(Node copy) {
+ this.content = copy.content;
+ this.neighbours.addAll(copy.neighbours);
+ }
+
+ public List<Node> getNeighbours() {
+ return neighbours;
+ }
+
+ 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);
+ }
+}