def test_is_connexe(self): v1 = Vertex(1) v2 = Vertex(2) v3 = Vertex(3) v4 = Vertex(4) v5 = Vertex(5) g1 = Graph(1, True, [v1, v2, v3, v4]) g2 = Graph(2, True, [v1, v2, v3, v5]) g3 = Graph(3, True, [v1, v2, v5]) print(g1) result = Graph("res", False, [v1, v2, v3, v4, v5]) result._add_edge_(v1, v2) result._add_edge_(v2, v3) result._add_edge_(v3, v5) result._add_edge_(v4, v5) print(result) is_connexe = result.is_connexe(g1) print("is connexe : {}".format(is_connexe)) self.assertFalse(is_connexe) is_connexe = result.is_connexe(g2) print("is connexe : {}".format(is_connexe)) self.assertTrue(is_connexe) is_connexe = result.is_connexe(g3) print("is connexe : {}".format(is_connexe)) self.assertFalse(is_connexe)
def best_choice_to_remove(vertex: Vertex, graph) -> Vertex: """try to minimize the edges to reduce the delta Parameters ---------- vertex: Vertex the vertex to reduce graph: list the graph with the vertices Returns ------- list: list of vertices minimized or None if no solution is found """ adj = vertex.get_adjacents() vertex_to_test = vertex.highest_degree_adjacent(included=adj, same_link=True) while not vertex.is_other_path_available(vertex_to_test): adj.remove(vertex_to_test) if len(adj) < 1: return None vertex_to_test = vertex.highest_degree_adjacent(included=adj, same_link=True) return vertex_to_test
def reduction(v: Vertex, graph): """reduce the vertex edges to fit the degree Parameters ---------- v: Vertex the target vertex who need to be reduce graph: list[Vertex] pass the all the graph Returns ------- bool: False if the vertex could not be reduced, True otherwise """ highest = best_choice_to_remove(v, graph) if not highest: return False if highest.degree() < 2: return False v.remove(highest) highest.remove(v) return True
def convert(subcomplexes: list) -> list: """return the list of value into complete graphs Parameters ---------- subcomplexes: list the list of subcomplexes to build the graph Returns ------- list: list of vertices fully linked for each subcomplex """ sets = [] flyweight = {} link_count = 1 for sub in subcomplexes: vs = [] for v in sub: if not flyweight.get(v, None): flyweight[v] = Vertex(v).add_link(link_count) else: flyweight[v].add_link(link_count) vs.append(flyweight[v]) sets.append([v.append_all(vs) for v in vs]) link_count += 1 return sets
def _add_vertex_(self, v: Vertex, compute_weight: bool = False): if not v: return if not self._has_vertex_(v): self._vertices.append(Vertex(v._tag)) elif compute_weight: real_vertex = self._get_vertex_(v) if real_vertex: real_vertex._increment_weight()
def test_compute(self): v1 = Vertex(1) v2 = Vertex(2) v3 = Vertex(3) v4 = Vertex(4) v5 = Vertex(5) v6 = Vertex(6) v7 = Vertex(7) v8 = Vertex(8) v9 = Vertex(9) v10 = Vertex(10) g1 = Graph(1, True, [v4, v7, v6, v9, v1]) g2 = Graph(2, True, [v1, v5, v10, v7, v9]) g3 = Graph(3, True, [v3, v9, v4, v5, v7]) #g4 = Graph(4, True, [v4, v5, v6, v7]) #g5 = Graph(5, True, [v6, v8]) # print(g1) graph = compute(k=0, delta=3, sg=[], real_list=[g1, g2, g3]) print(verify_result(3, 0, graph))
def init(k: int, delta: int, sg: list, real_list: list = []): global sub_graphs global result global actual_delta global vertices_with_delta_max actual_delta = 0 vertices_with_delta_max = [] sub_graphs = [] if real_list: sub_graphs = real_list else: i = 1 if sg: for g in sg: list_vertices = [] for v in g: real_vertex = Vertex(v) list_vertices.append(real_vertex) real_graph = Graph("Subgraph", True, vertices=list_vertices) sub_graphs.append(real_graph) i += 1 big_graph = get_all_sub_graphs_in_one() result = Graph("Result") result._reset_()
def test_vertex_adjacents(self): v1 = Vertex(1) self.assertEqual(v1.degree(), 0, "Should be 0") v2 = Vertex(2, [v1]) v3 = Vertex(3, [v1, v2, v2]) self.assertEqual(v3.degree(), 2, "Should be 2") v4 = Vertex(4, [v1, v2, v3]) v1.set_adjacents([v1, v2, v3, v4]) self.assertEqual(v3.highest_degree_adjacent([v1, v2]), v1, "Should be v1")
def test_reduction(self): v1, v2, v3, v4 = Vertex(1), Vertex(2), Vertex(3), Vertex(4) v1.set_adjacents([v2, v3]) self.assertFalse(reduction(v1, [v2, v3]), "Should be of a good degree") self.assertEqual(v1.degree(), 2, "Should not have been reduced") self.assertEqual(v1.degree(), 2, "Should have been reduced")