def test_connected_component(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], algo.connected_component(1)) self.assertEqual([5, 6, 7], algo.connected_component(6)) self.assertEqual([8], algo.connected_component(8)) self.assertEqual([], algo.connected_component(10)) algo = GraphAlgo() self.assertEqual([], algo.connected_component(10)) algo = GraphAlgo(None) self.assertEqual([], algo.connected_component(10))
def load_from_json(self, file_name: str): """ Loads a graph from a json file. @param file_name: The path to the json file @returns True if the loading was successful, False o.w. """ try: with open(file_name, "r") as file: my_dict = json.load(file) edges_list = my_dict["Edges"] nodes_list = my_dict["Nodes"] graph = DiGraph() for n in nodes_list: if "pos" in n: max_split = 2 pos = [ float(i) for i in n["pos"].split(",", max_split) ] graph.add_node(n["id"], (pos[0], pos[1], pos[2])) else: graph.add_node(n["id"]) for e in edges_list: graph.add_edge(e["src"], e["dest"], e["w"]) self.graph = graph return True except IOError as e: print(e) return False finally: file.close()
def test_all_out_edges_of_node(self): graph = DiGraph() tuple0 = (0, 30, 2) tuple1 = (50, 50, 50) node0 = NodeData(0, tuple0) # node: key=0 tuple=(0, 30, 2) node1 = NodeData(1, tuple0) # node: key=1 tuple=(0, 30, 2) node2 = NodeData(2, tuple1) # node: key=2 tuple=(50, 50, 50) node3 = NodeData(3, tuple1) # node: key=3 tuple=(50, 50, 50) graph.add_node(node0.get_key(), tuple0) # add node0 self.assertEqual({}, graph.all_out_edges_of_node( node0.get_key())) # check the list In empty graph.add_node(node1.get_key(), tuple0) # add node1 graph.add_node(node2.get_key(), tuple1) # add node2 graph.add_node(node3.get_key(), tuple1) # add node3 graph.add_edge(node1.get_key(), node0.get_key(), 10) # connect 1->0 graph.add_edge(node1.get_key(), node2.get_key(), 15) # connect 1->2 graph.add_edge(node1.get_key(), node3.get_key(), 20) # connect 1->2 ans_list_keys = [node0.get_key(), node2.get_key(), node3.get_key()] for i in graph.all_out_edges_of_node(node1.get_key()).keys(): self.assertIn(i, ans_list_keys) graph.remove_node(node2.get_key()) # remove node2 graph.remove_node(node3.get_key()) # remove node3 ans_list_keys = [node0.get_key()] for i in ans_list_keys: self.assertIn(i, graph.all_out_edges_of_node(node1.get_key()).keys())
def graph_generator(v_size: int, e_size: int) -> list: """ generate graph with v_size vertices and e_size edges randomly :param v_size: number of vertices :param e_size: number of edges :return: GraphAlgo with the given number of edges and vertices """ my_graph = DiGraph() g = nx.Graph() nx_graph = g.to_directed() v, e = 1, 1 while v <= v_size: x = random.uniform(0.1, 35) y = random.uniform(0.1, 35) position = (x, y, 0) my_graph.add_node(v, pos=position) nx_graph.add_node(v, pos=position) v += 1 while e <= e_size: r_src = random.randint(1, v_size) r_dest = random.randint(1, v_size) r_weight = random.uniform(0.1, 200.1) if not my_graph.has_edge(r_src, r_dest): my_graph.add_edge(r_src, r_dest, r_weight) nx_graph.add_edge(r_src, r_dest, weight=r_weight) e += 1 graph_algo = GraphAlgo(my_graph) return [graph_algo, nx_graph]
def load_from_json(self, file_name: str) -> bool: """ Loads a graph from a json file. @param file_name: The path to the json file @returns True if the loading was successful, False o.w. """ try: with open(file_name) as file: new_graph = DiGraph() json_file = json.load(file) for node in json_file["Nodes"]: key1 = node["id"] if "pos" not in node: new_graph.add_node(key1, None) else: pos = node["pos"] pos = pos.replace("(", "").replace(")", "") x, y, z = str.split(pos, ",") x = float(x) y = float(y) z = float(z) new_graph.add_node(key1, (x, y, z)) for edge in json_file["Edges"]: source = edge["src"] destination = edge["dest"] weight = edge["w"] new_graph.add_edge(source, destination, weight) self.graph = new_graph file.close() return True except Exception as exception: print(exception) return False
def test_connected_components(self): g = DiGraph() ga = GraphAlgo(g) assert [] == ga.connected_components() g.add_node(0) g.add_node(1) g.add_edge(0, 1, 5) g.add_edge(1, 0, 5) ga = GraphAlgo(g) assert [[0, 1]] == ga.connected_components() g.add_node(2) g.add_edge(0, 2, 1) print(ga.connected_components()) assert len(ga.connected_components()) == 2 g = self.graphForTest1() ga = GraphAlgo(g) print(ga.connected_components()) assert len(ga.connected_components()) == 4 g.add_edge(3, 0, 1) print(ga.connected_components()) assert len(ga.connected_components()) == 3 g.add_edge(5, 4, 1) print(ga.connected_components()) assert len(ga.connected_components()) == 2 g.add_edge(6, 7, 1) print(ga.connected_components()) assert len(ga.connected_components()) == 1
def test_save(self): # test saving the graph to a file g = DiGraph() for i in range(10): g.add_node(i) for i in range(5): g.add_edge(i, i + 1, i * 10 + 1) ga1 = GraphAlgo(g) # check if saving was successful self.assertTrue( ga1.save_to_json( "C:\\Users\\User\\PycharmProjects\\Ex3\\Data\\test.json")) # load a new graph from the saved file and compare graphs ga2 = GraphAlgo(DiGraph()) ga2.load_from_json( "C:\\Users\\User\\PycharmProjects\\Ex3\\Data\\test.json") self.assertEqual(repr(ga1), repr(ga2)) # test on a large graph ga1.load_from_json( "C:\\Users\\User\\PycharmProjects\\Ex3\\data\\G_100_800_1.json") self.assertTrue( ga1.save_to_json( "C:\\Users\\User\\PycharmProjects\\Ex3\\Data\\test.json")) ga2.load_from_json( "C:\\Users\\User\\PycharmProjects\\Ex3\\Data\\test.json") self.assertEqual(repr(ga1), repr(ga2))
def test_plot_graph(self): graph4 = DiGraph() g_algo = GraphAlgo(graph4) g_algo.load_from_json("../data/A0") g_algo.plot_graph() g_algo.load_from_json("../data/A1") g_algo.plot_graph() g_algo.load_from_json("../data/A2") g_algo.plot_graph() g_algo.load_from_json("../data/A3") g_algo.plot_graph() g_algo.load_from_json("../data/A4") g_algo.plot_graph() g_algo.load_from_json("../data/A5") g_algo.plot_graph() graph4 = DiGraph() g_algo3 = GraphAlgo(graph4) graph4.add_node(0) graph4.add_node(1) graph4.add_node(2) graph4.add_node(3) graph4.add_edge(1, 2, 11) graph4.add_edge(2, 1, 11) graph4.add_edge(2, 3, 5) graph4.add_edge(1, 0, 3) g_algo3.plot_graph()
def load_from_json(self, file_name: str) -> bool: try: json_file = open(file_name, "r") # open the file info = json_file.read() flag = True if info.find("pos") < 0: # check if the node have positions flag = False graph_dict = json.loads(info) # parse the json to a dictionary new_graph = DiGraph() nodes = graph_dict["Nodes"] for n in nodes: # for every node check if it has pos and add it if flag: str_pos = n["pos"] pos = tuple(map(float, str_pos.split(','))) new_graph.add_node(n["id"], pos) else: new_graph.add_node(n["id"]) edges = graph_dict["Edges"] # for every edge add the edge for e in edges: new_graph.add_edge(e["src"], e["dest"], e["w"]) self.graph = new_graph json_file.close() return True except IOError: # in case of failing to open the file return False
def load_from_json(self, file_name: str) -> bool: """ This method loads graph from json file. @param file_name: The path to the json file @returns True if the loading was successful, else returns False """ try: with open(file_name, "r") as file: loaded_graph = DiGraph() f = json.load(file) for node in f.get("Nodes"): id = node.get("id") pos = None if node.get("pos") is not None: list_pos = node.get("pos").split(",") x = float(list_pos[0]) y = float(list_pos[1]) z = float(list_pos[2]) pos = (x, y, z) loaded_graph.add_node(id, pos) for edge in f.get("Edges"): src = edge.get("src") dest = edge.get("dest") w = edge.get("w") loaded_graph.add_edge(src, dest, w) self.graph = loaded_graph except IOError as exp: print(exp) return False return True
def load_from_json(self, file_name: str) -> bool: """ Loads a graph from a json file. @param file_name: The path to the json file @returns True if the loading was successful, False o.w. """ load_graph = DiGraph() try: with open(file_name, "r") as file: dict_graph = json.load(file) for nodes in dict_graph["Nodes"]: try: pos = nodes["pos"] x, y, z = str.split(pos, ",") x = float(x) y = float(y) z = float(z) load_graph.add_node(nodes["id"], (x, y, z)) except Exception: load_graph.add_node(nodes["id"]) for edges in dict_graph["Edges"]: load_graph.add_edge(edges["src"], edges["dest"], edges["w"]) except IOError as e: print(e) return False self.__graph = load_graph return True
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 load_from_json(self, file_name: str) -> bool: """ Loads a graph from a json file. It deserialize it from a JSON file, by loading it from the given path. :param file_name: The path of the file :return: True if the loading was successful, False o.w. """ graph_dis = DiGraph() try: with open(file_name, mode='r') as my_file: json_str = my_file.read() graph_from_json = json.loads(json_str) for vertex in graph_from_json['Nodes']: pos = vertex.get('pos') if pos is not None: pos = tuple(map(float, vertex['pos'].split(','))) key = vertex['id'] graph_dis.add_node(key, pos) for edge in graph_from_json['Edges']: source = int(edge['src']) destination = int(edge['dest']) weight = float(edge['w']) graph_dis.add_edge(source, destination, weight) self.graph = graph_dis except IOError: return False if self.graph is not None: return True return False
def load_from_json(self, file_name: str) -> bool: """ Loads a graph from a json file. @param file_name: The path to the json file @returns True if the loading was successful, False o.w. """ graph = DiGraph() try: with open(file_name, "r") as file: my_d = json.load(file) except IOError as e: print(e) return False for i in my_d["Nodes"]: if "pos" not in i.keys(): graph.add_node(node_id=i["id"]) else: p = tuple(map(float, i["pos"].split(","))) graph.add_node(node_id=i["id"], pos=p) for edge in my_d["Edges"]: src = edge["src"] dest = edge["dest"] w = edge["w"] graph.add_edge(id1=src, id2=dest, weight=w) self.g = graph return True
def load_from_json(self, file_name: str) -> bool: """ Loads a graph from a json file. @param file_name: The path to the json file @returns True if the loading was successful, False o.w. """ try: with open(file_name, "r") as file: my_dict = json.load(file) except IOError as e: print(e) return False # go over the information in json and extract the information into the graph j_graph = DiGraph() for nodes in my_dict["Nodes"]: if "pos" in nodes: if ["pos"] is None: j_graph.add_node(nodes["id"]) else: pos = tuple(nodes["pos"].split(",")) j_graph.add_node(nodes["id"], pos) else: j_graph.add_node(nodes["id"]) for edges in my_dict["Edges"]: j_graph.add_edge(edges["src"], edges["dest"], edges["w"]) self.graph1 = j_graph return True
def load_from_json(self, file_name: str) -> bool: """ Loads a graph from a json file. @param file_name: The path to the json file @returns True if the loading was successful, False o.w. """ load_graph = DiGraph() try: with open(file_name, 'r') as file: load_file = json.load(file) for vertex in load_file["Nodes"]: load_graph.add_node(vertex["id"]) if 'pos' in load_file['Nodes']: if vertex['pos']: pos = tuple(map(float, vertex["pos"][1:-1].split(','))) load_graph.add_node(vertex["id"], pos) else: pos = (0, 0, 0) load_graph.add_node(vertex["id"], pos) for edge in load_file["Edges"]: load_graph.add_edge(edge["src"], edge["dest"], edge["w"]) self.graph = load_graph return True except Exception as ex: print("couldn't save to jason", ex) return False finally: file.close()
def load_from_json(self, file_name: str) -> bool: """ loads a graph from a json file. return if a grapg was successfuly loaded""" try: #try to read the json from the file with open(file_name, 'r') as file: json_dict = json.load(file) except: #if could'nt read from the file, return false return False new_graph = DiGraph() #make a new graph for json_node in json_dict.get( 'Nodes'): #get the list of nodes from the json if (json_node.get('pos') is not None): #check if a position was give new_graph.add_node( json_node['id'], #add a node with the given data tuple([float(x) for x in json_node['pos'].split(',')])) else: #if a position was not given new_graph.add_node( json_node['id']) #add a node with default position for json_edge in json_dict.get( 'Edges'): #get the list of edges from the json new_graph.add_edge( json_edge['src'], json_edge['dest'], json_edge['w']) #add an edge with the given data self.graph = new_graph #set the new grah return True
class GraphGenerator: def __init__(self, vertices, edges): self.graph = DiGraph() self.v = vertices self.e = edges self.mc = 0 self.Generate() def Generate(self) -> DiGraph: i = 0 for vertex in range(self.v): self.graph.add_node( vertex, (random.uniform(0, 100), random.uniform(0, 100), 0)) while self.graph.e < self.e: random_pair = (random.randrange(self.v), random.randrange(self.v), random.uniform(0, 40)) source = random_pair[0] destination = random_pair[1] weight = random_pair[2] self.graph.add_edge(source, destination, weight) if i % 2 == 0: self.graph.add_edge(destination, source, weight) i = i + 1 return self.graph
def test_graph_plot(self): """ in this test i will verify that the plot works for graphs with nodes that has position value as well as for nodes without. Returns ------- """ g = DiGraph() g.add_node(1, (2, 7, 0)) g.add_node(2, (7, 7, 0)) g.add_node(3, (7, 2, 0)) g.add_node(4, (2, 2, 0)) g.add_edge(1, 2, 1) g.add_edge(2, 3, 1) g.add_edge(3, 4, 1) g.add_edge(4, 1, 1) g.add_edge(3, 1, 1) g.add_edge(1, 3, 1) ga = GraphAlgo(g) ga.plot_graph() # plot in case of nodes with position g.add_node(5) g.add_node(6) g.add_node(7) g.add_edge(5, 6, 1) g.add_edge(6, 7, 1) g.add_edge(7, 5, 1) ga.plot_graph() # plot in case of nodes without position
def graph_creator_with_edges(v_size: int, e_size: int) -> GraphAlgo: """ generate graph with v_size vertices and e_size edges randomly :param v_size: number of vertices :param e_size: number of edges :return: GraphAlgo with the given number of edges and vertices """ t = DiGraph() v, e = 1, 1 while v <= v_size: x = random.uniform(0.1, 35) y = random.uniform(0.1, 35) position = (x, y, 0) t.add_node(v, pos=position) v += 1 while e <= e_size: r_src = random.randint(1, v_size) r_dest = random.randint(1, v_size) r_weight = random.uniform(0.1, 20) if not t.has_edge(r_src, r_dest): t.add_edge(r_src, r_dest, r_weight) e += 1 g = GraphAlgo(t) return g
def test_add_edge(self): g = self.graphCreator(20) assert 20 == g.v_size() g.add_edge(0, 1, 5) assert g.all_in_edges_of_node(1).get(0) == 5 g.add_edge(0, 1, 2) assert g.all_in_edges_of_node(1).get(0) == 5 g.add_edge(0, 2, 2) g.add_edge(0, 3, 7) g.add_edge(1, 2, 4.23) assert False == g.add_edge(1, 4, -15) assert False == g.add_edge(1, 2, 2) g = self.graphCreator1(20, 20) e = 20 if g.add_edge(4, 5, 15): e += 1 assert g.e_size() == e g.all_out_edges_of_node(4).get(5) == 15 g.all_in_edges_of_node(5).get(4) == 15 print("done") if g.add_edge(4, 6, 9.5): e += 1 assert g.e_size() == e assert g.all_out_edges_of_node(4).get(6) == 9.5 g.all_in_edges_of_node(6).get(4) == 9.5 print("done") g = DiGraph() g.add_node(0) g.add_edge(0, 0, 1) assert 0 == g.e_size() assert g.all_out_edges_of_node(0).get(0) == None
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 load_from_json(self, file_name: str) -> bool: """ This function loads a graph from a json file. @param file_name: The path to the json file @returns True if the loading was successful, or False o.w. """ new_graph = DiGraph() try: with open(file_name, "r") as file: json_data = file.read() my_dict = json.loads(json_data) Edges = my_dict["Edges"] Nodes = my_dict["Nodes"] for i in Nodes: id = i.get("id") if len(i) == 1: new_graph.add_node(id, None) else: pos_str = i.get("pos") x, y, z = pos_str.split(",") pos_f = (float(x), float(y), float(z)) new_graph.add_node(id, pos_f) for p in Edges: src = p.get("src") w = p.get("w") dest = p.get("dest") new_graph.add_edge(src, dest, w) self._graph = new_graph return True except Exception as e: print(e) return False
def load_from_json(self, file_name: str) -> bool: """ Loads a graph from a json file. @param file_name: The path to the json file @returns True if the loading was successful, False o.w. """ load_graph = DiGraph() try: with open(file_name, 'r') as f: dict_algo = json.load(f) nodes = dict_algo["Nodes"] if isinstance(nodes, list): for i in nodes: n = Node(**i) load_graph.add_node(n.id, n.pos) edges = dict_algo["Edges"] for i in edges: load_graph.add_edge(id1=i['src'], id2=i['dest'], weight=i['w']) elif isinstance(nodes, dict): for k, v in nodes.items(): n = Node(**v) load_graph.add_node(n.id, n.pos) edges = dict_algo["Edges"] for k, v in edges.items(): for i in v.keys(): load_graph.add_edge(int(k), int(i), float(v.get(i))) self.graph = load_graph except IOError as e: print(e) return False return True
def load_from_json(self, file_name: str) -> bool: loaded_graph = DiGraph() try: with open(file_name, 'r') as j_file: json_str = j_file.read() j_graph = json.loads(json_str) for node in j_graph['Nodes']: pos = node.get('pos') if pos is not None: pos = tuple(map(float, node['pos'].split(','))) key = node['id'] loaded_graph.add_node(key, pos) for edge in j_graph['Edges']: if edge['w'] is None: loaded_graph.add_edge(int(edge['src']), int(edge['dest']), 0.0) else: loaded_graph.add_edge(int(edge['src']), int(edge['dest']), float(edge['w'])) self.graph = loaded_graph except IOError: return False if self.graph is not None: return True return False
def test_path(self): graph = DiGraph() for i in range(1, 11): graph.add_node(i) graph.add_edge(1, 2, 5) graph.add_edge(1, 8, 4) graph.add_edge(9, 10, 0) graph.add_edge(5, 2, 3) graph.add_edge(1, 5, 7) graph.add_edge(2, 10, 10) algo = GraphAlgo() algo.graph = graph self.assertEqual((4, [1, 8]), algo.shortest_path(1, 8)) self.assertEqual((15, [1, 2, 10]), algo.shortest_path(1, 10)) self.assertEqual((float('inf'), []), algo.shortest_path(1, 9)) self.assertEqual((float('inf'), []), algo.shortest_path(1, 11)) self.assertEqual((5, [1, 2]), algo.shortest_path(1, 2)) self.assertTrue(graph.remove_edge(1, 2)) self.assertEqual((7, [1, 5]), algo.shortest_path(1, 5)) self.assertEqual((10, [1, 5, 2]), algo.shortest_path(1, 2)) self.assertEqual((20, [1, 5, 2, 10]), algo.shortest_path(1, 10)) self.assertEqual((0, [9, 10]), algo.shortest_path(9, 10)) self.assertTrue(graph.remove_node(2)) self.assertFalse(graph.add_edge(5, 10, -1)) self.assertTrue(graph.add_edge(5, 10, 1)) self.assertEqual((8, [1, 5, 10]), algo.shortest_path(1, 10))
def load_from_json(self, file_name: str) -> bool: load_graph = DiGraph() try: with open(file_name, "r") as file: my_dict = json.load(file) nodes = my_dict["Nodes"] edges = my_dict["Edges"] for n in nodes: node_key = n.get("id") pos = n.get("pos") if pos is None: load_graph.add_node(node_key, None) continue if isinstance(pos, str): x, y, z = pos.split(",") else: x, y, z = pos[0], pos[1], pos[2] pos = (float(x), float(y), float(z)) load_graph.add_node(node_key, pos) for e in edges: load_graph.add_edge(e.get("src"), e.get("dest"), e.get("w")) except IOError as e: print(e) return False self.graph = load_graph return True
def test_extremeCases(self): graph = DiGraph() 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_edge(1, 3, 6) graph.add_edge(3, 4, 10) graph.add_edge(4, 2, 5) graph.add_edge(2, 1, 10) graph.add_edge(2, 1, 10) graph.add_edge(2, 1, 10.9) algo = GraphAlgo(graph) self.assertEquals(11, graph.get_mc()) graph.remove_node(2) self.assertEquals(5, graph.v_size()) self.assertEquals(2, graph.e_size()) self.assertFalse(graph.remove_node(2)) self.assertEquals(16, algo.shortest_path(1, 4)[0]) self.assertEquals((float('inf'), []), algo.shortest_path(1, 2)) self.assertEquals(12, graph.get_mc())
def load_from_json(self, file_name: str) -> bool: fs: IO try: graph = DiGraph() fs = open(file_name, 'r') j = json.load(fs) for node in j['Nodes']: id: int try: id = node['id'] try: pos = node['pos'] pos_tuple = (float(pos.split(',')[0]), float(pos.split(',')[1]), float(pos.split(',')[2])) graph.add_node(id, pos_tuple) except: graph.add_node(id, None) except: return False for edge in j['Edges']: graph.add_edge(edge['src'], edge['dest'], edge['w']) self.__graph = graph fs.close() return True except: return False
def test_e_size(self): g0 = DiGraph() for i in range(0, 10): g0.add_node(i) for i in range(0, 10): g0.add_edge(i, 9 - i, 0.2) self.assertEqual(10, g0.e_size())
def design_graph_object(graph, G = None): """ Create the DiGraph object without creating a list param: ---------------- graph(dict) : Dictionary graph format, G(DiGraph) : DiGraph object (It doesn't belong to Networkx library) return: ---------------- G(DiGraph) : Object returning """ if (not G): G = DiGraph() for node in graph.keys(): if (node not in G.get_vertices()): G.add_node(node) for ng in graph[node]: if (ng not in G.get_vertices()): G.add_node(ng) G.add_edge(node,ng) return G