Example #1
0
 def test_connected_components(self):
     g = DiGraph()
     for i in range(1000):
         g6.add_node(i)
         g6.add_edge(i - 1, i, 1)
         if i % 100 != 0:
             g6.add_edge(i, i - 1, 1)
     ga6 = GraphAlgo(g6)
     self.assertEqual(len(ga6.connected_components()), 10)
     ga6.get_graph().add_edge(100, 99, 1)
     self.assertEqual(len(ga6.connected_components()), 9)
Example #2
0
def my_graph(filename):
    algo = GraphAlgo()
    algo.load_from_json(filename)

    start = timeit.default_timer()
    algo.shortest_path(1, 5)
    stop = timeit.default_timer()
    print("my shoetest path ", (stop - start))

    start = timeit.default_timer()
    algo.connected_components()
    stop = timeit.default_timer()
    print("my connected components ", (stop - start))

    start = timeit.default_timer()
    algo.connected_component(0)
    stop = timeit.default_timer()
    print("my connected component ", (stop - start))
Example #3
0
    def test_component_s(self):
        self.assertTrue(self.graph.add_node(0))
        self.assertTrue(self.graph.add_node(1))
        self.assertTrue(self.graph.add_node(2))
        self.assertTrue(self.graph.add_node(3))

        self.assertTrue(self.graph.add_edge(0, 1, 1))
        self.assertTrue(self.graph.add_edge(1, 2, 1))
        self.assertTrue(self.graph.add_edge(2, 0, 0.5))
        self.assertTrue(self.graph.add_edge(2, 3, 0.5))

        algo = GraphAlgo(self.graph)
        p = algo.connected_components()
        self.assertEqual(p, [[0, 1, 2], [3]])
        p = algo.connected_component(3)
        p1 = algo.connected_component(1)
        self.assertEqual(p, [3])
        self.assertEqual(p1, [1, 2, 0])