@Test public void undirectedGraph() { UndirectedGraph g = new UndirectedGraph(); g.add(); g.add(); g.add(1, 2); assertEquals(1, g.edgeSize()); assertEquals(2, g.successor(1, 0)); assertEquals(2, g.predecessor(1, 0)); g.add(); g.add(); g.add(1, 3); g.add(1, 4); ArrayList<Integer> output = new ArrayList<>(); for (int node : g.successors(1)) { output.add(node); } List<Integer> expected = Arrays.asList(2, 3, 4); assertTrue(output.equals(expected)); g.remove(1, 4); assertFalse(g.contains(1, 4)); assertFalse(g.contains(4, 1)); UndirectedGraph p = new UndirectedGraph(); p.add(); p.add(); p.add(); p.add(); p.add(2, 1); p.add(3, 1); p.add(4, 1); p.add(2, 3); p.remove(1); assertEquals(1, p.edgeSize()); }
@Test public void testSelfEdges() { UndirectedGraph q = new UndirectedGraph(); q.add(); q.add(); q.add(); q.add(); q.add(); q.add(); q.add(); q.add(); q.add(1, 8); q.add(1, 1); q.add(1, 2); q.add(1, 3); q.add(1, 4); ArrayList<Integer> output2 = new ArrayList<>(); for (int node : q.successors(1)) { output2.add(node); } List<Integer> expected2 = Arrays.asList(8, 1, 2, 3, 4); assertTrue(output2.equals(expected2)); assertEquals(5, q.edgeSize()); q.remove(2); ArrayList<Integer> output3 = new ArrayList<>(); for (int node : q.successors(1)) { output3.add(node); } List<Integer> expected3 = Arrays.asList(8, 1, 3, 4); assertTrue(output3.equals(expected3)); assertEquals(4, q.edgeSize()); }
@Test public void edgesCountTestUndirected() { UndirectedGraph<String, String> g1 = new UndirectedGraph<String, String>(); Graph<String, String>.Vertex v1 = g1.add("bar1"); Graph<String, String>.Vertex v2 = g1.add("bar2"); Graph<String, String>.Vertex v3 = g1.add("bar3"); Graph<String, String>.Vertex v4 = g1.add("bar4"); Graph<String, String>.Vertex v5 = g1.add("bar5"); Graph<String, String>.Edge e1 = g1.add(v1, v3); Graph<String, String>.Edge e2 = g1.add(v2, v1); Graph<String, String>.Edge e3 = g1.add(v3, v4); Graph<String, String>.Edge e4 = g1.add(v1, v4); Graph<String, String>.Edge e5 = g1.add(v5, v1); assertEquals("Incorrect number of edges.", g1.edgeSize(), 5); g1.remove(e4); assertEquals("Incorrect number edges after removal", g1.edgeSize(), 4); }
@Test public void outDegreeTestUndirected() { UndirectedGraph<String, String> g1 = new UndirectedGraph<String, String>(); Graph<String, String>.Vertex v1 = g1.add("shoe"); Graph<String, String>.Vertex v2 = g1.add("sandal"); Graph<String, String>.Vertex v3 = g1.add("flip"); Graph<String, String>.Vertex v4 = g1.add("flop"); Graph<String, String>.Vertex v5 = g1.add("beach"); Graph<String, String>.Edge e1 = g1.add(v1, v3); Graph<String, String>.Edge e2 = g1.add(v2, v1); Graph<String, String>.Edge e3 = g1.add(v3, v4); Graph<String, String>.Edge e4 = g1.add(v1, v4); Graph<String, String>.Edge e5 = g1.add(v5, v1); assertEquals("Wrong out degree.", g1.outDegree(v1), 4); g1.remove(v1); assertEquals("Out edge was removed", g1.contains(v2, v1), false); }
@Test public void inDegreeTestUndirected() { UndirectedGraph<String, String> g1 = new UndirectedGraph<String, String>(); Graph<String, String>.Vertex v1 = g1.add("pepsi"); Graph<String, String>.Vertex v2 = g1.add("coke"); Graph<String, String>.Vertex v3 = g1.add("7up"); Graph<String, String>.Vertex v4 = g1.add("sprite"); Graph<String, String>.Vertex v5 = g1.add("pepper"); Graph<String, String>.Edge e1 = g1.add(v1, v3); Graph<String, String>.Edge e2 = g1.add(v2, v1); Graph<String, String>.Edge e3 = g1.add(v3, v4); Graph<String, String>.Edge e4 = g1.add(v1, v4); Graph<String, String>.Edge e5 = g1.add(v5, v1); assertEquals("Wrong in degree v1.", g1.inDegree(v1), 4); assertEquals("Wrong in degree v3.", g1.inDegree(v3), 2); g1.remove(v1); assertEquals("In edge v2 was removed", g1.contains(v2, v1), false); }