Skip to content
Snippets Groups Projects
Commit aff99ec6 authored by Tyra.Eide's avatar Tyra.Eide
Browse files

Merge branch 'sem2-changes' into 'master'

Change method names to match sem2 in IGraph.java.

See merge request ii/inf102/24h/dev/lab-5-inf-102!2
parents 66bd2dbe c4dd160e
No related branches found
No related tags found
No related merge requests found
......@@ -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(" --> ");
......
......@@ -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);
}
......@@ -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");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment