diff --git a/src/main/java/INF102/lab5/graph/AdjacencySet.java b/src/main/java/INF102/lab5/graph/AdjacencySet.java index 0f16df7599156c66f2496ec9a65160bf63aca008..ce872ab3ff3c275fcf50bf346f549256392b966a 100644 --- a/src/main/java/INF102/lab5/graph/AdjacencySet.java +++ b/src/main/java/INF102/lab5/graph/AdjacencySet.java @@ -8,22 +8,22 @@ import java.util.Set; public class AdjacencySet<V> implements IGraph<V> { - private Set<V> nodes; - private Map<V, Set<V>> nodeToNode; + private Set<V> vertices; + private Map<V, Set<V>> adjacencyList; public AdjacencySet() { - nodes = new HashSet<>(); - nodeToNode = new HashMap<>(); + vertices = new HashSet<>(); + adjacencyList = new HashMap<>(); } @Override public int size() { - return nodes.size(); + return vertices.size(); } @Override - public Set<V> getNodes() { - return Collections.unmodifiableSet(nodes); + public Iterable<V> vertices() { + return Collections.unmodifiableSet(vertices); } @Override @@ -52,20 +52,20 @@ public class AdjacencySet<V> implements IGraph<V> { } @Override - public boolean hasEdge(V u, V v) { - return nodeToNode.get(u).contains(v); + public boolean adjacent(V u, V v) { + return adjacencyList.get(u).contains(v); } @Override - public Set<V> getNeighbourhood(V node) { - return Collections.unmodifiableSet(nodeToNode.get(node)); + public Set<V> neighbours(V node) { + return Collections.unmodifiableSet(adjacencyList.get(node)); } @Override public String toString() { StringBuilder build = new StringBuilder(); - for (V node : nodeToNode.keySet()) { - Set<V> nodeList = nodeToNode.get(node); + for (V node : adjacencyList.keySet()) { + Set<V> nodeList = adjacencyList.get(node); build.append(node); build.append(" --> "); diff --git a/src/main/java/INF102/lab5/graph/IGraph.java b/src/main/java/INF102/lab5/graph/IGraph.java index 0977314bd2b99b9d4fb1fbd513eca53369198a62..f14dbe664fb72333c7b53f4cd9ecbaa9b3b4cef1 100644 --- a/src/main/java/INF102/lab5/graph/IGraph.java +++ b/src/main/java/INF102/lab5/graph/IGraph.java @@ -11,10 +11,10 @@ public interface IGraph<V> { public int size(); /** - * Get all nodes in graph + * A way to iterate through all vertices of this graph * @return set of graph nodes */ - public Set<V> getNodes(); + public Iterable<V> vertices(); /** * Add <code>node</code> to graph @@ -44,19 +44,19 @@ public interface IGraph<V> { public void removeEdge(V u, V v); /** - * Checks if there is a node <code>node</code> in the graph + * Checks if the given <code>node</code> is in the graph * @param node * @return true if node is in graph */ public boolean hasNode(V node); /** - * Checks if there is an edge between node <code>u</code> and <code>v</code> - * @param u - * @param v - * @return true if there is an edge between <code>u</code> and <code>v</code> + * Checks if two given vertices are adjacent + * + * @return true if both vertices are in the graph and there is an edge between + * them in the graph */ - public boolean hasEdge(V u, V v); + public boolean adjacent(V u, V v); /** * Finds all neighbours of node <code>u</code>. @@ -64,6 +64,6 @@ public interface IGraph<V> { * @param node * @return list of all neighbours */ - public Set<V> getNeighbourhood(V node); + public Set<V> neighbours(V node); } diff --git a/src/test/java/INF102/lab5/graph/GraphTest.java b/src/test/java/INF102/lab5/graph/GraphTest.java index fb4aaad2164ff9f34ff0a180a53db8b476f352fd..28d2846d97bc2021051454474bfabb625ff124ac 100644 --- a/src/test/java/INF102/lab5/graph/GraphTest.java +++ b/src/test/java/INF102/lab5/graph/GraphTest.java @@ -45,8 +45,8 @@ public abstract class GraphTest { u = random.nextInt(N_NODES); v = random.nextInt(N_NODES); graph.addEdge(u, v); - assertTrue(graph.hasEdge(u, v), "Nodes " + u + " and " + v + " should have an edge between them."); - assertTrue(graph.hasEdge(v, u), + assertTrue(graph.adjacent(u, v), "Nodes " + u + " and " + v + " should have an edge between them."); + assertTrue(graph.adjacent(v, u), "In an undirected graph " + u + " and " + v + " should have an edge both ways."); } } @@ -56,12 +56,12 @@ public abstract class GraphTest { int u = random.nextInt(N_NODES); int v = random.nextInt(N_NODES); graph.addEdge(u, v); - assertTrue(graph.hasEdge(u, v), "Nodes " + u + " and " + v + " should have an edge between them."); - assertTrue(graph.hasEdge(v, u), + assertTrue(graph.adjacent(u, v), "Nodes " + u + " and " + v + " should have an edge between them."); + assertTrue(graph.adjacent(v, u), "In an undirected graph " + u + " and " + v + " should have an edge both ways."); graph.removeEdge(u, v); - assertFalse(graph.hasEdge(u, v), "Nodes " + u + " and " + v + " should not have an edge between them."); - assertFalse(graph.hasEdge(v, u), "Nodes " + v + " and " + u + " should not have an edge between them."); + assertFalse(graph.adjacent(u, v), "Nodes " + u + " and " + v + " should not have an edge between them."); + assertFalse(graph.adjacent(v, u), "Nodes " + v + " and " + u + " should not have an edge between them."); } } @@ -74,7 +74,7 @@ public abstract class GraphTest { randomNodes.add(v); } - for (Integer w : graph.getNeighbourhood(u)) { + for (Integer w : graph.neighbours(u)) { if (!randomNodes.contains(w)) fail("A neighbour of u was not in the list of neigbhours"); } @@ -94,7 +94,7 @@ public abstract class GraphTest { int node = N_NODES - 2; graph.addEdge(node, node + 1); graph.addNode(node); - assertTrue(graph.hasEdge(node, node + 1), + assertTrue(graph.adjacent(node, node + 1), "Trying to add a duplicate node should not remove the egdes from the original node"); } @@ -114,10 +114,10 @@ public abstract class GraphTest { int z = random.nextInt(N_NODES); graph.addEdge(u, v); graph.addEdge(u, z); - assertTrue(graph.getNeighbourhood(u).size() >= 1); + assertTrue(graph.neighbours(u).size() >= 1); assertThrows(NullPointerException.class, () -> { graph.removeNode(u); - assertTrue(graph.getNeighbourhood(u).size() == 0); + assertTrue(graph.neighbours(u).size() == 0); }, "Removing a node should remove the egdes connencting the node");