def test_connected_components(self): graph = DiGraph() for i in range(1, 9): graph.add_node(i) graph.add_edge(1, 2, 1) graph.add_edge(2, 3, 1) graph.add_edge(3, 4, 1) graph.add_edge(4, 1, 1) graph.add_edge(3, 5, 1) graph.add_edge(5, 6, 1) graph.add_edge(6, 7, 1) graph.add_edge(7, 5, 1) graph.add_edge(7, 8, 1) algo = GraphAlgo(graph) self.assertEqual([[1, 2, 3, 4], [5, 6, 7], [8]], algo.connected_components()) graph.add_edge(5, 3, 1) self.assertEqual([[1, 2, 3, 4, 5, 6, 7], [8]], algo.connected_components()) algo = GraphAlgo() self.assertEqual([], algo.connected_components()) algo = GraphAlgo(None) self.assertEqual([], algo.connected_components())
def test_connected_components(self): g = DiGraph() g.add_node(1) g.add_node(2) g.add_node(3) g.add_node(4) g.add_node(5) g.add_edge(1, 2, 1) g.add_edge(2, 3, 1) g.add_edge(3, 1, 1) g.add_edge(3, 4, 1) g.add_edge(4, 5, 1) ga = GraphAlgo(g) expected = [[1, 2, 3], [4], [5]] actual = ga.connected_components() self.assertEqual(expected, actual) g = DiGraph() g.add_node(8) g.add_edge(8, 1, 3) ga = GraphAlgo(g) expected = [[8]] actual = ga.connected_components() self.assertEqual(expected, actual) g = DiGraph() g.add_edge(8, 1, 3) ga = GraphAlgo(g) expected = [[]] actual = ga.connected_components() self.assertEqual(expected, actual)
def time_test(g: DiGraph): random.seed(55555) algo = GraphAlgo(g) src = random.randint(0, g.NumberOfNodes) dest = random.randint(0, g.NumberOfNodes) startShortest = timer() algo.shortest_path(src, dest) endShortest = timer() startConnected = timer() algo.connected_components() endConnected = timer() component = random.randint(0, g.NumberOfNodes) startComponent = timer() algo.connected_component(component) endComponent = timer() print("My DiGraph:", g, " ->", "\n", "Shortest Path:", (endShortest - startShortest), "\n", "Connected Components:", (endConnected - startConnected), "\n", "Connected Component:", (endComponent - startComponent), "\n") MyTestCaseForComparison.shortestPathSumDiGraph += (endShortest - startShortest) MyTestCaseForComparison.connectedComponentsDiGraph += (endConnected - startConnected) MyTestCaseForComparison.connectedComponenTDiGraph += (endComponent - startComponent)
def test_connected_components(self): graph = self.build_graph() graph_a = GraphAlgo() GraphAlgo.__init__(graph_a, graph) connected = graph_a.connected_components() self.assertEqual(1, len(connected)) connected = graph_a.connected_components().__getitem__(0) self.assertEqual(5, len(connected)) graph.remove_edge(3, 4) graph.remove_edge(2, 3) graph_a = GraphAlgo() GraphAlgo.__init__(graph_a, graph) connected = graph_a.connected_components().__getitem__(1) self.assertEqual(1, len(connected)) self.assertTrue(3 in connected) self.assertFalse(2 in connected) connected = graph_a.connected_components().__getitem__(0) self.assertEqual(4, len(connected)) self.assertTrue(1 in connected) self.assertTrue(2 in connected) self.assertTrue(0 in connected) self.assertTrue(4 in connected) self.assertFalse(3 in connected) graph = self.build_empty_graph() GraphAlgo.__init__(graph_a, graph) self.assertEqual(0, len(graph_a.connected_components()))
def test_small_graph(self): g = DiGraph() algo = GraphAlgo(g) self.assertTrue(g.add_node(1)) self.assertTrue(g.add_node(2)) self.assertTrue(g.add_node(3)) self.assertTrue(g.add_node(4)) self.assertTrue(g.add_node(5)) self.assertTrue(g.add_node(6)) self.assertTrue(g.add_edge(1, 3, 9)) self.assertTrue(g.add_edge(1, 2, 3.45)) self.assertTrue(g.add_edge(1, 6, 14)) self.assertTrue(g.add_edge(2, 3, 10)) self.assertTrue(g.add_edge(2, 4, 2.34322)) self.assertTrue(g.add_edge(3, 6, 2)) self.assertTrue(g.add_edge(3, 4, 11)) self.assertTrue(g.add_edge(4, 5, 6)) self.assertTrue(g.add_edge(6, 5, 9)) self.assertEqual(algo.connected_components(), [[5], [6], [4], [3], [2], [1]]) self.assertTrue(g.add_edge(5, 3, 2.1211)) self.assertTrue(g.add_edge(4, 2, 2.34322)) self.assertTrue(g.add_edge(2, 1, 3.45)) self.assertEqual(algo.connected_components(), [[2, 4, 5, 6, 3, 1]]) self.assertEqual(algo.shortest_path(1, 5), (11.79322, [1, 2, 4, 5])) self.assertEqual(algo.shortest_path(4, 6), (10.1211, [4, 5, 3, 6])) self.assertTrue(g.add_node(7)) self.assertEqual(algo.connected_components(), [[2, 4, 5, 6, 3, 1], [7]]) self.assertEqual(algo.connected_component(7), [7]) self.assertTrue(g.remove_node(7)) self.assertEqual(algo.connected_components(), [[2, 4, 5, 6, 3, 1]]) self.assertEqual(algo.connected_component(6), [1, 2, 4, 3, 5, 6])
def maincheck_our(s): ga = GraphAlgo() t = time.time() ga.load_from_json(s) t = time.time() - t print('loaded:', t) print() t = time.time() ga.connected_components() t = time.time() - t print('sccs:', t) print() r = rnd(x=0) keys = list(ga.get_graph().get_all_v().keys()) s, e = map(r.choice, [keys, keys]) t = time.time() ga.connected_component(s) t = time.time() - t print('scc:', t) print() t = time.time() ga.shortest_path(s, e) t = time.time() - t print('sp:', t) print()
def test_connected_components(self): graph = DiGraph() graph_algo = GraphAlgo(graph) for i in range(0, 3): graph.add_node(i) graph.add_edge(0, 1, 1) graph.add_edge(1, 0, 1) graph.add_edge(1, 2, 1) graph.add_edge(2, 3, 1) graph.add_edge(3, 1, 1) check = [[0, 1], [2]] self.assertEqual(graph_algo.connected_components(), check) graph.add_edge(3, 2, 1) graph.add_edge(2, 1, 1) check2 = [[0, 1, 2]] self.assertEqual(graph_algo.connected_components(), check2) graph.remove_edge(0, 1) graph.remove_edge(3, 1) check3 = [[0], [1, 2]] self.assertEqual(graph_algo.connected_components(), check3) graph.remove_edge(1, 0) graph.remove_edge(1, 2) graph.remove_edge(2, 3) graph.remove_edge(3, 2) check4 = [[0], [1], [2]] self.assertEqual(graph_algo.connected_components(), check4)
class MyTestCase(unittest.TestCase): def setUp(self) -> None: graph = DiGraph() for i in range(10): graph.add_node(i) for i in range(9): graph.add_edge(i, i + 1, 3) self.ga = GraphAlgo(graph) def test_Save_and_load(self): file_name = "ga.json" self.ga.save_to_json(file_name) la = GraphAlgo() la.load_from_json("ga.json") self.assertEqual(self.ga.Graph, la.Graph) def test_shortest_path(self): self.assertEqual(self.ga.shortest_path(0, 1), [3.0, [0, 1]]) self.assertEqual(self.ga.shortest_path(0, 0), [inf, []]) self.assertEqual(self.ga.shortest_path(0, 9), [27.0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) self.ga.Graph.add_edge(0, 9, 5) self.assertEqual(self.ga.shortest_path(0, 9), [5.0, [0, 9]]) def test_connected_components(self): self.assertEqual(self.ga.connected_components(), [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]) self.ga.Graph.add_edge(1, 0, 5) self.assertEqual(self.ga.connected_components(), [[0, 1], [2], [3], [4], [5], [6], [7], [8], [9]]) self.assertEqual(self.ga.connected_component(0), [0, 1]) empty_graph = GraphAlgo() self.assertEqual(empty_graph.connected_components(), [])
class TestGraphAlgo(TestCase): def setUp(self): self.graph = None self.ga = GraphAlgo() def test_load_from_json(self): self.assertTrue(self.ga.load_from_json("../data/G_10_80_0.json")) self.assertTrue(self.ga.load_from_json("../data/G_10_80_1.json")) self.assertTrue(self.ga.load_from_json("../data/G_10_80_2.json")) def test_save_to_json(self): self.ga.load_from_json("../data/G_10_80_0.json") self.assertTrue(self.ga.save_to_json("graph1.json")) self.ga.load_from_json("../data/G_10_80_1.json") self.assertTrue(self.ga.save_to_json("graph2.json")) self.ga.load_from_json("../data/G_10_80_2.json") self.assertTrue(self.ga.save_to_json("graph3.json")) def test_shortest_path(self): self.ga.graph = graph_2 self.assertEqual("(6, [1, 2, 4, 3, 5, 0, 6])", str(self.ga.shortest_path(1, 6))) self.assertEqual("(2, [3, 5, 0])", str(self.ga.shortest_path(3, 0))) self.assertEqual("(0, [2])", str(self.ga.shortest_path(2, 2))) # no path no_path = (inf, []) self.assertEqual(no_path, self.ga.shortest_path(6, 1)) # shortest path between nodes that one or more of the nodes does not exist in the graph self.assertRaises(Exception, self.ga.shortest_path, (10, 1)) self.assertRaises(Exception, self.ga.shortest_path, (1, 10)) self.assertRaises(Exception, self.ga.shortest_path, (9, 10)) def test_connected_component(self): self.ga.graph = graph_3 for i in range(0, 10): for j in range(10): if j in range(3): self.assertEqual([0, 1, 2], sorted(self.ga.connected_component(j))) if j in range(3, 5): self.assertEqual([3, 4], sorted(self.ga.connected_component(j))) if j in range(5, 7): self.assertEqual([j], self.ga.connected_component(j)) if j in range(8, 11): self.assertEqual([7, 8, 9], sorted(self.ga.connected_component(j))) def test_connected_components(self): self.ga.graph = graph_2 self.assertEqual([[1, 2, 4, 3, 5, 0], [6]], self.ga.connected_components()) self.ga.graph = graph_3 self.assertEqual([[8, 9, 7], [5], [6], [2, 1, 0], [4, 3]], self.ga.connected_components()) def test_plot_graph(self): # plot graph without positions self.ga.graph = graph_2 self.ga.plot_graph() # plot graph with position self.ga.graph = graph_3 self.ga.plot_graph()
class MyTestCase(unittest.TestCase): def setUp( self ) -> None: # loads the file to GraphAlgo object ga and networkx DiGraph object nxg nxr = NXJsonReader() file = "C:\\Users\\User\\PycharmProjects\\Ex3\\data\\G_30000_240000_1.json" nxr.read(file) self.nxg = nxr.get_graph() self.ga = GraphAlgo() self.ga.load_from_json(file) def test_nx_shortest( self ): # calculate the shortest path and distance 10 times and return average runtime start = time.time() for i in range(10): nx.shortest_path(self.nxg, 1, 25468, weight="weight") nx.shortest_path_length(self.nxg, 1, 25468, weight="weight") end = time.time() print((end - start) / 10) def test_nx_CCS( self ): # finds all the connected components 10 times and return average runtime start = time.time() for i in range(10): nx.strongly_connected_components(self.nxg) end = time.time() print((end - start) / 10) def test_GA_shortest( self ): # calculate the shortest path and distance 10 times and return average runtime start = time.time() for i in range(10): self.ga.shortest_path(1, 25468) end = time.time() print((end - start) / 10) def test_GA_CCS( self ): # finds all the connected components 10 times and return average runtime start = time.time() for i in range(10): self.ga.connected_components() end = time.time() print((end - start) / 10) def test_GA_CC( self ): # finds the nodes that are part of 456 component 10 times and return average runtime start = time.time() for i in range(10): self.ga.connected_component(456) end = time.time() print((end - start) / 10)
def test_load_and_save(self): # g = GraphAlgo() # g.load_from_json("/Users/danielsela/PycharmProjects/OOP-Ex3/Graphs_no_pos/G_10_80_0.json") # g.connected_components() g = GraphAlgo() g.load_from_json( "/Users/danielsela/PycharmProjects/OOP-Ex3/Graphs_no_pos/G_1000_8000_0.json" ) start = time.time() g.connected_components() end = time.time() print("connected_components -> : %start sec" % (end - start))
def test_connected_components(self): # test connected components 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 correction of values for simple and small graph 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].get_key(), check[c][n]) # check correction of values in a big connected graph ga.load_from_json("C:\\Users\\User\\PycharmProjects\\Ex3\\Data\\A5") comps = ga.connected_components() com = comps[0] for n in range(len(com)): self.assertEqual(com[n].get_key(), n) # check correction against networkx nxr = NXJsonReader() nxr.read( "C:\\Users\\User\\PycharmProjects\\Ex3\\data\\G_10000_80000_1.json" ) nxg = nxr.get_graph() nx_comps = nx.strongly_connected_components(nxg) ga.load_from_json( "C:\\Users\\User\\PycharmProjects\\Ex3\\data\\G_10000_8000_1.json") comps = ga.connected_components() # convert the returned values to list of lists of integers nx_list = list() ga_list = list() for nxc in nx_comps: sub_list = list() for n in nxc: sub_list.append(n) sub_list.sort() nx_list.append(sub_list) for c in comps: sub_list = list() for n in c: sub_list.append(n.get_key()) sub_list.sort() ga_list.append(sub_list) # sort and compare results ga_list.sort() nx_list.sort() for sub in range(len(ga_list)): for n in range(len(ga_list[sub])): self.assertEqual(ga_list[sub][n], nx_list[sub][n])
def test_connected_components(self): """ test the connected components function in the graph algo """ graph = create_graph1() graph_algo = GraphAlgo(graph) comp = graph_algo.connected_components() list_comp = [[0, 1, 4], [5, 6], [2, 3, 7]] self.assertTrue(is_list_in_lists(comp, list_comp)) graph2 = create_graph_2() algo = GraphAlgo(graph2) comp2 = algo.connected_components() list_comp2 = [[0, 1], [2, 4, 5], [3], [8], [6, 7]] self.assertTrue(is_list_in_lists(comp2, list_comp2))
def test_connected_components(self): ga = GraphAlgo(simpleGraphGenerator()) self.assertEqual(ga.connected_components().sort(), [[3], [0, 1, 2]].sort(), "connected components dont math") ga.get_graph().remove_edge(2, 0) self.assertEqual(ga.connected_components().sort(), [[0], [1], [2], [3]].sort(), "connected components dont math") ga = GraphAlgo(DiGraph()) self.assertEqual(ga.connected_components(), [], "emtpy graph have no SCC")
def run_time(file_name: str, src, dest): graph_algo = GraphAlgo() dt = 0 for i in range(100): start = timeit.default_timer() graph_algo.load_from_json(file_name) stop = timeit.default_timer() dt = dt + (stop - start) print(graph_algo.get_graph()) print("run time of read from json file and build the graph: ", dt / 100) dt = 0 for i in range(100): start = timeit.default_timer() connecteds = graph_algo.connected_components() stop = timeit.default_timer() dt = dt + (stop - start) # print(connecteds) print("run time of connected_components: ", dt / 100) dt = 0 for i in range(100): start = timeit.default_timer() graph_algo.connected_component(1) stop = timeit.default_timer() dt = dt + (stop - start) print("run time of connected_component: ", dt / 100) dt = 0 for i in range(100): start = timeit.default_timer() graph_algo.shortest_path(src, dest) stop = timeit.default_timer() dt = dt + (stop - start) print("run time of shortest_path: ", dt / 100)
def test_connected_components_none_graph(self): ga1 = GraphAlgo(None) self.assertEqual( "[]", ga1.connected_components().__str__(), "connected_components returns " "uncorrected list for none graph")
def test_connected_components(self): self.assertEqual(self.ga.connected_components(), [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]) self.ga.Graph.add_edge(1, 0, 5) self.assertEqual(self.ga.connected_components(), [[0, 1], [2], [3], [4], [5], [6], [7], [8], [9]]) self.assertEqual(self.ga.connected_component(0), [0, 1]) empty_graph = GraphAlgo() self.assertEqual(empty_graph.connected_components(), [])
def test_connected_component(self): graph = self.build_graph() graph_a = GraphAlgo() GraphAlgo.__init__(graph_a, graph) is_connected = graph_a.connected_component(3) y = (len(is_connected)) self.assertEqual(5, y) for i in range(4): self.assertTrue(i in is_connected) graph.remove_edge(2, 3) GraphAlgo.__init__(graph_a, graph) is_connected = graph_a.connected_component(3).__getitem__(0) self.assertEqual(3, is_connected) graph.remove_edge(3, 4) graph.add_edge(2, 4, 10) GraphAlgo.__init__(graph_a, graph) is_connected = graph_a.connected_component(3).__getitem__(0) self.assertEqual(3, is_connected) graph = self.build_empty_graph() GraphAlgo.__init__(graph_a, graph) self.assertEqual(0, len(graph_a.connected_components()))
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 test_connected_components(self): d1 = DiGraph() d1.add_node(0, (1, 2, 0)) d1.add_node(1, (4, 7, 0)) d1.add_node(2, (0, 1, 4)) d1.add_node(3, (0, 9, 8)) d1.add_node(4, (4, 4, 0)) d1.add_node(5, (5, 15, 0)) d1.add_node(6, (21, 2, 0)) d1.add_node(7, (13, 13, 0)) d1.add_edge(0, 1, 1) d1.add_edge(1, 2, 2) d1.add_edge(2, 3, 3) d1.add_edge(2, 0, 3) d1.add_edge(3, 4, 0.5) d1.add_edge(4, 5, 0.5) d1.add_edge(4, 7, 0.5) d1.add_edge(5, 6, 0.5) d1.add_edge(6, 4, 0.5) d1.add_edge(6, 7, 0.5) d1.add_node(8, (6, 8, 0)) d1.add_node(9, (7, 8, 0)) d1.add_edge(8, 9, 1) d1.add_edge(9, 8, 1) d1.add_edge(8, 0, 1) algo = GraphAlgo(d1) self.assertTrue([[0, 2, 1], [3], [4, 6, 5], [7], [9, 8]] == algo.connected_components())
def test_connected_components(self): graph = DiGraph() graph.add_node(0) graph.add_node(1) graph.add_node(2) graph.add_node(3) graph.add_node(4) graph.add_node(5) graph.add_node(6) graph.add_node(7) graph.add_edge(0, 1, 2) graph.add_edge(1, 2, 2) graph.add_edge(2, 0, 2) graph.add_edge(5, 0, 2) graph.add_edge(5, 6, 2) graph.add_edge(6, 2, 2) graph.add_edge(6, 0, 2) graph.add_edge(6, 4, 2) graph.add_edge(4, 5, 2) graph.add_edge(3, 4, 2) graph.add_edge(3, 7, 2) graph.add_edge(7, 3, 2) graph.add_edge(7, 5, 2) g_algo = GraphAlgo(graph) answer = [[0, 1, 2], [3, 7], [4, 5, 6]] self.assertEqual(g_algo.connected_components(), answer)
def compares_run_time_cc_json(file_name: str) -> list: """ Compares the running time of the Connected components method in GraphAlgo and Networkx The graph is created from json file :param file_name: :return: list , the first place is the results of nx graph and the second is the results of GraphAlgo """ graph_algo = GraphAlgo() graph_algo.load_from_json(file_name) # load the json g = nx.Graph() nx_graph = g.to_directed() for node in graph_algo.get_graph().get_all_v().values( ): # copies all data to nx_graph nx_graph.add_node(node.key, pos=node.pos) for edges in graph_algo.get_graph().Edges.values(): for edge in edges.values(): nx_graph.add_edge(edge.src, edge.dest, weight=edge.weight) start_time = datetime.now() # cc run time of networkx nx_results = nx.number_strongly_connected_components(nx_graph) end_time = datetime.now() run_time_nx = end_time - start_time print("run time of networkx graph:", run_time_nx) start_time = datetime.now() # cc run time of GraphAlgo my_results = len(graph_algo.connected_components()) end_time = datetime.now() run_time_my = end_time - start_time print("run time of GraphAlgo :", run_time_my) return [nx_results, my_results]
class TestGraphAlgo(unittest.TestCase): def setUp(self) -> None: self.g = DiGraph() self.g.add_node(1) self.g.add_node(2) self.g.add_node(3) self.g.add_node(4) self.g.add_node(5) self.g.add_node(6) self.g.add_edge(1, 2, 3) self.g.add_edge(1, 3, 2) self.g.add_edge(2, 5, 3) self.g.add_edge(3, 1, 1) self.g.add_edge(3, 4, 2) self.g.add_edge(4, 3, 1) self.g.add_edge(4, 5, 1) self.g.add_edge(6, 2, 1) self.ga = GraphAlgo(self.g) def tearDown(self) -> None: return None def test_get_graph(self): self.assertEqual(True, self.ga.save_to_json('testgraph.json')) self.assertEqual(True, self.ga.load_from_json('testgraph.json')) self.assertEqual(False, self.ga.load_from_json('noname.json')) def test_load_save_json(self): self.assertEqual(self.g, self.ga.get_graph()) def test_shortest_path(self): self.assertEqual((5, [1, 3, 4, 5]), self.ga.shortest_path(1, 5)) self.assertEqual((inf, []), self.ga.shortest_path(1, 7)) self.assertEqual((inf, []), self.ga.shortest_path(1, 6)) def test_connected_component(self): self.assertEqual([1, 3, 4], self.ga.connected_component(1)) self.ga.get_graph().remove_node(1) self.assertEqual([], self.ga.connected_component(1)) self.ga = GraphAlgo(None) self.assertEqual([], self.ga.connected_component(1)) def test_connected_components(self): self.assertEqual([[1, 3, 4], [2], [5], [6]], self.ga.connected_components()) self.ga = GraphAlgo(None) self.assertEqual([], self.ga.connected_components())
def test_connected_components(self): graph = GraphAlgo(create_small_graph()) self.assertEqual(graph.connected_components(), [[0, 1], [2, 3]]) graph.get_graph().remove_edge(2, 3) self.assertEqual(graph.get_graph().e_size(), 5) # the edge was deleted but still shows that yes
def test_scc2(self): g = graph_builder(100) algo = GraphAlgo(g) for n in g.get_all_v().keys(): self.assertEqual(1, len(algo.connected_component(n))) self.assertEqual(0, len(algo.connected_component(200))) # un-existed node self.assertEqual(100, len(algo.connected_components()))
def test_connected_components(self): self.graph = DiGraph() galg = GraphAlgo(self.graph) for i in range(7): self.graph.add_node(i, (1, 5, 7)) ans = [[0], [1], [2], [3], [4], [5], [6]] self.assertEqual(ans, galg.connected_components()) self.graph.add_edge(5, 6, 4) self.graph.add_edge(3, 4, 3) self.graph.add_edge(4, 5, 2) self.graph.add_edge(4, 6, 5) self.graph.add_edge(4, 3, 2) self.graph.add_edge(6, 3, 2) ans = galg.connected_components() self.assertTrue([0] in ans) self.assertTrue([1] in ans) self.assertTrue([3, 4, 6, 5] in ans)
def check5(): ga = GraphAlgo() file = '../data/G_20000_160000_1.json' ga.load_from_json(file) start1 = time.time() dist, path = ga.shortest_path(25, 300) # print(dist, path) end1 = time.time() print(end1 - start1) start2 = time.time() ga.connected_component(900) end2 = time.time() print(end2 - start2) start3 = time.time() ga.connected_components() end3 = time.time() print(end3 - start3)
def test_connected_components_time(): """ checks the time for the connected_components on a big graph """ v, e = 10**5, 10**5 seed(2) g = graphCreator(v) for i in range(e): w = random.uniform(0, 30) n1 = random.randint(0, v - 1) n2 = random.randint(0, v - 1) g.add_edge(n1, n2, w) ga1 = GraphAlgo(g) start = timeit.default_timer() ga1.connected_components() end = timeit.default_timer() print("time for connected_components-> " + str(end - start))
def check_functions(file_path) -> bool: with open(file_path) as f: json_data = json.loads(f.read()) nx_graph = nx.DiGraph() nx_graph.add_nodes_from(elem['id'] for elem in json_data['Nodes']) nx_graph.add_weighted_edges_from( (elem['src'], elem['dest'], elem['w']) for elem in json_data['Edges']) dg_algo = GraphAlgo() dg_algo.load_from_json(file_path) # nx.draw(nx_graph, with_labels=True) # plt.show() print("JSON file: " + file_path) start_time = time.time_ns() dg_algo.shortest_path(0, 5) end_time = time.time_ns() resultTime = end_time - start_time print("Running time Python shortest_path: " + str(resultTime / 1000000) + " ms") start_time = time.time_ns() print("Shortest Path (0,5) (NetworkX Graph):\n" + str(nx.shortest_path(nx_graph, source=0, target=5, weight='weight'))) end_time = time.time_ns() resultTime = end_time - start_time print("Running time NetworkX shortest_path: " + str(resultTime / 1000000) + " ms") start_time = time.time_ns() print(dg_algo.connected_components()) end_time = time.time_ns() resultTime = end_time - start_time print("Running time Python connected_components: " + str(resultTime / 1000000) + " ms") start_time = time.time_ns() connected_components_graph = nx.strongly_connected_components(nx_graph) end_time = time.time_ns() # for elem in connected_components_graph: # print(elem) resultTime = end_time - start_time print("Running time NetworkX connected_components: " + str(resultTime / 1000000) + " ms") start_time = time.time_ns() dg_algo.connected_component(9) # for elem in connected_components_graph: # print(elem) end_time = time.time_ns() resultTime = end_time - start_time print("Running time Python connected_component: " + str(resultTime / 1000000) + " ms")
def test_10000_nodes_with_80000_edges(self): file_name = "comapring_files/G_10000_80000_0.json" nx = network_x() nx.load_from_json(file_name) my_graph = GraphAlgo() my_graph.load_from_json(file_name) start = time.time() # test for connected copmponents nx_scc = nx.connected_components() end = time.time() scc_nx_time = end - start start = time.time() my_graph_scc = my_graph.connected_components() end = time.time() scc_my_graph_time = end - start self.assertTrue(equal_list_of_lists(nx_scc, my_graph_scc)) # test for connected copmponent start = time.time() my_graph_connected_component = my_graph.connected_component(1500) end = time.time() my_graph_connected_component_time = end - start # test for shortest path start = time.time() nx_shortest_path_len = nx.shortest_path_length(0, 9999) nx_shortest_path_list = nx.shortest_path_list(0, 9999) end = time.time() nx_shortest_path_time = end - start start = time.time() my_graph_shortest_path_len, my_graph_shortest_path_list = my_graph.shortest_path( 0, 9999) end = time.time() my_graph_shortest_path_time = end - start self.assertTrue(nx_shortest_path_len, my_graph_shortest_path_len) self.assertTrue(nx_shortest_path_list, my_graph_shortest_path_list) print("10000_nodes_with_80000_edges:") # print( # f"run time for my graph\n connected component: {my_graph_connected_component_time}, connected components: {scc_my_graph_time}, shortest path: {my_graph_shortest_path_time}") # print( # f"run time for networkX\n connected components: {scc_nx_time}, shortest path: {nx_shortest_path_time}") print( f"python:\n shortest path distance: {my_graph_shortest_path_len}\n shortest path list: {my_graph_shortest_path_list}\n connected component: {len(my_graph_connected_component)}\n connected components: {len(my_graph_scc)}" ) print( f"networkX :\n shortest path: {nx_shortest_path_len}\n shortest path list: {nx_shortest_path_list}\n connected components: {len(nx_scc)}" ) print("-" * 50)