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 test_remove_node(self): print("test_remove_node") graph = DiGraph() graph.add_node(0) self.assertTrue(len(graph.get_nodes()), 1) graph.remove_node(0) self.assertTrue(len(graph.get_nodes()), 0)
def test_remove_edge(self): graph = DiGraph() is_remove_edge = graph.remove_edge( 1, 2) # tried to remove edge that not exist in the graph self.assertFalse( is_remove_edge ) # is_remove_edge is false because tried to remove edge that not exist self.assertTrue(graph.e_size() == 0) graph.add_edge( 1, 2, 30 ) # tried to connect node1 to node2 and the nodes that not exist in the graph graph.add_node(1) is_remove_edge = graph.remove_edge( 1, 2) # tried to connect node1 to node2 and the node2 that not exist self.assertFalse( is_remove_edge ) # is_remove_edge is false because tried to remove edge that node2 not exist self.assertTrue(graph.e_size() == 0) graph.add_node(2) graph.add_edge(1, 2, 30) graph.add_edge(1, 2, 30) self.assertTrue(graph.e_size() == 1) graph.add_edge(2, 1, 30) self.assertTrue(graph.e_size() == 2) is_remove_edge = graph.remove_edge(1, 2) self.assertTrue(is_remove_edge) # remove edge 1->2 self.assertTrue(graph.e_size() == 1) is_remove_edge = graph.remove_edge( 1, 2) # tried remove edge that not exist self.assertFalse(is_remove_edge) self.assertTrue(graph.e_size() == 1) is_remove_edge = graph.remove_edge(2, 1) # remove edge 2->1 self.assertTrue(is_remove_edge) # remove edge 1->2 self.assertTrue(graph.e_size() == 0)
def test_shortest_path(self): g = DiGraph() for i in range(0, 5): g.add_node(i) g.add_edge(0, 1, 10) g.add_edge(1, 2, 10) g.add_edge(0, 2, 5) g.add_edge(2, 3, 10) g.add_edge(3, 4, 10) graph = GraphAlgo(g) (path, path_list) = (25, [0, 2, 3, 4]) (p, pl) = graph.shortest_path(0, 4) print(p, pl) self.assertEqual(path, p) self.assertEqual(path_list, pl) (path0, path_list0) = (float('inf'), []) (p0, pl0) = graph.shortest_path(4, 0) self.assertEqual(path0, p0) self.assertEqual(path_list0, pl0) (p0, pl0) = graph.shortest_path(7, 0) # nodes does not exist self.assertEqual(path0, p0) self.assertEqual(path_list0, pl0)
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 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 test_get_mc(self): """ verify that each change in the graph the mc increased at least by 1 """ g = DiGraph() self.assertEqual(g.get_mc(), 0) # there are no changes made in the graph g.add_node(1) self.assertEqual(g.get_mc(), 1) # added one node to the graph g.remove_node(1) self.assertEqual(g.get_mc(), 2) # the node has been removed g.add_node(1) g.add_node(2) self.assertEqual(g.get_mc(), 4) g.add_edge(1, 2, 1) self.assertEqual( g.get_mc(), 5) # after the edge was connected the mc should be increased by 1 g.remove_edge(1, 2) self.assertEqual(g.get_mc(), 6) self.assertFalse(g.remove_edge(1, 2)) # there is no edge between (1,2) self.assertEqual(g.get_mc(), 6) # the mc should stay 6 self.assertFalse(g.remove_node(3)) # 3 is not in the graph self.assertEqual(g.get_mc(), 6) # the mc should stay 7 g.add_edge(1, 2, 2) self.assertEqual(g.get_mc(), 7) g.add_edge(1, 2, 2) self.assertEqual(g.get_mc(), 7) g.add_edge(1, 2, 3) self.assertEqual( g.get_mc(), 7 ) # the weight of the edge has been update ,so the mc increased by 1
def test_add_node(self): graph = DiGraph() self.assertEqual(graph.v_size(), 0) self.assertTrue(graph.add_node(0)) self.assertEqual(graph.v_size(), 1) self.assertTrue(graph.add_node(1)) self.assertFalse(graph.add_node(-9))
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
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_save_and_load(self): file_path = "unittest.json" graph = DiGraph() for i in range(9): graph.add_node(i) graph.add_edge(0, 1, 2.3) graph.add_edge(0, 3, 1.2) graph.add_edge(0, 2, 1.0) graph.add_edge(1, 4, 4.5) graph.add_edge(1, 3, 4.9) graph.add_edge(1, 7, 8.5) graph.add_edge(2, 5, 6.1) graph.add_edge(3, 2, 1.34) graph.add_edge(4, 3, 4.5) graph.add_edge(4, 2, 3.7) graph.add_edge(4, 5, 0.73) graph.add_edge(4, 6, 4.9) graph.add_edge(5, 2, 6.1) graph.add_edge(6, 5, 1.34) graph.add_edge(6, 7, 2.9) graph.add_edge(7, 6, 2.9) graph.add_edge(7, 1, 8.5) graph.add_edge(7, 8, 2.35) graph.add_edge(7, 4, 5.0) graph.add_edge(8, 7, 4.5) graph_algo = GraphAlgo(graph) graph_algo.save_to_json(file_path) graph_algo = GraphAlgo() graph_algo.load_from_json(file_path) self.assertEqual(graph, graph_algo.get_graph()) graph.remove_edge(3, 2) self.assertNotEqual(graph, graph_algo.get_graph())
def test_shortest_path(self): g = DiGraph() for i in range(10): g.add_node(i) k = GraphAlgo(g) self.assertEqual((float('inf'), []), k.shortest_path(0, 9)) g.add_edge(0, 1, 2) g.add_edge(1, 2, 1) g.add_edge(1, 3, 6.2) g.add_edge(3, 2, 1) g.add_edge(0, 3, 3) g.add_edge(0, 8, 16) g.add_edge(0, 4, 14) g.add_edge(0, 5, 3) g.add_edge(5, 6, 15) g.add_edge(5, 7, 7) g.add_edge(4, 7, 2.5) g.add_edge(7, 9, 3) g.add_edge(7, 8, 4) g.add_edge(8, 1, 9) dis, l = k.shortest_path(0, 9) self.assertEqual(l, [0, 5, 7, 9]) self.assertEqual(dis, 13) g.remove_node(9) g.add_node(9) self.assertEqual((float('inf'), []), k.shortest_path(0, 9))
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 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_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 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 check0(): my_graph = DiGraph() for i in range(6): my_graph.add_node(i) my_graph.add_edge(0, 1, 3) my_graph.add_edge(1, 2, 1) my_graph.add_edge(2, 0, 20) my_graph.add_edge(1, 3, 5) my_graph.add_edge(1, 4, 7) my_graph.remove_edge(2, 0) num_of_edges = 4 num_of_nodes = 5 print(num_of_edges, my_graph.e_size) print(num_of_nodes, my_graph.v_size) my_graph.remove_edge(1, 2) num_of_edges = 3 print(num_of_edges, my_graph.e_size) my_graph.remove_node(2) num_of_nodes = 4 print(num_of_nodes, my_graph.v_size) print("graph:", my_graph.graph_vertices.keys()) print("sons:", my_graph.sons.keys()) print("fathers:", my_graph.fathers.keys()) my_graph.add_edge(0, 3, 4) print("sons:", my_graph.sons.keys()) print("fathers:", my_graph.fathers.keys()) my_graph.remove_edge(0, 1) print("remove edge (0,1)") print("sons:", my_graph.sons.keys()) print("fathers:", my_graph.fathers.keys()) print(my_graph.graph_vertices.keys())
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 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_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 test_remove_node_ESize(self): """ verify that the node has been removed and all the edges he was connected with also verify the ESize this test will be preformed over a simple graph looks like: 1◄--►3 | ▲ | / ▼ ▼ 2 """ g = DiGraph() # create a new graph g.add_node(1) # add two nodes to g g.add_node(2) g.add_node(3) g.add_edge(1, 2, 12) g.add_edge(1, 3, 13) g.add_edge(2, 3, 23) g.add_edge(3, 2, 32) g.add_edge(3, 1, 31) self.assertEqual(5, g.ESize) g.remove_node(3) self.assertEqual(1, g.ESize) self.assertEqual( 12, g.get_edge(1, 2).weight ) # verify that the only edge remaining is 1-->2 in weight of 12 self.assertEqual(None, g.get_edge(2, 1)) self.assertTrue(g.has_edge(1, 2)) self.assertFalse(g.has_edge(2, 1)) self.assertFalse(g.has_edge(3, 1)) self.assertFalse(g.has_edge(1, 3))
def generate_graph(self, v) -> DiGraph(): i = 0 g = DiGraph() while i < v: g.add_node(i) i += 1 return g
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 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
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_connected_component_a(self): g = DiGraph() k = GraphAlgo(g) for i in range(10): g.add_node(i) g.add_edge(3, 4, 1) g.add_edge(3, 7, 1) g.add_edge(7, 3, 1) g.add_edge(7, 5, 1) g.add_edge(5, 0, 1) g.add_edge(5, 6, 1) g.add_edge(4, 5, 1) g.add_edge(6, 4, 1) g.add_edge(6, 0, 1) g.add_edge(6, 2, 1) g.add_edge(0, 1, 1) g.add_edge(1, 2, 1) g.add_edge(2, 0, 1) l0 = k.connected_component(0) l1 = k.connected_component(1) l2 = k.connected_component(2) l3 = k.connected_component(3) l4 = k.connected_component(4) l5 = k.connected_component(5) l6 = k.connected_component(6) l7 = k.connected_component(7) self.assertEqual(l0, l1, l2) # one component self.assertEqual(l3, l7) # one component self.assertEqual(l4, l5, l6) # one component
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_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)
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