def test_shortest_path(self): graph = self.build_graph() graph_a = GraphAlgo() GraphAlgo.__init__(graph_a, graph) path_len = graph_a.shortest_path(2, 4).__getitem__(0) path_list = graph_a.shortest_path(2, 4).__getitem__(1) expected_list = [2, 3, 4] self.assertEqual(4, path_len) self.assertEqual(3, len(path_list)) self.assertEqual(expected_list.__str__(), path_list.__str__()) path_len = graph_a.shortest_path(1, 4).__getitem__(0) path_list = graph_a.shortest_path(1, 4).__getitem__(1) expected_list = [1, 0, 4] self.assertEqual(5.5, path_len) self.assertEqual(3, len(path_list)) self.assertEqual(expected_list.__str__(), path_list.__str__()) graph = self.build_empty_graph() graph_a.__init__(graph) path_len = graph_a.shortest_path(1, 50).__getitem__(0) path_list = graph_a.shortest_path(1, 50).__getitem__(1) self.assertEqual(math.inf, path_len) self.assertEqual([], path_list)
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 test_shortest_path(self): graph = GraphAlgo() graph.__init__(create_graph()) self.assertEqual(len(graph.get_graph().get_all_v()), 11) graph.get_graph().add_edge(0, 1, 1) graph.get_graph().add_edge(0, 2, 3) graph.get_graph().add_edge(1, 4, 1) graph.get_graph().add_edge(2, 6, 1) graph.get_graph().add_edge(2, 1, 3) self.assertEqual(graph.shortest_path(0, 0), (0, [0])) self.assertEqual(graph.shortest_path(0, 6), (4, [0, 2, 6])) self.assertEqual(graph.shortest_path(0, 4), (2, [0, 1, 4])) self.assertEqual(graph.shortest_path(0, 9), (inf, []))
def test_save_load(self): algo = GraphAlgo() algo.load_from_json('../data/A4') G = copy.deepcopy(algo.get_graph()) self.assertTrue(algo.get_graph().__eq__(G)) G.remove_node(5) self.assertNotEqual(algo.get_graph(), G) algo.__init__(G) self.assertEqual(algo.get_graph(), G) algo.G.remove_node(1) algo.G.remove_node(2) algo.G.remove_node(3) algo.save_to_json('test_save_load.json') G = copy.deepcopy(algo.get_graph()) algo.__init__(DiGraph()) self.assertNotEqual(algo.get_graph(), G) algo.load_from_json('test_save_load.json') self.assertTrue(algo.get_graph().__eq__(G))
def test_json(self): graph = self.build_graph() graph_a = GraphAlgo() GraphAlgo.__init__(graph_a, graph) self.assertTrue(graph_a.save_to_json('saved.txt')) self.assertTrue(graph_a.load_from_json('saved.txt')) self.assertEqual(graph, graph_a.get_graph()) graph = self.build_empty_graph() GraphAlgo.__init__(graph_a, graph) self.assertTrue(graph_a.save_to_json('saved1.txt')) self.assertTrue(graph_a.load_from_json('saved1.txt')) self.assertEqual(graph, graph_a.get_graph()) graph_b = GraphAlgo() graph_a = GraphAlgo() self.assertTrue(graph_a.load_from_json("../../data/A4")) self.assertTrue(graph_a.save_to_json("check")) self.assertTrue(graph_b.load_from_json("check")) self.assertEqual(graph_b.get_graph(), graph_a.get_graph())
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()))
class TestGraphAlgo(unittest.TestCase): def test_save_and_load(self): # add nodes self.graph = DiGraph() self.graph.add_node(1, (1.44, 2.43, 4.477)) self.graph.add_node(2, (2.789, 4.3331, 6.41457)) self.graph.add_node(3, (3.142, 4.1376, 2.1376)) self.graph.add_node(4, (5.137, 6.2, 12.137)) # add edges self.graph.add_edge(1, 2, 2) self.graph.add_edge(2, 3, 3) self.graph.add_edge(3, 4, 4) self.graph.add_edge(4, 1, 6) # init graph algo self.algo = GraphAlgo() self.algo.__init__(self.graph) # save as JSON file self.algo.save_to_json("../data/test.json") self.graph1 = self.algo.get_graph() # load JSON file self.algo.load_from_json("../data/test.json") self.graph2 = self.algo.get_graph() # check if Graph's.toString are equal self.assertEqual(self.graph1.__repr__(), self.graph2.__repr__()) def test_save_and_load_no_pos(self): # add nodes self.graph = DiGraph() self.graph.add_node(1) self.graph.add_node(2) self.graph.add_node(3) self.graph.add_node(4) # add edges self.graph.add_edge(1, 2, 2) self.graph.add_edge(2, 3, 3) self.graph.add_edge(3, 4, 4) self.graph.add_edge(4, 1, 6) # init graph algo self.algo = GraphAlgo() self.algo.__init__(self.graph) # save as JSON file self.algo.save_to_json("../data/test.json") self.graph1 = self.algo.get_graph() # load JSON file self.algo.load_from_json("../data/test.json") self.graph2 = self.algo.get_graph() # check if Graph's.toString are equal self.assertEqual(self.graph1.__repr__(), self.graph2.__repr__()) def test_shortest_path(self): # add nodes self.graph = DiGraph() self.graph.add_node(1, (1.44, 2.43, 4.477)) self.graph.add_node(2, (2.789, 4.3331, 6.41457)) self.graph.add_node(3, (3.142, 4.1376, 2.1376)) self.graph.add_node(4, (5.137, 6.2, 12.137)) # add edges self.graph.add_edge(1, 2, 2) self.graph.add_edge(2, 3, 3) self.graph.add_edge(3, 4, 4) self.graph.add_edge(4, 1, 6) # init graph algo self.algo = GraphAlgo() self.algo.__init__(self.graph) # shortest path self.assertEqual("(9, [1, 2, 3, 4])", str(self.algo.shortest_path(1, 4))) def test_connected_components(self): p = (1.21, 2.12, 3.16) # add nodes self.graph = DiGraph() self.graph.add_node(1, p) self.graph.add_node(2, p) self.graph.add_node(3, p) self.graph.add_node(4, p) # add edges self.graph.add_edge(1, 2, 2) self.graph.add_edge(2, 3, 3) self.graph.add_edge(3, 4, 4) self.graph.add_edge(4, 2, 9) # init graph algo self.algo = GraphAlgo() self.algo.__init__(self.graph) # connected_components self.assertEqual([[1], [2, 3, 4]], self.algo.connected_components()) # connected_components(id) self.assertEqual([3, 4, 2], self.algo.connected_component(3)) def test_plot_graph(self): p1 = (1.21, 2.12, 3.16) p2 = (2.45, 4.16, 11.22) p3 = (10.6, 3.12, 3.04) # add nodes self.graph = DiGraph() self.graph.add_node(1, p1) self.graph.add_node(2, p2) self.graph.add_node(3, p3) self.graph.add_node(4) # no pos given # add edges self.graph.add_edge(1, 2, 2) self.graph.add_edge(2, 3, 3) self.graph.add_edge(3, 4, 4) self.graph.add_edge(4, 2, 9) # init graph algo self.algo = GraphAlgo() self.algo.__init__(self.graph) # print(self.algo.get_graph()) self.algo.create_graph() def test_given_graphs(self): # init graph algo self.algo = GraphAlgo() # load JSON file self.algo.load_from_json("../data/Graph_no_pos/G_30000_240000_0.json") # self.algo.load_from_json("../data/Graphs_on_circle/G_30000_240000_1.json"); # self.algo.load_from_json("../data/Graphs_random_pos/G_30000_240000_2.json"); # self.algo.shortest_path(0, 30000) print(self.algo.connected_components())
class MyTestCase(unittest.TestCase): global is_not_connected global connected global algo def setUp(self): """The method creates 3 graphs for this unittest. connected graph and the other one isn't connected and the last one is scc_graph. algo initialized on connected""" self.startTime = time.time() g = DiGraph() for n in range(15): g.add_node(n) g.add_edge(0, 9, 2.5) g.add_edge(1, 9, 6.7) g.add_edge(4, 9, 40) g.add_edge(3, 10, 7) g.add_edge(3, 13, 27) g.add_edge(3, 2, 4.5) g.add_edge(14, 5, 21) g.add_edge(14, 8, 41) g.add_edge(11, 2, 17) self.is_not_connected = g graph = DiGraph() for n in range(10): graph.add_node(n) graph.add_edge(0, 1, 12) graph.add_edge(1, 2, 67) graph.add_edge(2, 3, 43) graph.add_edge(3, 4, 17) graph.add_edge(4, 5, 27) graph.add_edge(5, 6, 4.5) graph.add_edge(6, 7, 21) graph.add_edge(7, 8, 41) graph.add_edge(8, 9, 11.23) graph.add_edge(9, 0, 13.765649959) self.connected = graph di_graph = DiGraph() for n in range(10): di_graph.add_node(n) di_graph.add_edge(0, 1, 13) di_graph.add_edge(1, 2, 32) di_graph.add_edge(1, 4, 11) di_graph.add_edge(2, 3, 12) di_graph.add_edge(2, 6, 35) di_graph.add_edge(3, 2, 7) di_graph.add_edge(3, 7, 21) di_graph.add_edge(4, 0, 10) di_graph.add_edge(4, 5, 47) di_graph.add_edge(5, 6, 31) di_graph.add_edge(5, 8, 91) di_graph.add_edge(6, 5, 0.2) di_graph.add_edge(7, 3, 29) di_graph.add_edge(7, 6, 61) di_graph.add_edge(8, 5, 22) di_graph.add_edge(9, 8, 75) self.scc_graph = di_graph self.algo = GraphAlgo(self.connected) def tearDown(self): """This method is activated after eatch test is done, to calculate the time it ran""" t = time.time() - self.startTime print('%s: %.3f' % (self.id(), t)) def test_save_load(self): """Initialization algo on Connected. Then, save Connected in file whose name is "OurGraph". After this, replace the graph of algo to is_not_onnected. Load algo on "myGraph" file. Now, algo operates Connected again, instead of is_not_onnected.""" print("algo is operating now on connected:" + str(self.algo.get_graph().as_dict())) print() self.assertFalse(self.algo.save_to_json("")) self.assertTrue(self.algo.save_to_json("OurGraph")) self.algo.__init__(self.is_not_connected) print("algo is operating now on is_not_connected:" + str(self.algo.myGraph.as_dict())) print() self.assertFalse(self.algo.load_from_json("")) self.assertFalse(self.algo.load_from_json("fakefile")) self.assertTrue(self.algo.load_from_json("OurGraph")) print(str(self.algo.myGraph)) print(str(self.is_not_connected)) self.assertFalse(self.algo.get_graph().__eq__(self.is_not_connected)) self.assertTrue(self.algo.get_graph().__eq__(self.connected)) print("algo is operating now on connected:" + str(self.algo.myGraph.as_dict())) print() self.algo.plot_graph() def test_shortest_path_list(self): """Creating a graph and checking the shortestPathDist on it""" print("Checking shortest_path_list... ") g1 = DiGraph() for i in range(0, 5): g1.add_node(i) g1.add_edge(0, 1, 2.5) g1.add_edge(0, 1, 20) g1.add_edge(1, 2, 6.7) g1.add_edge(2, 3, 40) g1.add_edge(3, 4, 10) g1.add_edge(4, 3, 1) g1.add_edge(3, 2, 16) g1.add_edge(2, 1, 0.5) g1.add_edge(1, 0, 20) g1.add_edge(4, 2, 70) g1.add_edge(2, 4, 70) g1.add_edge(1, 2, 100) g1.add_edge(3, 1, 80) g1.add_edge(1, 3, 80) operator = GraphAlgo(g1) self.assertEqual([1], operator.shortest_path(1, 1)[1]) self.assertEqual([0, 1, 2], operator.shortest_path(0, 2)[1]) self.assertEqual([0, 1, 2, 3], operator.shortest_path(0, 3)[1]) self.assertEqual([0, 1, 2, 3, 4], operator.shortest_path(0, 4)[1]) self.assertEqual([4, 3], operator.shortest_path(4, 3)[1]) self.assertEqual([4, 3, 2], operator.shortest_path(4, 2)[1]) self.assertEqual([4, 3, 2, 1], operator.shortest_path(4, 1)[1]) self.assertEqual([4, 3, 2, 1, 0], operator.shortest_path(4, 0)[1]) def test_shortest_path_dist(self): """Creating a graph and checking the shortestPath on it""" print("Checking shortest_path _dist... ") g1 = DiGraph() for i in range(0, 5): g1.add_node(i) g1.add_edge(0, 1, 2.5) g1.add_edge(0, 1, 20) g1.add_edge(1, 2, 6.7) g1.add_edge(2, 3, 40) g1.add_edge(3, 4, 10) g1.add_edge(4, 3, 1) g1.add_edge(3, 2, 16) g1.add_edge(2, 1, 0.5) g1.add_edge(1, 0, 20) g1.add_edge(4, 2, 70) g1.add_edge(2, 4, 70) g1.add_edge(1, 2, 100) g1.add_edge(3, 1, 80) g1.add_edge(1, 3, 80) operator = GraphAlgo(g1) self.assertEqual(float('inf'), operator.shortest_path(9, 1)[0]) self.assertEqual(0, operator.shortest_path(1, 1)[0]) self.assertEqual(9.2, operator.shortest_path(0, 2)[0]) self.assertEqual(49.2, operator.shortest_path(0, 3)[0]) self.assertEqual(59.2, operator.shortest_path(0, 4)[0]) self.assertEqual(1, operator.shortest_path(4, 3)[0]) self.assertEqual(17, operator.shortest_path(4, 2)[0]) self.assertEqual(17.5, operator.shortest_path(4, 1)[0]) self.assertEqual(37.5, operator.shortest_path(4, 0)[0]) def test_scc(self): """Test for SCC: 1. print all the SCC's in connected: we have only one SCC, because the graph is connected 2. init algo on scc_graph 3. print all the SCC's in scc_graph 4. print SCC's of 0, 4, 3 5. check if we have 4 SCC in our graph 6. check if nodes 6, 8 have same SCC""" print("List of all the SCC's in connected :" + str(self.algo.connected_components())) self.algo.__init__(self.scc_graph) self.algo.plot_graph() print("List of all the SCC's in scc_graph :" + str(self.algo.connected_components())) print("The SCC of node 0: " + str(self.algo.connected_component(0))) print("The SCC of node 4: " + str(self.algo.connected_component(4))) print("The SCC of node 3: " + str(self.algo.connected_component(3))) self.assertEqual(4, len(self.algo.connected_components())) self.assertTrue( True, self.algo.connected_component(6) == self.algo.connected_component( 8)) def test_shortestPathCompare1(self): """Runs the shortestPath method from this project's algo, and runs it also through netWorkX, to compare results and performance. runs on graph: G_10_80_1.json """ print("reading from file...", self.algo.load_from_json("G_10_80_1.json")) graphnx1 = nx.DiGraph() for nodeKey in self.algo.myGraph._nodes.keys(): graphnx1.add_node(nodeKey) for src in self.algo.get_graph()._edges.keys(): for dest in self.algo.get_graph().all_out_edges_of_node( src).keys(): graphnx1.add_edge( src, dest, weight=self.algo.get_graph().all_out_edges_of_node( src).get(dest)) self.startTime = time.time() print("shortestPath is ", self.algo.shortest_path(0, 4)) print("Networx: ") print(single_source_dijkstra(graphnx1, 0, 4)) self.assertEqual(graphnx1.number_of_nodes(), self.algo.myGraph.v_size()) def test_shortestPathCompare2(self): """Runs the shortestPath method from this project's algo, and runs it also through netWorkX, to compare results and performance. runs on graph: G_100_800_1.json """ print("reading from file...", self.algo.load_from_json("G_100_800_1.json")) graphnx2 = nx.DiGraph() for nodeKey in self.algo.myGraph._nodes.keys(): graphnx2.add_node(nodeKey) for src in self.algo.myGraph._nodes.keys(): for dest in self.algo.myGraph._edges.get(src).keys(): graphnx2.add_edge( src, dest, weight=self.algo.myGraph._edges.get(src).get(dest)) self.startTime = time.time() print("shortestPath is ", self.algo.shortest_path(0, 4)) print("Networx: ") print(self.algo.shortest_path(0, 4)) print(single_source_dijkstra(graphnx2, 0, 4)) def test_shortestPathCompare3(self): """Runs the shortestPath method from this project's algo, and runs it also through netWorkX, to compare results and performance. runs on graph: G_1000_8000_1.json """ print("reading from file...", self.algo.load_from_json("G_1000_8000_1.json")) graphnx3 = nx.DiGraph() for nodeKey in self.algo.myGraph._nodes.keys(): graphnx3.add_node(nodeKey) for src in self.algo.myGraph._edges.keys(): for dest in self.algo.myGraph._edges.get(src).keys(): graphnx3.add_edge( src, dest, weight=self.algo.myGraph._edges.get(src).get(dest)) self.startTime = time.time() print("shortestPath is ", self.algo.shortest_path(0, 4)) print("Networx: ") print(single_source_dijkstra(graphnx3, 0, 4)) def test_shortestPathCompare4(self): """Runs the shortestPath method from this project's algo, and runs it also through netWorkX, to compare results and performance. runs on graph: G_10000_80000_1.json """ print("reading from file...", self.algo.load_from_json("G_10000_80000_1.json")) graphnx4 = nx.DiGraph() for nodeKey in self.algo.myGraph._nodes.keys(): graphnx4.add_node(nodeKey) for src in self.algo.myGraph._edges.keys(): for dest in self.algo.myGraph._edges.get(src).keys(): graphnx4.add_edge( src, dest, weight=self.algo.myGraph._edges.get(src).get(dest)) self.startTime = time.time() print("shortestPath is ", self.algo.shortest_path(0, 4)) print("Networx: ") print(single_source_dijkstra(graphnx4, 0, 4)) def test_shortestPathCompare5(self): """Runs the shortestPath method from this project's algo, and runs it also through netWorkX, to compare results and performance. runs on graph: G_20000_160000_1.json """ print("reading from file...", self.algo.load_from_json("G_20000_160000_1.json")) graphnx5 = nx.DiGraph() for nodeKey in self.algo.myGraph._nodes.keys(): graphnx5.add_node(nodeKey) for src in self.algo.myGraph._edges.keys(): for dest in self.algo.myGraph._edges.get(src).keys(): graphnx5.add_edge( src, dest, weight=self.algo.myGraph._edges.get(src).get(dest)) self.startTime = time.time() print("shortestPath is ", self.algo.shortest_path(0, 4)) print("Networx: ") print(single_source_dijkstra(graphnx5, 0, 4)) def test_shortestPathCompare6(self): """Runs the shortestPath method from this project's algo, and runs it also through netWorkX, to compare results and performance. runs on graph: G_30000_240000_1.json """ print("reading from file...", self.algo.load_from_json("G_30000_240000_1.json")) graphnx6 = nx.DiGraph() for nodeKey in self.algo.myGraph._nodes.keys(): graphnx6.add_node(nodeKey) for src in self.algo.myGraph._edges.keys(): for dest in self.algo.myGraph._edges.get(src).keys(): graphnx6.add_edge( src, dest, weight=self.algo.myGraph._edges.get(src).get(dest)) self.startTime = time.time() print("shortestPath is ", self.algo.shortest_path(0, 4)) print("Networx: ") print(single_source_dijkstra(graphnx6, 0, 4))
data = json.load(f) for node in data['Nodes']: G.add_node(node['id'], pos=(node['pos'])) for edge in data['Edges']: G.add_edge(edge['src'], edge['dest'], weight=edge['w']) return G # nx.draw(G, with_labels=1) # plt.show() g = DiGraph() g_a = GraphAlgo() g_a.__init__(g) print("shortest path comparison") file_name = '../data/G_10000_80000_1.json' print("this is the 10000_80000 graph shortest path comparison") start_time = time.time() c = nx.single_source_dijkstra(from_json(file_name), 226, 2797) end_time1 = (time.time() - start_time) print("--- %s seconds --- Networkx" % end_time1) print(c) g_a.load_from_json(file_name) start_time = time.time() c = g_a.shortest_path(226, 2797) print("--- %s seconds --- python" % (time.time() - start_time)) print(c) print('\n') print("this is the 100_800 graph shortest path comparison")
def test_save_to_json(self): graph = self.build_graph() graph_a = GraphAlgo() GraphAlgo.__init__(graph_a, graph) self.assertTrue(graph_a.save_to_json("check1"))