import graphlib.edges.UndirectedEdge; import graphlib.graphs.UndirectedGraph; import graphlib.nodes.Node; import graphlib.util.Graphs; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.util.HashMap; import java.util.HashSet; import java.util.stream.IntStream; public class SimpleTest { private static GreedyColouring kleuring; @BeforeClass public static void init() { kleuring = new MyGreedyColouring(); } @Test public void testDriehoek() { UndirectedGraph graaf1 = new UndirectedGraph<>(); graaf1.addNode(1); graaf1.addNode(2); graaf1.addNode(3); graaf1.addEdge(new UndirectedEdge<>(graaf1.getNode(1), graaf1.getNode(2))); graaf1.addEdge(new UndirectedEdge<>(graaf1.getNode(2), graaf1.getNode(3))); graaf1.addEdge(new UndirectedEdge<>(graaf1.getNode(1), graaf1.getNode(3))); HashMap, Integer> colouring = kleuring.greedyColouring(Graphs.unmodifiableUndirectedGraph(graaf1)); Assert.assertEquals(3, coloursUsed(colouring)); } @Test public void testCykel() { UndirectedGraph graaf = new UndirectedGraph<>(); graaf.addNode(1); graaf.addNode(2); graaf.addNode(3); graaf.addNode(4); graaf.addNode(5); graaf.addNode(6); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(1), graaf.getNode(3))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(3), graaf.getNode(4))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(4), graaf.getNode(2))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(2), graaf.getNode(5))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(5), graaf.getNode(6))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(1), graaf.getNode(6))); HashMap, Integer> colouring = kleuring.greedyColouring(Graphs.unmodifiableUndirectedGraph(graaf)); Assert.assertEquals(2, coloursUsed(colouring)); } @Test public void testCykel2() { UndirectedGraph graaf = new UndirectedGraph<>(); IntStream.range(0,10).forEach(graaf::addNode); for (int i = 0; i < 10; i++) { graaf.addEdge(graaf.getNode(3 * i % 10), graaf.getNode((3 * i + 3) % 10)); } HashMap, Integer> colouring = kleuring.greedyColouring(Graphs.unmodifiableUndirectedGraph(graaf)); Assert.assertEquals(2, coloursUsed(colouring)); } @Test public void testGraaf3() { UndirectedGraph graaf = new UndirectedGraph<>(); graaf.addNode(1); graaf.addNode(2); graaf.addNode(3); graaf.addNode(4); graaf.addNode(5); graaf.addNode(6); graaf.addNode(7); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(1), graaf.getNode(2))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(1), graaf.getNode(3))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(1), graaf.getNode(4))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(1), graaf.getNode(5))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(2), graaf.getNode(3))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(2), graaf.getNode(6))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(2), graaf.getNode(4))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(3), graaf.getNode(6))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(4), graaf.getNode(6))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(4), graaf.getNode(7))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(5), graaf.getNode(6))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(5), graaf.getNode(7))); graaf.addEdge(new UndirectedEdge<>(graaf.getNode(6), graaf.getNode(7))); var colouring = kleuring.greedyColouring(Graphs.unmodifiableUndirectedGraph(graaf)); Assert.assertEquals(3, coloursUsed(colouring)); } @Test public void testGreedy(){ UndirectedGraph graph = new UndirectedGraph<>(); IntStream.range(0,5).forEach(graph::addNode); graph.addEdge(new UndirectedEdge<>(graph.getNode(0), graph.getNode(1))); graph.addEdge(new UndirectedEdge<>(graph.getNode(2), graph.getNode(0))); graph.addEdge(new UndirectedEdge<>(graph.getNode(0), graph.getNode(3))); graph.addEdge(new UndirectedEdge<>(graph.getNode(0), graph.getNode(4))); graph.addEdge(new UndirectedEdge<>(graph.getNode(1), graph.getNode(3))); graph.addEdge(new UndirectedEdge<>(graph.getNode(3), graph.getNode(4))); graph.addEdge(new UndirectedEdge<>(graph.getNode(4), graph.getNode(2))); var colouring = kleuring.greedyColouring(Graphs.unmodifiableUndirectedGraph(graph)); Assert.assertEquals(3, coloursUsed(colouring)); } private int coloursUsed(HashMap, Integer> colouring){ return new HashSet<>(colouring.values()).size(); } }