def test_add_edge_and_remove_edge(self): g = DiGraph() # adding nodes to the graph for i in range(10): g.add_node(i) # creating edges for i in range(4): g.add_edge(i, i + 3, i * 5 / 2) my_dict = g.get_all_v() # checking if the edges successfully added in the node for i in range(4): current_node = my_dict[i] edge_dic = current_node.edges_towards self.assertEqual(edge_dic[i + 3], i * 5 / 2) # removing the edges for i in range(4): g.remove_edge(i, i + 3) my_dict = g.get_all_v() flag = True # checking if the edges were removed successfully for i in range(4): current_node = my_dict[i] empty_dict = {} if empty_dict != current_node.edges_towards: flag = False break self.assertEqual(True, flag)
def single_node_graph(self) -> DiGraph: """ This method creates a new graph with only one node :return: DiGraph """ graph = DiGraph() graph.add_node(100) return graph
def test_add_node(self): g = DiGraph() flag = True for i in range(10): g.add_node(i) if not flag: break self.assertEqual(flag, True)
def test_basic_graph(self): """ This test contains the basic methods implemented in DiGraph @:param: mc -> measured by the method "get_mc" :return: True if it works """ print("first test") graph = DiGraph() mc = graph.get_mc() self.assertEqual(mc, 0, "No move was made in the Graph") print("Completed")
def test_node_size(self): """ This test contains the basic methods implemented in DiGraph @:param: nodes -> measured by the method "v_size" :return: True if it works """ print("nodes test") graph = DiGraph() nSize = graph.v_size() self.assertEqual(nSize, 0, "The graph is nodes free") print("Completed")
def test_e_size(self): counter = 0 g = DiGraph() for i in range(10000): g.add_node(i) for i in range(500): g.add_edge(i, (i + 1) * 10, i * 10 / 5) counter += 1 self.assertEqual(counter, g.e_size()) for i in range(100): g.remove_edge(i, (i + 1) * 10) counter -= 1 self.assertEqual(counter, g.e_size())
def load_from_json(self, file_name: str) -> bool: try: with open(file_name, "r") as file: data = json.load(file) self.__init__() self.myGraph = DiGraph() for n in data.get("Nodes"): self.myGraph.add_node(n.get("id"), n.get("pos")) for e in data.get("Edges"): self.myGraph.add_edge(e.get("src"), e.get("dest"), e.get("w")) return True except Exception as e: return False """
def test_remove_node(self): g = DiGraph() flag = True g.add_node(11) for i in range(10): g.add_node(i) g.add_edge(i, 11, 1) for i in range(10): flag = g.remove_node(i) if not flag: break self.assertEqual(flag, True)
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 bfs(self, g: DiGraph(), id1: int, has_family: dict) -> list: scc = [] queue = deque() v_list = g.get_all_v() u = v_list.get(id1) scanned, scanned_reverse = {}, {} queue.append(u) scc.append(u.key) has_family[u.key] = True scanned[u] = True while queue: u = queue.popleft() if u.key in g._edges.keys(): for key in g.all_out_edges_of_node(u.key).keys(): v = g.get_all_v().get(key) if v not in scanned: scanned[v] = True queue.append(v) queue.append(g.get_all_v().get(id1)) while queue: u = queue.popleft() if g.all_in_edges_of_node(u.key) is not None: for key in g.all_in_edges_of_node(u.key).keys(): v = g.get_all_v().get(key) if v not in scanned_reverse: scanned_reverse[v] = True queue.append(v) if v in scanned.keys() and key not in has_family.keys( ): scc.append(key) has_family[key] = True return sorted(list(dict.fromkeys(scc)))
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 setUp(self) -> None: """ build the central graph for all the tests """ self.graph = DiGraph() self.graph.add_node(1) self.graph.add_node(2) self.graph.add_node(3) self.graph.add_node(4) self.graph.add_node(5) self.graph.add_edge(1, 2, 600) self.graph.add_edge(2, 3, 600) self.graph.add_edge(3, 1, 600) self.graph.add_edge(3, 4, 600) self.graph.add_edge(4, 5, 600) self.graph.add_edge(5, 4, 600)
def test_empty_graph(self): graph = DiGraph.DiGraph() graph.remove_node(10) self.assertEqual(graph.get_mc(), 0) graph.add_edge(0, 8, 10) graph.add_edge(8, 8, 10) self.assertEqual(graph.get_mc(), 0) graph.remove_edge(3, 10)
def test_all_out_edges_of_node(self): g = DiGraph() my_dict = dict() for i in range(15): g.add_node(i) for i in range(9): g.add_edge(0, i + 2, i * 5) my_dict[i + 2] = i * 5 self.assertEqual(my_dict, g.all_out_edges_of_node(0))
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 test_v_size(self): counter = 0 g = DiGraph() for i in range(10000): g.add_node(i) counter += 1 for i in range(500): g.remove_node(i) counter -= 1 self.assertEqual(counter, g.v_size())
def createGraph(): graph = DiGraph.DiGraph() for i in range(0, 5): graph.add_node(i, (0, 0, 0)) graph.add_edge(0, 4, 4) graph.add_edge(0, 2, 2) graph.add_edge(1, 4, 12) graph.add_edge(4, 1, 1) graph.add_edge(2, 1, 10) graph.add_edge(3, 2, 8) return graph
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_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_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_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_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_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_get_all_v(self): g = DiGraph() my_dict = {} for i in range(10000): g.add_node(i) current_node = g.get_all_v()[i] my_dict[i] = current_node self.assertEqual(my_dict, g.get_all_v())
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 liniar_graph(self) -> DiGraph: """ This method create a linear 10 node graph :return: DiGraph """ graph = DiGraph() for i in range(0, 10): graph.add_node(i) for i in range(0, 10): graph.add_edge(i, i + 1, 1.0) return graph
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 connected_graph(self) -> DiGraph: """ This method create a linear 10 node graph :return: DiGraph """ graph = DiGraph() for i in range(0, 10): graph.add_node(i) for i in range(0, 10): for j in range(0,10): if i is not j: graph.add_edge(i, j, 1.0) return graph