Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • ii/inf102/24h/students/lab-5-inf-102
  • Rein.Landmark/lab-5-inf-102
  • antoine.hureau/lab-5-inf-102
  • Per.Broberg/lab-5-inf-102
  • erlend.hontorp/lab-5-inf-102
  • Nicolay.Maland/lab-5-inf-102
  • Markus.Sellevoll/lab-5-inf-102
  • Alexander.Namdal/lab-5-inf-102
  • Thomas.Barth/lab-5-inf-102
  • Martin.Pedersen1/lab-5-inf-102
  • Heris.Sivanesarajah/lab-5-inf-102-2024
  • Amalie.Sotvedt/lab-5-inf-102
  • Synne.S.Holen/lab-5-inf-102
  • Swarnika.Sellathurai/lab-5-inf-102
  • Njal.Nordal/lab-5-inf-102
  • Raphael.Olande/lab-5-inf-102
  • Ajdin.Puskar/lab-5-inf-102
  • Jonas.Justesen/lab-5-inf-102
  • Max.A.Muller/lab-5-inf-102
  • Bahja.Sheikh/lab-5-inf-102
  • Jakob.Berg/lab-5-inf-102
  • Espen.Lilleengen/lab-5-inf-102
  • Goran.Kvitne/lab-5-inf-102
  • Martin.Andreassen/lab-5-inf-102
  • Even.Tuv/lab-5-inf-102
  • Elena.Balaban/lab-5-inf-102
  • Diana.Carvajal/lab-5-inf-102
  • jonas.sterud/inf-102-lab-05
  • Sofie.K.Hagen/lab-5-inf-102
  • Anne.Skurtveit/lab-5-inf-102
  • Sebastian.Oen/lab-5-inf-102-graphs
  • Endre.Aspoy/lab-5-inf-102
  • Andor.Herrewijnen/lab-5-inf-102
  • Fredrik.Lauritzen/inf-102-lab-5
  • Fredrik.B.Hansen/lab-5-inf-102
  • Martin.Ronning/lab-5-inf-102
  • Andreas.Dixon/lab-5-inf-102
  • sverre.f.langaas/lab-5-inf-102
  • Stian.Ha/lab-5-inf-102
  • O.Vik/lab-5-inf-102-ole
  • Anders.Tokje/lab-5-inf-102
41 results
Show changes
Commits on Source (4)
......@@ -7,22 +7,22 @@ The graphs in this assignment are **undirected** and **unweighted**:
### Task 1
Implement the remaining methods in `AdjacencySet.java`. Methods:
* `addNode(V node)`
* `removeNode(V node)`
* `addVertice(V vertice)`
* `removeVertice(V vertice)`
* `addEdge(V u, v v)`
* `removeEdge(V u, V v)`
* `hasNode(V node)`
* `hasVertice(V vertice)`
✅ Run `AdjacencySetTest` to check your solution. The task is passed if all tests pass.
### Task 2
An essential part of graph theory is to be able to search through a graph. In this task we want to know whether two nodes `u` and `v` are connected.
An essential part of graph theory is to be able to search through a graph. In this task we want to know whether two vertices `u` and `v` are connected.
<img align="center" src="images/Lab5_graph.png" width="400"/>
In the graph above `0` and `2` are connected since there is a direct edge between them. `3` and `2` are also connected since there are nodes inbetween that connect them. <br></br>
While `2` and `5` are not connected since there are noe edges that directly connect them or any inbetween other nodes.
In the graph above `0` and `2` are connected since there is a direct edge between them. `3` and `2` are also connected since there are vertices inbetween that connect them. <br></br>
While `2` and `5` are not connected since there are no edges that directly connect them or any inbetween other vertices.
**TODO: Implement `GraphSearch::connected`.**
......
......@@ -27,12 +27,12 @@ public class AdjacencySet<V> implements IGraph<V> {
}
@Override
public void addNode(V node) {
public void addVertice(V vertice) {
throw new UnsupportedOperationException("Implement me :)");
}
@Override
public void removeNode(V node) {
public void removeVertice(V vertice) {
throw new UnsupportedOperationException("Implement me :)");
}
......@@ -47,7 +47,7 @@ public class AdjacencySet<V> implements IGraph<V> {
}
@Override
public boolean hasNode(V node) {
public boolean hasVertice(V vertice) {
throw new UnsupportedOperationException("Implement me :)");
}
......@@ -57,22 +57,22 @@ public class AdjacencySet<V> implements IGraph<V> {
}
@Override
public Set<V> neighbours(V node) {
return Collections.unmodifiableSet(adjacencyList.get(node));
public Set<V> neighbours(V vertice) {
return Collections.unmodifiableSet(adjacencyList.get(vertice));
}
@Override
public String toString() {
StringBuilder build = new StringBuilder();
for (V node : adjacencyList.keySet()) {
Set<V> nodeList = adjacencyList.get(node);
build.append(node);
build.append(" --> ");
build.append(nodeList);
build.append("\n");
StringBuilder builder = new StringBuilder();
for (V vertice : adjacencyList.keySet()) {
Set<V> verticeList = adjacencyList.get(vertice);
builder.append(vertice);
builder.append(" --> ");
builder.append(verticeList);
builder.append("\n");
}
return build.toString();
return builder.toString();
}
}
......@@ -5,50 +5,50 @@ import java.util.Set;
public interface IGraph<V> {
/**
* Number of nodes in the graph
* @return number of nodes in graph
* Number of vertices in the graph
* @return number of vertices in graph
*/
public int size();
/**
* A way to iterate through all vertices of this graph
* @return set of graph nodes
* @return set of graph vertices
*/
public Iterable<V> vertices();
/**
* Add <code>node</code> to graph
* @param node
* Add <code>vertice</code> to graph
* @param vertice
*/
public void addNode(V node);
public void addVertice(V vertice);
/**
* Remove <code>node</code> from graph
* @param node
* Remove <code>vertice</code> from graph
* @param vertice
*/
public void removeNode(V node);
public void removeVertice(V vertice);
/**
* Add edge between node <code>u</code> and <code>v</code>
* Add edge between vertice <code>u</code> and <code>v</code>
* @param u
* @param v
* @throws IllegalArgumentException if the nodes given are not in the graph
* @throws IllegalArgumentException if the vertices given are not in the graph
*/
public void addEdge(V u, V v);
/**
* Remove edge between node <code>u</code> and <code>v</code>
* Remove edge between vertice <code>u</code> and <code>v</code>
* @param u
* @param v
*/
public void removeEdge(V u, V v);
/**
* Checks if the given <code>node</code> is in the graph
* @param node
* @return true if node is in graph
* Checks if the given <code>vertice</code> is in the graph
* @param vertice
* @return true if vertice is in graph
*/
public boolean hasNode(V node);
public boolean hasVertice(V vertice);
/**
* Checks if two given vertices are adjacent
......@@ -59,11 +59,11 @@ public interface IGraph<V> {
public boolean adjacent(V u, V v);
/**
* Finds all neighbours of node <code>u</code>.
* The neighbours of a node is all nodes which it has an edge to.
* @param node
* Finds all neighbours of vertice <code>u</code>.
* The neighbours of a vertice is all vertices which it has an edge to.
* @param vertice
* @return list of all neighbours
*/
public Set<V> neighbours(V node);
public Set<V> neighbours(V vertice);
}
......@@ -3,7 +3,7 @@ package INF102.lab5.graph;
public class Main {
public static final int N_NODES = 5;
public static final int N_VERTICES = 5;
public static void main(String[] args) {
IGraph<Integer> graph = new AdjacencySet<>();
......@@ -22,18 +22,18 @@ public class Main {
}
/**
* Creates a simple graph with 5 nodes.
* Creates a simple graph with 5 vertices.
*
* 0 -> 1 -> 2 3 -> 4
*
* @param graph
*/
public static void createGraph(IGraph<Integer> graph) {
graph.addNode(0);
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addNode(4);
graph.addVertice(0);
graph.addVertice(1);
graph.addVertice(2);
graph.addVertice(3);
graph.addVertice(4);
graph.addEdge(0, 1);
graph.addEdge(1, 2);
......
......@@ -10,14 +10,14 @@ public class AdjacencySetTest extends GraphTest {
@BeforeEach
public void setup() {
adjacencySet = new AdjacencySet<>();
for (int i = 0; i < N_NODES; i++) {
adjacencySet.addNode(i);
for (int i = 0; i < N_VERTICES; i++) {
adjacencySet.addVertice(i);
}
}
@Test
public void setAddEdgeToNodeNotInGraph() {
addEdgeToNodeNotInGraph(adjacencySet);
public void setAddEdgeToVerticeNotInGraph() {
addEdgeToVerticeNotInGraph(adjacencySet);
}
@Test
......@@ -36,21 +36,21 @@ public class AdjacencySetTest extends GraphTest {
}
@Test
public void setAddNodeTest() {
addNodeTest(adjacencySet);
public void setAddVerticeTest() {
addVerticeTest(adjacencySet);
}
@Test
public void setRemoveNodeTest() {
removeNodeTest(adjacencySet);
public void setRemoveVerticeTest() {
removeVerticeTest(adjacencySet);
}
@Test
public void setDuplicateNodeTest() {
duplicateNodeTest(adjacencySet);
public void setDuplicateVerticeTest() {
duplicateVerticeTest(adjacencySet);
}
@Test
public void setRemoveEgdesWithNodeTest(){
removeEgdesWithNodeTest(adjacencySet);
public void setRemoveEgdesWithVerticeTest(){
removeEgdesWithVerticeTest(adjacencySet);
}
}
......@@ -4,7 +4,7 @@ package INF102.lab5.graph;
import org.junit.jupiter.api.Test;
/**
* An implementation of a graph datastrucutre using adjacency matrix should yield a OutOfMemoryException when the number of nodes
* An implementation of a graph datastrucutre using adjacency matrix should yield a OutOfMemoryException when the number of vertices
* is too high, while using adjacency list/set should not.
*/
public class GraphImplementationTest {
......@@ -15,7 +15,7 @@ public class GraphImplementationTest {
public void listNotOutOfMemoryTest() {
AdjacencySet<Integer> set = new AdjacencySet<Integer>();
for (int i = 0; i < OUT_OF_MEMORY_GRAPH_SIZE; i++) {
set.addNode(i);
set.addVertice(i);
}
}
}
......@@ -14,17 +14,17 @@ public class GraphSearchTest {
private IGraph<Integer> graph;
private IGraphSearch<Integer> graphSearch;
private final int N_NODES = 1000;
private final int N_VERTICES = 1000;
private Random random = new Random();
/**
* Adds edges of all nodes from 0 to N, i.e.:
* Adds edges of all vertices from 0 to N, i.e.:
* (0 -> 1), (1 -> 2), ..., (N-1 -> N)
*
* @param graph
*/
public void createConnectedGraph(IGraph<Integer> graph) {
for (int i = 0; i < N_NODES - 1; i++) {
for (int i = 0; i < N_VERTICES - 1; i++) {
graph.addEdge(i, i + 1);
}
}
......@@ -32,8 +32,8 @@ public class GraphSearchTest {
@BeforeEach
public void setup() {
graph = new AdjacencySet<>();
for (int i = 0; i < N_NODES; i++) {
graph.addNode(i);
for (int i = 0; i < N_VERTICES; i++) {
graph.addVertice(i);
}
graphSearch = new GraphSearch<>(graph);
}
......@@ -42,7 +42,7 @@ public class GraphSearchTest {
public void connectedTest() {
createConnectedGraph(graph);
int u = 0;
int v = N_NODES - 1;
int v = N_VERTICES - 1;
assertTrue(graphSearch.connected(u, v));
}
......@@ -51,19 +51,19 @@ public class GraphSearchTest {
public void notConnectedTest() {
createConnectedGraph(graph);
int u = 0;
int v = N_NODES - 1;
int w = N_NODES - 2;
int v = N_VERTICES - 1;
int w = N_VERTICES - 2;
graph.removeEdge(w, v);
assertFalse(graphSearch.connected(u, v));
}
@Test
public void neighbouringNodesAreConnectedTest() {
public void neighbouringVerticesAreConnectedTest() {
createConnectedGraph(graph);
for (int i = 0; i < 1000; i++) {
int u = random.nextInt(N_NODES);
int v = random.nextInt(N_NODES);
int u = random.nextInt(N_VERTICES);
int v = random.nextInt(N_VERTICES);
graph.addEdge(u, v);
assertTrue(graphSearch.connected(u, v));
......@@ -73,14 +73,14 @@ public class GraphSearchTest {
@Test
public void connectedUndirectedTest() {
for (int i = 0; i < 1000; i++) {
int u = random.nextInt(N_NODES);
int v = random.nextInt(N_NODES);
int u = random.nextInt(N_VERTICES);
int v = random.nextInt(N_VERTICES);
graph.addEdge(u, v);
}
for (int i = 0; i < 10000; i++) {
int u = random.nextInt(N_NODES);
int v = random.nextInt(N_NODES);
int u = random.nextInt(N_VERTICES);
int v = random.nextInt(N_VERTICES);
boolean uConnectedToV = graphSearch.connected(u, v);
boolean vConnectedToU = graphSearch.connected(v, u);
......
......@@ -13,113 +13,113 @@ import java.util.Set;
public abstract class GraphTest {
final int N_NODES = 1000;
final int N_VERTICES = 1000;
Random random = new Random();
/**
* Adds edges of all nodes from 0 to N, i.e.:
* Adds edges of all vertices from 0 to N, i.e.:
* (0 -> 1), (1 -> 2), ..., (N-1 -> N)
*
* @param graph
*/
public void createConnectedGraph(IGraph<Integer> graph) {
for (int i = 0; i < N_NODES - 1; i++) {
for (int i = 0; i < N_VERTICES - 1; i++) {
graph.addEdge(i, i + 1);
}
}
public void addEdgeToNodeNotInGraph(IGraph<Integer> graph) {
int u = N_NODES + 1;
int v = random.nextInt(N_NODES);
public void addEdgeToVerticeNotInGraph(IGraph<Integer> graph) {
int u = N_VERTICES + 1;
int v = random.nextInt(N_VERTICES);
assertThrows(IllegalArgumentException.class, () -> graph.addEdge(u, v),
"You can't add an edge to a node that doesn't exist on the graph.");
"You can't add an edge to a vertice that doesn't exist on the graph.");
assertThrows(IllegalArgumentException.class, () -> graph.addEdge(v, u),
"You can't add an edge to a node that doesn't exist on the graph.");
"You can't add an edge to a vertice that doesn't exist on the graph.");
}
public void addEdgeTest(IGraph<Integer> graph) {
int u = 0;
int v = 0;
for (int i = 0; i < N_NODES; i++) {
u = random.nextInt(N_NODES);
v = random.nextInt(N_NODES);
for (int i = 0; i < N_VERTICES; i++) {
u = random.nextInt(N_VERTICES);
v = random.nextInt(N_VERTICES);
graph.addEdge(u, v);
assertTrue(graph.adjacent(u, v), "Nodes " + u + " and " + v + " should have an edge between them.");
assertTrue(graph.adjacent(u, v), "Vertices " + 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.");
}
}
public void removeEdgeTest(IGraph<Integer> graph) {
for (int i = 0; i < N_NODES; i++) {
int u = random.nextInt(N_NODES);
int v = random.nextInt(N_NODES);
for (int i = 0; i < N_VERTICES; i++) {
int u = random.nextInt(N_VERTICES);
int v = random.nextInt(N_VERTICES);
graph.addEdge(u, v);
assertTrue(graph.adjacent(u, v), "Nodes " + u + " and " + v + " should have an edge between them.");
assertTrue(graph.adjacent(u, v), "Vertices " + 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.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.");
assertFalse(graph.adjacent(u, v), "Vertices " + u + " and " + v + " should not have an edge between them.");
assertFalse(graph.adjacent(v, u), "Vertices " + v + " and " + u + " should not have an edge between them.");
}
}
public void getNeighbourhoodTest(IGraph<Integer> graph) {
int u = random.nextInt(N_NODES);
Set<Integer> randomNodes = new HashSet<>();
int u = random.nextInt(N_VERTICES);
Set<Integer> randomVertices = new HashSet<>();
for (int i = 0; i < 100; i++) {
int v = random.nextInt(N_NODES);
int v = random.nextInt(N_VERTICES);
graph.addEdge(u, v);
randomNodes.add(v);
randomVertices.add(v);
}
for (Integer w : graph.neighbours(u)) {
if (!randomNodes.contains(w))
if (!randomVertices.contains(w))
fail("A neighbour of u was not in the list of neigbhours");
}
}
public void addNodeTest(IGraph<Integer> graph) {
int extraNode = N_NODES + 1;
assertEquals(N_NODES, graph.size(), "Graph should have " + N_NODES + " nodes.");
assertFalse(graph.hasNode(extraNode), "Graph should not have node " + extraNode);
int u = extraNode;
graph.addNode(u);
assertTrue(graph.hasNode(u), "Graph should have node " + u);
assertEquals(extraNode, graph.size(), "Graph should have " + extraNode + " nodes.");
public void addVerticeTest(IGraph<Integer> graph) {
int extraVertice = N_VERTICES + 1;
assertEquals(N_VERTICES, graph.size(), "Graph should have " + N_VERTICES + " vertices.");
assertFalse(graph.hasVertice(extraVertice), "Graph should not have vertice " + extraVertice);
int u = extraVertice;
graph.addVertice(u);
assertTrue(graph.hasVertice(u), "Graph should have vertice " + u);
assertEquals(extraVertice, graph.size(), "Graph should have " + extraVertice + " vertices.");
}
public void duplicateNodeTest(IGraph<Integer> graph) {
int node = N_NODES - 2;
graph.addEdge(node, node + 1);
graph.addNode(node);
assertTrue(graph.adjacent(node, node + 1),
"Trying to add a duplicate node should not remove the egdes from the original node");
public void duplicateVerticeTest(IGraph<Integer> graph) {
int vertice = N_VERTICES - 2;
graph.addEdge(vertice, vertice + 1);
graph.addVertice(vertice);
assertTrue(graph.adjacent(vertice, vertice + 1),
"Trying to add a duplicate vertice should not remove the egdes from the original vertice");
}
public void removeNodeTest(IGraph<Integer> graph) {
addNodeTest(graph);
int u = random.nextInt(N_NODES);
assertNotEquals(N_NODES, graph.size(), "Graph should not have " + N_NODES + " nodes.");
graph.removeNode(u);
assertFalse(graph.hasNode(u), "Graph should not have node " + u);
assertEquals(N_NODES, graph.size(), "Graph should have " + N_NODES + " nodes.");
public void removeVerticeTest(IGraph<Integer> graph) {
addVerticeTest(graph);
int u = random.nextInt(N_VERTICES);
assertNotEquals(N_VERTICES, graph.size(), "Graph should not have " + N_VERTICES + " vertices.");
graph.removeVertice(u);
assertFalse(graph.hasVertice(u), "Graph should not have vertice " + u);
assertEquals(N_VERTICES, graph.size(), "Graph should have " + N_VERTICES + " vertices.");
}
public void removeEgdesWithNodeTest(IGraph<Integer> graph) {
int u = random.nextInt(N_NODES);
int v = random.nextInt(N_NODES);
int z = random.nextInt(N_NODES);
public void removeEgdesWithVerticeTest(IGraph<Integer> graph) {
int u = random.nextInt(N_VERTICES);
int v = random.nextInt(N_VERTICES);
int z = random.nextInt(N_VERTICES);
graph.addEdge(u, v);
graph.addEdge(u, z);
assertTrue(graph.neighbours(u).size() >= 1);
assertThrows(NullPointerException.class, () -> {
graph.removeNode(u);
graph.removeVertice(u);
assertTrue(graph.neighbours(u).size() == 0);
},
"Removing a node should remove the egdes connencting the node");
"Removing a vertice should remove the egdes connencting the vertice");
}
......