def run_time_results(self, file_name: str, i: int, list_i: list): algo = GraphAlgo() algo.load_from_json(ROOT_DIR + file_name) print(f"************time for graph{i}*****************") self.run_time_shortest_path(algo, list_i) self.run_time_connected_component(algo, list_i) self.run_time_connected_components(algo)
def setUp(self) -> None: nxr = Networkx() file = '../data/G_1000_8000_1.json' nxr.read(file) self.nxg = nxr.get_graph() self.ga = GraphAlgo() self.ga.load_from_json(file)
def test_connected_component_s(self): test_list = Galgo.connected_components() self.assertEqual( 1, test_list.__len__(), "the graph is connected so it will contain 1 list with all nodes") for i in range(48): self.assertTrue(test_list[0].__contains__(i)) test_list1 = Galgo.connected_component(0) self.assertEqual(test_list1, test_list[0], "same lists because is connected") self.assertEqual([], Galgo.connected_component(100), "there isnt such a node at the graph") Gr = GraphAlgo(graph) test_list2 = Gr.connected_components() self.assertEqual(3, test_list2.__len__(), "should be three lists in that list") test_list3 = Gr.connected_component(2) test_list4 = Gr.connected_component(0) self.assertEqual(test_list3, test_list4, "same list because they are strongly connected") self.assertTrue(test_list3.__contains__(2), "should contain this key because they SCC") self.assertTrue(test_list3.__contains__(0), "should contain this key because they SCC") self.assertTrue(test_list3.__contains__(3), "should contain this key because they SCC")
def checknx(): tester = GraphAlgo() fileName = "../data/G_10_80_1.json" tester.load_from_json_nx(fileName) start_time = time.time_ns() print(tester.get_graph()) print("--- %s seconds ---" % (time.time_ns() - start_time))
def test_load(self): g = DiGraph() ga = GraphAlgo(g) self.assertTrue(ga.load_from_json('../data/A5')) g = ga.get_graph() self.assertEqual(g.v_size(), 48) self.assertEqual(g.e_size(), 166)
def check4(): tester = GraphAlgo() fileName = "../data/G_10_80_1.json" tester.load_from_json(fileName) start_time = time.time_ns() print(tester.shortest_path(0, 10)) print("--- %s seconds ---" % (time.time_ns() - start_time))
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) print("test add existing node (FALSE) = {}".format(g.add_node(1))) 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) # 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()
def test_save_load(): graph = Test_G.build_graph("graph_algo") algo = GraphAlgo(graph) print("graph: {0}".format(graph)) print("nodes: {0}".format(graph.get_all_v())) path = '../data/test_save_to_json' print( f"Test: save to json -> (true, path: {path}) = {algo.save_to_json(path)}" ) algo_load = GraphAlgo() print(f"Test: load from json -> (true) = {algo_load.load_from_json(path)}") print( f"Test: graph after load -> (|V|=5 , |E|=5) -> = {algo_load.get_graph()}" ) print( f"Test: nodes: (0: 0: |edges out| 0 |edges in| 2, 1: 1: |edges out| 1 |edges in| 1," f" 2: 2: |edges out| 2 |edges in| 1, 3: 3: |edges out| 1 |edges in| 1, 4: 4: |edges out| 1 |edges in| 0)" f"\n -> {algo_load.get_graph().get_all_v()}") print( f"Test: in edges to node {1}: -> (2: 2) = {algo_load.get_graph().all_in_edges_of_node(1)}" ) print( f"Test: out edges from node {1}: -> (0: 1) = {algo_load.get_graph().all_out_edges_of_node(1)}" )
def test_shortest_path(self): (dis, test_list) = Galgo.shortest_path(0, 5) self.assertEqual(1, 1) self.assertEqual(0, test_list[0], "key of this node should be 0") self.assertEqual(2, test_list[1], "key of this node should be 2") self.assertEqual(3, test_list[2], "key of this node should be 3") self.assertEqual(13, test_list[3], "key of this node should be 13") self.assertEqual(5, test_list[4], "key of this node should be 5") self.assertEqual(5, test_list.__len__(), "there should be 5 nodes in that path") self.assertEqual(4.685698594072881, dis, "shortest path distance should be 4.685698594072881") Galgo.G.remove_node(13) (dis1, test_list1) = Galgo.shortest_path(0, 5) self.assertNotEqual( dis, dis1, "after removing node 13 should be different distance") Gr = GraphAlgo(graph) (dis2, testi) = Gr.shortest_path(1, 0) self.assertEqual(math.inf, dis2, "there isnt a path between 1 to 0 so should be inf") self.assertEqual((math.inf, []), Gr.shortest_path(13, 14), "no such nodes at the graph") (dis3, test_list2) = Gr.shortest_path(2, 5) self.assertEqual(4.5, dis3, "shortest dis should be 4.5") self.assertEqual(2, test_list2[0], "key of this node should be 2") self.assertEqual(4, test_list2[1], "key of this node should be 4") self.assertEqual(5, test_list2[2], "key of this node should be 5")
def test_connected_components(self): self.assertTrue(len(self.algo.connected_components()) == 5) self.assertEqual(self.algo.connected_component(2), [self.algo.graph.get_node(2), self.algo.graph.get_node(1), self.algo.graph.get_node(4), self.algo.graph.get_node(3)]) self.assertTrue(self.algo.connected_component(10) == []) al = GraphAlgo() self.assertTrue(al.connected_components() == [])
def test_get_graph(self): g = DiGraph() g.add_node(1) g.add_node(2) g.add_node(3) g.add_edge(1, 2, 3) ga = GraphAlgo(g) self.assertEqual(g, ga.get_graph())
def test_save_load_json(self): graph = self.create_graph(5, 5) graph_algo1 = GraphAlgo(graph) graph_algo1.save_to_json("test.txt") graph_algo2 = GraphAlgo() graph_algo2.load_from_json("test.txt") self.assertEqual(graph_algo1.shortest_path(1, 3), graph_algo2.shortest_path(1, 3))
def test_get_graph(self): graph = Graph() for number in range(10): graph.add_node(number) for number in range(10): graph.add_edge(0, number, 1) algo = Algo(graph) self.assertTrue(id(graph), id(algo.get_graph()))
def check1(): g_algo = GraphAlgo() # init an empty graph - for the GraphAlgo file = os.path.dirname(os.path.realpath(__file__)) + "/data/A0" 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 + '_saved') g_algo.plot_graph()
def test_scc(self): g = DiGraph() for i in range(0, 10000): g.add_node(i) ga = GraphAlgo(g) for i in range(0, 9999): g.add_edge(i, i + 1, 1) g.add_edge(9999, 0, 1) self.assertEqual(1, len(ga.connected_components()))
def test_shortest_path(self): algo = GraphAlgo() loaded = algo.load_from_json("../data/A0") dist, path = algo.shortest_path(0, 7) self.assertEqual(dist, 5.653293226161572) self.assertEqual([0, 10, 9, 8, 7], path) dist, path = algo.shortest_path(0, 8888887) self.assertEqual(dist, float('inf')) self.assertEqual(path, [])
def test_get_graph(self): graph = DiGraph() graph.add_node(1, (1, 1, 2)) graph.add_node(3, (3, 1, 2)) graph.add_node(4, (4, 2, 2)) graph.add_edge(3, 1, 2.3) graph.add_edge(1, 3, 2.3) graph.add_edge(3, 4, 2.3) ga = GraphAlgo(graph) self.assertEqual(graph, ga.get_graph())
def setUp(self) -> None: self.graph = DiGraph() self.graph_algo = GraphAlgo(self.graph) self.graph.add_node(1, (1, 1, 1)) self.graph.add_node(2, (2, 2, 2)) self.graph.add_node(3, (3, 3, 3)) self.graph.add_node(4, (4, 4, 4)) self.graph.add_node(5, (5, 5, 5)) for i in range(4): self.graph.add_edge(i + 1, i + 2, i + 1)
def test_shortest_path(self): graph_algo = GraphAlgo(self.graph) self.assertEqual((float('inf'), []), graph_algo.shortest_path(1, 2)) """empty graph""" for i in range(4): self.graph.add_node(i) self.graph.add_edge(0, 1, 1) self.graph.add_edge(1, 0, 1.1) self.graph.add_edge(1, 2, 1.3) self.graph.add_edge(2, 3, 0.5) self.assertEqual((0, []), graph_algo.shortest_path(1, 1)) shortest_path: List[list] = graph_algo.shortest_path(1, 3) self.assertEqual((1.8, [1, 2, 3]), shortest_path) self.graph.add_edge(1, 3, 1.8) shortest_path: List[list] = graph_algo.shortest_path(1, 3) self.assertEqual((1.8, [1, 3]), shortest_path) self.graph = DiGraph() for i in range(4): self.graph.add_node(i) self.graph.add_edge(0, 1, 1) self.graph.add_edge(1, 0, 1.1) self.graph.add_edge(1, 2, 1.3) self.graph.add_edge(1, 3, 1.8) self.graph.add_edge(2, 3, 1.1) self.graph.add_node(4) graph_algo = GraphAlgo(self.graph) shortest_path: List[list] = graph_algo.shortest_path(1, 4) self.assertEqual((float('inf'), []), shortest_path)
def test_connected_components(self): g = DiGraph() for i in range(4): g.add_node(i) g.add_edge(1, 2, 2) g.add_edge(2, 1, 1) ga = GraphAlgo(g) check = [[0], [1, 2], [3]] comps = ga.connected_components() for c in range(len(comps)): com = comps[c] for n in range(len(com)): self.assertEqual(com[n], check[c][n]) ga.load_from_json('../data/A5') comps = ga.connected_components() com = comps[0] for n in range(len(com)): self.assertEqual(com[n], n) nxr = Networkx() nxr.read('../data/G_1000_8000_1.json') nxg = nxr.get_graph() nx_comps = nx.strongly_connected_components(nxg) ga.load_from_json('../data/G_1000_8000_1.json') comps = ga.connected_components() comps.sort(reverse=True) i = 0 for nx_comp in nx_comps: comp = comps[i] i += 1 j = 0 for nx_node in nx_comp: node = comp[j] j += 1 self.assertEqual(node, nx_node)
def test_connected_components(self): g = DiGraph() g.add_node(0) g.add_node(1) g.add_node(2) ga = GraphAlgo(g) g.add_edge(0, 1, 1) g.add_edge(0, 2, 9) g.add_edge(1, 2, 3) g.add_edge(2, 1, 3) self.assertEqual(2, len(ga.connected_components()))
def test_10nodes(self): ga = GraphAlgo() ga.load_from_json("../data/G_10_80_0.json") g1 = ga.get_graph() g2 = json_to_nx_graph("../data/G_10_80_0.json") print("comparing shortest path:\n") compare_shortest_path(g1, g2, 10) print("\ncomparing connected components:\n") compare_connected_components(g1, g2)
def test_shortest_path(self): graph = DiGraph() graph.add_node(1, (1, 1, 2)) graph.add_node(2, (3, 1, 2)) graph.add_node(3, (4, 2, 2)) graph.add_edge(1, 2, 1) graph.add_edge(2, 3, 1) graph.add_edge(1, 3, 3) ga = GraphAlgo(graph) res_list = [1, 2, 3] self.assertEqual((2, res_list), ga.shortest_path(1, 3))
def test_save_to_json(self): algo = GraphAlgo() loaded = algo.load_from_json("../data/A0") saved = algo.save_to_json("../data/A0-dumped.json") self.assertTrue(saved) algo2 = GraphAlgo() algo2.load_from_json("../data/A0-dumped.json") self.assertEqual(algo.get_graph().v_size(), algo2.get_graph().v_size()) self.assertEqual(algo.get_graph().e_size(), algo2.get_graph().e_size())
def test_plot_graph(self): # graph = createGraph() # ga = GraphAlgo(graph) # ga.plot_graph() # graph.add_node(7) # graph.add_node(8) # graph.add_node(9, (30, 35)) # ga = GraphAlgo(graph) # ga.plot_graph() g = self.randomGraph(50) algo = GraphAlgo(g) algo.plot_graph()
def test_connected_component(self): graph = createGraph() ga = GraphAlgo(graph) list = [graph.getNode(6)] self.assertEqual(list, ga.connected_component(6)) list.clear() list.append(graph.getNode(0)) list.append(graph.getNode(1)) list.append(graph.getNode(2)) list.append(graph.getNode(3)) list.append(graph.getNode(4)) list.append(graph.getNode(5)) self.assertEqual(list, ga.connected_component(4))
def setUp(self) -> None: graph = DiGraph() self.graphTest = GraphAlgo(graph) for n in range(6): self.graphTest.graphAlgo.add_node(n) self.graphTest.graphAlgo.add_edge(0, 1, 1) self.graphTest.graphAlgo.add_edge(1, 0, 1.1) self.graphTest.graphAlgo.add_edge(1, 2, 1.3) self.graphTest.graphAlgo.add_edge(2, 3, 1.1) self.graphTest.graphAlgo.add_edge(1, 3, 1.9) self.graphTest.graphAlgo.add_edge(3, 4, 8) self.graphTest.graphAlgo.add_edge(4, 5, 1.9) self.graphTest.graphAlgo.add_edge(5, 2, 1.9)
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 + '_saved') g_algo.plot_graph()
def test_connected_components(self): graph = DiGraph() graph.add_node(1, (1, 1, 2)) graph.add_node(2, (2, 2, 2)) graph.add_node(3, (3, 1, 2)) graph.add_node(4, (4, 2, 2)) graph.add_edge(1, 2, 4) graph.add_edge(3, 1, 2.3) graph.add_edge(1, 3, 2.3) graph.add_edge(3, 4, 2.3) ga = GraphAlgo(graph) res = [[2], [4], [1, 3]] self.assertEqual(res.sort(), ga.connected_components().sort())
def run_time_shortest_path(self, algo: GraphAlgo, list_i: list) -> None: print("shortest_path:") start = time.time() short1 = algo.shortest_path(list_i[0], list_i[1]) length_time1 = time.time() - start print(f"time_short1 ={length_time1}") short2 = algo.shortest_path(list_i[2], list_i[3]) length_time2 = time.time() - length_time1 - start print(f"time_short2 ={length_time2}") short3 = algo.shortest_path(list_i[4], list_i[5]) length_time3 = time.time() - length_time2 - start - length_time1 print(f"time_short3 ={length_time3}") print(f"time_all_short={time.time() - start}")