Example #1
0
 @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());
 }
Example #2
0
 @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);
 }
Example #3
0
 @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);
 }