def test_edges(self): g = MatrixGraph(5) edges_in = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)] for (a, b) in edges_in: g.add(a, b) counter = 0 for a, b in g.E(): self.assertTrue((a, b) in edges_in or (b, a) in edges_in) counter += 1 self.assertEqual(len(edges_in), counter)
def test_E(self): g = MatrixGraph(5) edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)] for a,b in edges: g.add(a,b) for a,b in generate_lower_triangle(5, diagonal=True): if (a, b) in g: self.assertTrue(g.is_edge(a, b)) self.assertTrue(g.is_edge(b, a)) else: self.assertFalse(g.is_edge(a, b)) self.assertFalse(g.is_edge(b, a))
def test_complement(self): g = MatrixGraph(4) g.add(0, 1) g.add(1, 2) g.add(2, 3) g.add(3, 0) c = g.complement() self.assertIn((0, 2), c) self.assertIn((1, 3), c) self.assertNotIn((0, 1), c) self.assertNotIn((1, 2), c) self.assertNotIn((2, 3), c) self.assertNotIn((3, 0), c)
def test_d(self): """ Test the degree function """ g = MatrixGraph(order=4).add(0, 1).add(1, 2).add(2, 3).add(3, 0) self.assertEqual(2, g.d(0)) self.assertEqual(2, g.d(1)) self.assertEqual(2, g.d(2)) self.assertEqual(2, g.d(3)) g.add(0,2) self.assertEqual(3, g.d(0)) self.assertEqual(2, g.d(1)) self.assertEqual(3, g.d(2)) self.assertEqual(2, g.d(3))
def do_you_speak_barrington(g_string): """ Given a representation of a graph of the form: 0 01 101 1111 create the associated graph. :param g_string: string containing the graph information :return: The appropriate MatrixGraph """ if ':' in g_string: g_string = g_string[g_string.index(':') + 1:] g_string = g_string.strip() g_strings = g_string.split() print (g_strings) order = len(g_strings) + 1 G = MatrixGraph(order=order) for i, s in enumerate(g_strings): for j, c in enumerate(s): if c == '1': G.add(i+1, j) return G
def test_add(self): g = MatrixGraph(4) self.assertFalse(g.is_edge(0, 1)) g.add(0, 1) self.assertTrue(g.is_edge(0, 1))
def test_remove(self): g = MatrixGraph(3) g.add(0, 1) g.remove(0, 1) self.assertNotIn((0, 1), g) self.assertNotIn((1, 0), g)