def test_cyclic_detection(self): g = Graph() g.add_vertex(1) g.add_vertex(2) g.add_vertex(3) g.add_vertex(4) assert g.cycle_detect() is False # Let's add a straight line g.add_edge(1, 2) g.add_edge(2, 3) assert g.cycle_detect() is False # move from 3 to 1 g.add_edge(3, 1) assert g.cycle_detect() is True g.remove_edge(3, 1) # Looping 4 to 4 g.add_edge(4, 4) assert g.cycle_detect() is True # Removed 4 to 4 g.remove_edge(4, 4) assert g.cycle_detect() is False
def test_remove(self): g = Graph() g.add_vertex(1) g.add_vertex(2) g.add_vertex(3) g.add_edge(1, 2) g.add_edge(1, 3) # Should be removed g.remove_edge(1, 2) assert 2 not in g.vertices[1] assert 3 in g.vertices[1] assert 1 not in g.vertices[2] assert 3 not in g.vertices[2] assert 1 not in g.vertices[3] assert 2 not in g.vertices[3] # Let's reset that and add more things that link to 3 g.add_edge(1, 2) g.add_edge(2, 3) assert 3 in g.vertices[1] assert 3 in g.vertices[2] # And remove 3 g.remove_vertex(3) # It should stop existing, even in previous adjacincies assert 3 not in g.vertices[1] assert 3 not in g.vertices[2] assert 3 not in g.vertices # Delete something that doesn't exist, with self.assertRaises(ValueError): g.remove_vertex(6) # Let's make 1 not exist g.remove_vertex(1) with self.assertRaises(ValueError): g.add_edge(2, 1) assert 1 not in g.vertices # Adding edges with self.assertRaises(ValueError): g.add_edge(2, 1) with self.assertRaises(ValueError): g.add_edge(1, 2) # Removing edges that can't exist, src and dest with self.assertRaises(ValueError): g.remove_edge(5, 6) with self.assertRaises(ValueError): g.remove_edge(1, 6)