aboutsummaryrefslogtreecommitdiff
path: root/Practical1/src/nl/camilstaps/cs/graphs/Graph.java
diff options
context:
space:
mode:
Diffstat (limited to 'Practical1/src/nl/camilstaps/cs/graphs/Graph.java')
-rw-r--r--Practical1/src/nl/camilstaps/cs/graphs/Graph.java68
1 files changed, 68 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();
+ }
+}