Beispiel #1
0
    def should_get_all_clusters(self):
        graph = clckwrkbdgr.math.graph.Graph(directional=True)
        graph.add_node('A')
        graph.add_node('B')
        self.assertCountEqual(get_clusters(graph), [{'A'}, {'B'}])

        graph.add_node('C')
        self.assertCountEqual(get_clusters(graph), [{'A'}, {'B'}, {'C'}])

        graph.connect('A', 'B')
        self.assertCountEqual(get_clusters(graph), [{'A', 'B'}, {'C'}])

        graph.connect('C', 'A')
        self.assertCountEqual(get_clusters(graph), [{'A', 'B', 'C'}])

        graph.connect('B', 'C')
        self.assertCountEqual(get_clusters(graph), [{'A', 'B', 'C'}])

        graph.add_node('D')
        self.assertCountEqual(get_clusters(graph), [{'A', 'B', 'C'}, {'D'}])

        graph.add_node('E')
        graph.connect('D', 'E')
        self.assertCountEqual(get_clusters(graph),
                              [{'A', 'B', 'C'}, {'D', 'E'}])
Beispiel #2
0
 def should_disconnect_nodes_in_directional_graph(self):
     graph = clckwrkbdgr.math.graph.Graph(directional=True)
     graph.add_node('A')
     graph.add_node('B')
     graph.add_node('C')
     graph.add_node('D')
     graph.connect('A', 'B')
     graph.connect('C', 'A')
     graph.connect('C', 'B')
     graph.connect('B', 'C')
     with self.assertRaises(ValueError):
         graph.disconnect('A', 'X')
     with self.assertRaises(ValueError):
         graph.disconnect('X', 'A')
     with self.assertRaises(ValueError):
         graph.disconnect('A', 'D')
     with self.assertRaises(ValueError):
         graph.disconnect('D', 'A')
     with self.assertRaises(ValueError):
         graph.disconnect('B', 'A')
     graph.disconnect('A', 'B')
     graph.disconnect('C', 'B')
     graph.disconnect('B', 'C')
     self.assertFalse(graph.has_connection('A', 'B'))
     self.assertFalse(graph.has_connection('B', 'A'))
     self.assertFalse(graph.has_connection('B', 'C'))
     self.assertFalse(graph.has_connection('C', 'B'))
     self.assertTrue(graph.has_connection('C', 'A'))
Beispiel #3
0
 def should_connect_nodes_in_directional_graph(self):
     graph = clckwrkbdgr.math.graph.Graph(directional=True)
     graph.add_node('A')
     graph.add_node('B')
     graph.add_node('C')
     graph.add_node('D')
     with self.assertRaises(ValueError):
         graph.connect('A', 'X')
     with self.assertRaises(ValueError):
         graph.connect('X', 'A')
     graph.connect('A', 'B')
     graph.connect('B', 'C')
     with self.assertRaises(ValueError):
         graph.connect('B', 'C')
     graph.connect('C', 'B')
     self.assertEqual(graph.all_connections('A'), {'B'})
     self.assertEqual(graph.all_connections('B'), {'C'})
     self.assertEqual(graph.all_connections('B', with_incoming=True),
                      {'C', 'A'})
     self.assertEqual(graph.all_connections('D'), set())
     self.assertTrue(graph.has_connection('A', 'B'))
     self.assertFalse(graph.has_connection('B', 'A'))
     self.assertTrue(graph.has_connection('B', 'C'))
     self.assertTrue(graph.has_connection('C', 'B'))
     self.assertFalse(graph.has_connection('A', 'C'))
     self.assertFalse(graph.has_connection('D', 'A'))
     self.assertFalse(graph.has_connection('A', 'D'))
Beispiel #4
0
 def should_get_all_links(self):
     graph = clckwrkbdgr.math.graph.Graph()
     graph.add_node('A')
     graph.add_node('B')
     graph.add_node('C')
     graph.add_node('D')
     graph.connect('A', 'B')
     graph.connect('A', 'C')
     graph.connect('B', 'C')
     self.assertEqual(
         set(tuple(sorted(link)) for link in graph.all_links()), {
             ('A', 'B'),
             ('A', 'C'),
             ('B', 'C'),
         })
Beispiel #5
0
 def should_get_all_links_in_directional_graph(self):
     graph = clckwrkbdgr.math.graph.Graph(directional=True)
     graph.add_node('A')
     graph.add_node('B')
     graph.add_node('C')
     graph.add_node('D')
     graph.connect('A', 'B')
     graph.connect('C', 'A')
     graph.connect('C', 'B')
     graph.connect('B', 'C')
     self.assertEqual(set(graph.all_links()), {
         ('A', 'B'),
         ('C', 'A'),
         ('C', 'B'),
         ('B', 'C'),
     })
Beispiel #6
0
 def should_add_node(self):
     graph = clckwrkbdgr.math.graph.Graph()
     graph.add_node('A')
     self.assertTrue(graph.has_node('A'))
     self.assertFalse(graph.has_node('B'))
     graph.add_node('B')
     self.assertTrue(graph.has_node('B'))
     with self.assertRaises(ValueError):
         graph.add_node('B')
Beispiel #7
0
    def should_detect_fully_connected_graph(self):
        graph = clckwrkbdgr.math.graph.Graph(directional=True)
        graph.add_node('A')
        graph.add_node('B')
        graph.add_node('C')
        self.assertFalse(is_connected(graph))

        graph.connect('A', 'B')
        self.assertFalse(is_connected(graph))

        graph.connect('C', 'A')
        self.assertTrue(is_connected(graph))

        graph.connect('B', 'C')
        self.assertTrue(is_connected(graph))

        graph.add_node('D')
        self.assertFalse(is_connected(graph))
Beispiel #8
0
 def should_remove_node(self):
     graph = clckwrkbdgr.math.graph.Graph()
     graph.add_node('A')
     graph.add_node('B')
     graph.add_node('C')
     graph.connect('A', 'B')
     graph.connect('A', 'C')
     graph.connect('B', 'C')
     with self.assertRaises(ValueError):
         graph.remove_node('X')
     graph.remove_node('A')
     self.assertFalse(graph.has_node('A'))
     self.assertFalse(graph.has_connection('A', 'B'))
     self.assertFalse(graph.has_connection('A', 'C'))
     self.assertTrue(graph.has_connection('B', 'C'))
Beispiel #9
0
 def should_get_all_nodes(self):
     graph = clckwrkbdgr.math.graph.Graph()
     graph.add_node('A')
     graph.add_node('B')
     graph.add_node('C')
     self.assertEqual(graph.all_nodes(), {'A', 'B', 'C'})