def test_connected_components(self): graph = self.create_graph() g_algo = GraphAlgo(None) x = [] self.assertEqual(x, g_algo.connected_components()) g_algo = GraphAlgo(graph) x = [[0, 1, 2, 3, 4, 5], [8, 6, 7], [9]] self.assertEqual(x, g_algo.connected_components()) graph.remove_edge(2, 3) graph.add_edge(9, 8, 10) x = [[0, 1, 2], [3, 4, 5], [8, 9, 6, 7]] self.assertEqual(x, g_algo.connected_components())
def test_save_and_load_json(self): graph = self.create_graph() g1_algo = GraphAlgo(graph) g1_algo.save_to_json("Test.json") g2_algo = GraphAlgo() g2_algo.load_from_json("Test.json") self.assertTrue(g1_algo.graph.__eq__(g2_algo.graph)) self.assertEqual(g1_algo.graph.v_size(), g2_algo.graph.v_size()) self.assertEqual(g1_algo.graph.e_size(), g2_algo.graph.e_size()) g2_algo.graph.remove_node(3) self.assertNotEqual(g1_algo.graph.v_size(), g2_algo.graph.v_size()) self.assertNotEqual(g1_algo.graph.e_size(), g2_algo.graph.e_size())
def test_connected_component(self): graph = self.create_graph() g_algo = GraphAlgo(None) x = [] self.assertEqual(x, g_algo.connected_components()) g_algo = GraphAlgo(graph) x = [] self.assertEqual(x, g_algo.connected_component(20)) x = [0, 1, 2, 3, 4, 5] self.assertEqual(x, g_algo.connected_component(0)) graph.remove_edge(3, 2) x = [0, 1, 2] self.assertEqual(x, g_algo.connected_component(0)) graph.add_edge(3, 2, 1) graph.add_edge(6, 5, 7) graph.add_edge(9, 8, 2) x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] self.assertEqual(x, g_algo.connected_component(5))
def build_G_30000_240000_0(): algo = GraphAlgo() st = time.time() flag = algo.load_from_json("../data/G_30000_240000_0.json") if flag: print("successfully construction from \"G_30000_240000_0.json\" run time: ", round(time.time() - st, 3), " sec") st = time.time() algo.shortest_path(0, 4) print("shortest_path 0 -->> 4 run time: ", round(time.time() - st, 3), " sec") """
def check1(): """ This function tests the naming (main methods of the GraphAlgo class, as defined in GraphAlgoInterface. :return: """ g_algo = GraphAlgo() # init an empty graph - for the GraphAlgo file = '../data/T0.json' g_algo.load_from_json(file) # init a GraphAlgo from a json file print(g_algo.connected_components()) print(g_algo.shortest_path(0, 3)) print(g_algo.shortest_path(3, 1)) g_algo.save_to_json(file + "_aved") g_algo.plot_graph()
def build_G_20000_160000_0(): algo = GraphAlgo() st = time.time() flag = algo.load_from_json("../data/G_20000_160000_0.json") if flag: print("successfully construction from \"G_20000_160000_0.json\" run time: ", round(time.time() - st, 3), " sec") st = time.time() algo.shortest_path(5000, 15250) print("shortest_path 5000 -->> 15250 run time: ", round(time.time() - st, 3), " sec") st = time.time() algo.connected_component(0) print("connected_component - node 0 run time: ", round(time.time() - st, 3), " sec") st = time.time() algo.connected_components() print("connected_components run time: ", round(time.time() - st, 3), " sec")
def test_shortest_path(self): graph = self.create_graph() g_algo = GraphAlgo(graph) x = (37.4, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # self.assertEquals(x, g_algo.shortest_path(0, 9), 0.1) graph.add_edge(0, 3, 2) self.assertNotEqual(x, g_algo.shortest_path(0, 9)) graph.add_edge(0, 2, 3.5) x = (3.1, [0, 1, 2]) self.assertEqual(x, g_algo.shortest_path(0, 2)) x = (float('infinity'), []) self.assertEqual(x, g_algo.shortest_path(20, 0)) self.assertEqual(x, g_algo.shortest_path(6, 5)) graph.remove_node(3) self.assertEqual(x, g_algo.shortest_path(0, 9)) graph.remove_edge(5, 6) self.assertEqual(x, g_algo.shortest_path(4, 9)) x = (0, [0]) self.assertEqual(x, g_algo.shortest_path(0, 0))
def check2(): """ This function tests the naming, basic testing over A5 json file. :return: """ g_algo = GraphAlgo() file = '../data/A5' g_algo.load_from_json(file) g_algo.get_graph().remove_edge(13, 14) g_algo.save_to_json(file + "_edited") dist, path = g_algo.shortest_path(1, 7) print(dist, path) dist, path = g_algo.shortest_path(47, 19) print(dist, path) dist, path = g_algo.shortest_path(20, 2) print(dist, path) dist, path = g_algo.shortest_path(2, 20) print(dist, path) print(g_algo.connected_component(0)) print(g_algo.connected_components()) g_algo.plot_graph()
def check0(): """ This function tests the naming (main methods of the DiGraph class, as defined in GraphInterface. :return: """ g = DiGraph() # creates an empty directed graph for n in range(4): g.add_node(n) g.add_edge(0, 1, 1) g.add_edge(1, 0, 1.1) g.add_edge(1, 2, 1.3) g.add_edge(2, 3, 1.1) g.add_edge(1, 3, 1.9) g.remove_edge(1, 3) g.add_edge(1, 3, 10) print(g.v_size()) # prints the __repr__ (func output) print(g.e_size()) # prints the __repr__ (func output) print(g.get_all_v()) # prints a dict with all the graph's vertices. print(g.all_in_edges_of_node(1)) print(g.all_out_edges_of_node(1)) g_algo = GraphAlgo(g) print(g_algo.shortest_path(0, 3)) g_algo.plot_graph()