def test_get_graph(self): """ Tests if the method "get_graph" is in order :return: True if so """ print("first test") algo = GraphAlgo() self.assertTrue(algo.get_graph()) print("Completed")
def test_connected_component(self): """ This test checks if there is a connected to a specific node :return: True if it does """ print("seventh test") algo = GraphAlgo(self.single_node_graph()) ans = algo.connected_component(100) self.assertEqual([100], ans) print("Completed")
def testc_shortest_path(self): """ This method tests the shortest path on one node graph :return: True if the paths are the same """ print("fifth test") algo = GraphAlgo(self.single_node_graph()) ans = algo.shortest_path(1,1) self.assertEqual((float('inf'), []), ans) print("Completed")
def testa_transpose(self): """ This method checks if the method 'transpose', that we chose to implement is working the way we wished for :return: True if it does """ print("sixth test") algo = GraphAlgo(self.single_node_graph()) trans = algo.transpose() self.assertEqual(trans, algo.get_graph()) print("Completed")
def testb_shortest_path(self): """ This method tests the shortest path between 2 nodes in a liniar graph :return: True if the paths are the same """ print("fourth test") algo = GraphAlgo(self.liniar_graph()) algo.save_to_json("../tests/third_algo_test.json") s_path_algo = algo.shortest_path(0, 9) g_algo = GraphAlgo() file = '../tests/third_algo_test.json' g_algo.load_from_json(file) s_path_g_algo = g_algo.shortest_path(0, 9) self.assertEqual(s_path_algo, s_path_g_algo) print("Completed")
def test_save(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/A5_edited") ga.save_to_json("../data/Atest") newG: G = G.DiGraph() newGA: GA = GA.GraphAlgo(newG) newGA.load_from_json("../data/Atest") self.assertEqual(g.v_size(), newG.v_size()) self.assertEqual(g.e_size(), newG.e_size()) self.assertEqual( g.get_node(0).getLocation(), newG.get_node(0).getLocation() ) # checks if the same node is in this 2 graphs
def test_load(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/A5_edited") self.assertEqual(48, g.v_size()) self.assertEqual(1.4195069847291193, g.get_node(0).getEdgesFrom().get(2))
def test_shortestPath2(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/A5") tup: tuple = ga.shortest_path(0, 40) self.assertEqual(8.502968926650318, tup[0]) self.assertEqual([0, 2, 3, 13, 14, 15, 39, 40], tup[1])
def test_shortestPathTime(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/A3") start = float(time.time()) ans: tuple = ga.shortest_path(0, 41) end = float(time.time()) print("shortest path on A3 graph Time in seconds: " + str(end - start)) print("From source:0 to dest: 41", ans) print("")
def test_save(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/A5") start = float(time.time()) ga.save_to_json("../data/Atest") end = float(time.time()) print("Testing sace function on A5 graph") print("Time it takes in seconds: " + str(end - start)) print("")
def testa_shortest_path(self): """ This method tests the shortest path between 2 nodes :return: True if the graphs are the same """ print("third test") graph = DiGraph() graph.add_node(0) graph.add_node(1) graph.add_edge(0, 1, 0.0) algo = GraphAlgo(graph) algo.save_to_json("../tests/third_algo_test.json") s_path_algo = algo.shortest_path(0, 1) g_algo = GraphAlgo() file = '../tests/third_algo_test.json' g_algo.load_from_json(file) s_path_g_algo = g_algo.shortest_path(0, 1) self.assertEqual(s_path_algo, s_path_g_algo) print("Completed")
def test_shortestPath1(self): g: G = G.DiGraph() for i in range(10): g.add_node(i) g.add_edge(0, 1, 18) g.add_edge(0, 4, 1.2) g.add_edge(3, 1, 5.4) g.add_edge(1, 2, 6.7) g.add_edge(4, 3, 2.3) ga: GA = GA.GraphAlgo(g) self.assertAlmostEqual(15.6, ga.shortest_path(0, 2)[0], delta=0.001)
def test_connected_components(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/G_30000_240000_0.json") print("Test connected_component on graph with |V|=30, |E|=240k ") start = float(time.time()) ans: List[list] = ga.connected_components() end = float(time.time()) timeItTakes: float = end - start print("It takes: ", str(timeItTakes), " seconds") print(len(ans)) print("")
def test_connected(self): # small graph g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/A5_edited") start = float(time.time()) ans: List[list] = ga.connected_components() end = float(time.time()) print("Testing connected_components on A5_edited") print("first component: ", ans[0]) print("second component: ", ans[1]) print("Time in it takes seconds: " + str(end - start)) print("")
def test_loadTime(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) start = float(time.time()) ga.load_from_json("../data/A3") end = float(time.time()) self.assertEqual(49, g.v_size()) self.assertEqual(136, g.e_size()) print("loading A3 graph, |V| = 49, |E| = 136") print("time it takes in seconds: ", str(end - start)) print("")
def test_create_1(self): # 10 nodes and 80 edges g: G = G() ga: GA = GA.GraphAlgo(g) start = float(time.time()) ga.load_from_json("../data/G_10_80_0.json") end = float(time.time()) # {"src":5,"w":10.383183147587454,"dest":3} self.assertEqual(10.383183147587454, g.get_node(5).getEdgesFrom().get(3)) print("Creating graph with 10 nodes and 80 edges: ", str(g.v_size()), " NODES, and: ", str(g.e_size()), " EDGES") print("It takes: ", f'{end-start:.10f}', " seconds") print("")
def test_allConnected2(self): g: G = G.DiGraph() for i in range(5): g.add_node(i) g.add_edge(0, 1, 7) g.add_edge(3, 4, 6.0) g.add_edge(4, 3, 8.0) g.add_edge(2, 4, 0.5) g.add_edge(4, 2, 7.8) ga: GA = GA.GraphAlgo(g) self.assertEqual(3, len(ga.connected_components()))
def test_create_3(self): # 1000 nodes and 8000 edges g: G = G() ga: GA = GA.GraphAlgo(g) start = float(time.time()) ga.load_from_json("../data/G_1000_8000_0.json") end = float(time.time()) # {"src":2,"w":70.95459152756798,"dest":133}, self.assertEqual(70.95459152756798, g.get_node(2).getEdgesFrom().get(133)) print("Creating graph with 1000 nodes and 8000 edges: ", str(g.v_size()), " NODES, and: ", str(g.e_size()), " EDGES") print("It takes: ", f'{end - start:.10f}', " seconds") print("")
def test_create_4(self): # 10000 nodes and 80000 edges g: G = G() ga: GA = GA.GraphAlgo(g) start = float(time.time()) ga.load_from_json("../data/G_10000_80000_0.json") end = float(time.time()) # {"src":9,"w":79.59769700285791,"dest":3448} self.assertEqual(79.59769700285791, g.get_node(9).getEdgesFrom().get(3448)) print("Creating graph with 10000 nodes and 80000 edges: ", str(g.v_size()), " NODES, and: ", str(g.e_size()), " EDGES") print("It takes: ", f'{end - start:.10f}', " seconds") print("")
def test_create_2(self): # 100 nodes and 800 edges g: G = G() ga: GA = GA.GraphAlgo(g) start = float(time.time()) ga.load_from_json("../data/G_100_800_0.json") end = float(time.time()) # {"src":64,"w":26.4615978682933,"dest":8} self.assertEqual(26.4615978682933, g.get_node(64).getEdgesFrom().get(8)) print("Creating graph with 100 nodes and 800 edges: ", str(g.v_size()), " NODES, and: ", str(g.e_size()), " EDGES") print("It takes: ", f'{end - start:.10f}', " seconds") print("")
def test_shortestPath(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/G_30000_240000_0.json") start = float(time.time()) path: tuple = ga.shortest_path(0, 53) end = float(time.time()) timeItTakes: float = end - start print("Shortest path on graph with |V| = 30k, |E|= 240k") print("Shortest path from 0 to 53 by weight of edges is: ", path[0]) print("The path is: ", path[1]) print("Testing connected_components on: ", str(g.v_size()), " NODES, and: ", str(g.e_size()), " EDGES") print("It takes: ", str(timeItTakes), " seconds") print("")
def test_connectedBig(self): # 30K and 240K g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/G_30000_240000_0.json") # {"src": 0, "w": 69.21467975989786, "dest": 28846} self.assertEqual(69.21467975989786, g.get_node(0).getEdgesFrom().get(28846)) start = float(time.time()) ans: List[list] = ga.connected_components() end = float(time.time()) timeItTakes: float = end - start print("Testing connected_components on: ", str(g.v_size()), " NODES, and: ", str(g.e_size()), " EDGES") print("It takes: ", str(timeItTakes), " seconds") print("")
def test_ccg_graph(self): """ This test checks if the graph is strongly connected :return: True if it does """ print("eighth test") graph = DiGraph() graph.add_node(100) algo = GraphAlgo() algo.graph = graph ans = algo.connected_components() self.assertEqual([[100]], ans) g_algo = GraphAlgo(self.connected_graph()) res = g_algo.connected_components() exp = [] for i in g_algo.get_graph().graph_nodes: exp.append(i) self.assertEqual([exp], res) print("Completed")
def test_plot2(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/A3") ga.plot_graph()
def test_plot3(self): g: G = G.DiGraph() ga: GA = GA.GraphAlgo(g) ga.load_from_json("../data/G_100_800_0.json") ga.plot_graph()
def test_load_save_json(self): """ Tests load_from_json works correctly :return: True if so """ print("second test") algo = GraphAlgo() file = "../data/T0.json" # print the graph to the screen algo.load_from_json(file) algo.plot_graph() algo.save_to_json("../data/T0_saved.json") g_algo = GraphAlgo() file = "../data/T0_saved.json" g_algo.load_from_json(file) # print the graph to the screen g_algo.plot_graph() self.assertFalse(algo.__eq__(g_algo)) print("Completed")