def test_delete_node(self): g1 = AdjMat.make(GRAPH_CASES["circle"]) assert g1.get_num_node() == 5 assert g1.get_num_edge() == 5 g1.del_node(1) assert g1.get_num_node() == 4 assert g1.get_num_edge() == 3 g1.del_node(2) assert g1.get_num_node() == 3 assert g1.get_num_edge() == 2 g1.del_node(3) assert g1.get_num_node() == 2 assert g1.get_num_edge() == 1 g1.del_node(4) assert g1.get_num_node() == 1 assert g1.get_num_edge() == 0 g1.del_node(5) assert g1.get_num_node() == 0 assert g1.get_num_edge() == 0 g1.del_node(2) assert g1.get_num_node() == 0 assert g1.get_num_edge() == 0
def test_get_edge(self): for label, lis in GRAPH_CASES.items(): g = AdjMat.make(lis) for u, v, w in lis: p = g.get_edge(u, v) assert (u, v, w) == p, \ f"error, test case: {label}. expected ({u}, {v}, {w}), got {p}"
def from_list(self, lis: List[Tuple]): if self.__type == PHY_ADJ_LIS: self.__storage = WeightedAdjLis.make(lis) elif self.__type == PHY_MAT: self.__storage = AdjMat.make(lis) else: raise GraphException(ERR_INVALID_STORAGE_TYPE.format(self.__type)) return self
def test_set_weight(self): g = AdjMat.make(GRAPH_CASES["4-complete"]) assert (2, 1, 1) == g.get_edge(2, 1) assert (1, 2, 1) == g.get_edge(1, 2) g.set_weight(1, 2, 10).set_weight(2, 1, 666) assert (2, 1, 666) == g.get_edge(2, 1) assert (1, 2, 10) == g.get_edge(1, 2) assert None == g.get_edge(666, 233) g.set_weight(666, 233, 888) assert None == g.get_edge(666, 233)
def test_delete_edge(self): """ "4-complete":[ (1, 2, 1), (1, 3, 1), (1, 4, 1), (2, 1, 1,),(2, 3, 1), (2, 4, 1), (3, 1, 1), (3, 2, 1), (3, 4, 1), (4, 1, 1), (4, 2, 1), (4, 3, 1), ], :return: """ g = AdjMat.make(GRAPH_CASES["4-complete"]) assert g.del_edge(1, 2) == (1, 2, 1) assert g.del_edge(2, 1) == (2, 1, 1) assert g.get_num_edge() == 10, f"error: {g.get_num_edge()}" assert g.get_num_node() == 4 assert g.del_edge(1, 2) == None
def test_from_lis(self): for label, lis in GRAPH_CASES.items(): node_set = set() for u, v, _ in lis: node_set.add(u) node_set.add(v) n = len(lis) g = AdjMat.make(lis) assert g.get_num_edge() == n assert g.get_num_node() == len(node_set) for u, v, w in lis: assert g.contains_node(u) assert g.contains_node(v) assert (u, v, w) == g.get_edge(u, v)