def get_type_global(self, curr: Cut, change_time: int) -> Cut: final_cut = Cut() final_cut.set(curr.cl.copy(), curr.pl.copy()) self.random_replace_edge(final_cut, change_time) return final_cut
def get_type_s1(self, curr: Cut) -> Cut: if len(curr.cl) == 0: return Cut() final_cut = Cut() final_cut.cl = curr.cl.copy() self.random_one_less_edge(final_cut) count = 0 while self.support_el(final_cut.pl) >= self.support_count and \ count <= self.G[self.mini_idx].number_of_edges(): count += 1 final_cut.cl = curr.cl.copy() self.random_one_less_edge(final_cut) if count > self.G[self.mini_idx].number_of_edges(): return Cut() final_cut.cl = final_cut.pl.copy() grand_parents = self.one_less_edge(final_cut.cl) for gp in grand_parents: if self.support_el(gp) >= self.support_count: final_cut.pl = gp break if len(final_cut.pl) == 0: return Cut() return final_cut
def random_one_more_edge(self, cut: Cut) -> None: list_cand_edge = list(set(self.G[self.mini_idx].edges) - set(cut.pl)) while True: chose_edge = random.choice(list_cand_edge) cut.cl = cut.pl.copy() + [chose_edge] cut.cl = list(sorted(cut.cl)) p_graph = self.edgelist_to_graph(cut.cl) if not nx.classes.function.is_empty(p_graph) and nx.is_connected( p_graph): break
def get_type_m(self, curr: Cut) -> Cut: pall = self.get_type_pall(curr) if len(pall.pl) == 0: return Cut() final_cut = Cut() final_cut.pl = pall.pl.copy() self.random_one_more_edge(final_cut) count = 0 while self.support_el(final_cut.cl) < self.support_count and \ count <= self.G[self.mini_idx].number_of_edges(): count += 1 final_cut.pl = pall.pl.copy() self.random_one_more_edge(final_cut) if count > self.G[self.mini_idx].number_of_edges(): return Cut() cm_child = self.find_common_child(final_cut.cl, curr.cl) final_cut.pl = final_cut.cl final_cut.cl = cm_child return final_cut
def random_one_less_edge(self, cut: Cut) -> None: while True: remove_idx = np.random.randint(0, len(cut.cl)) cut.pl = cut.cl.copy() del cut.pl[remove_idx] p_graph = self.edgelist_to_graph(cut.pl) if not nx.classes.function.is_empty(p_graph) and nx.is_connected( p_graph): break
def get_type_pall(self, curr: Cut) -> Cut: if len(curr.cl) == 0: return Cut() final_cut = Cut() final_cut.cl = curr.cl.copy() self.random_one_less_edge(final_cut) count = 0 while self.support_el(final_cut.pl) < self.support_count and \ count <= self.G[self.mini_idx].number_of_edges(): count += 1 final_cut.cl = curr.cl.copy() self.random_one_less_edge(final_cut) if count > self.G[self.mini_idx].number_of_edges(): return Cut() return final_cut
def run(self, G: Dict[int, nx.Graph]) -> List[nx.Graph]: self.G = G self.support_count = int(self.support * len(self.G)) if self.sortrep: self.build_list_freq_edges() for i in range(len(self.G)): print("=== GRAPH %d ===" % i) self.mini_idx = i self.MF[i] = [] representative, represent_path = self.find_representative() if representative == None: continue initial_cut = Cut() if len(represent_path) > 0: initial_cut.set_cut(representative, represent_path[-1]) else: initial_cut.set_cut(representative) self.hashmap = [] if self.randwalk: self.expand_cut_random(initial_cut) else: self.expand_cut(initial_cut) final = [] line = [] for gid, list_el in self.MF.items(): self.mini_idx = gid for each_el in list_el: cm_sg = self.edgelist_to_graph(each_el) num_line = cm_sg.number_of_nodes() + cm_sg.number_of_edges() final.append(cm_sg) line.append(num_line) print( "Top 10:", sorted(range(len(line)), key=lambda i: line[i], reverse=True)[:10]) return final
def get_type_call(self, curr: Cut) -> Cut: pall = self.get_type_pall(curr) if len(pall.pl) == 0: return Cut() final_cut = Cut() final_cut.pl = pall.pl.copy() self.random_one_more_edge(final_cut) count = 0 while self.support_el(final_cut.cl) >= self.support_count and \ count <= self.G[self.mini_idx].number_of_edges(): count += 1 pall = self.get_type_pall(curr) final_cut.pl = pall.pl.copy() self.random_one_more_edge(final_cut) if count > self.G[self.mini_idx].number_of_edges(): return Cut() return final_cut
def random_replace_edge(self, cut: Cut, change_time: int) -> None: final_cut = Cut() final_cut.pl = cut.pl.copy() list_cand_edge = list(set(self.G[self.mini_idx].edges) - set(cut.pl)) for _ in range(change_time): step_cut = final_cut.copy() chose_edge = None p_graph = nx.Graph() while len(list_cand_edge) > 0: chose_edge = random.choice(list_cand_edge) final_cut.pl = step_cut.pl.copy() + [chose_edge] final_cut.pl = list(sorted(final_cut.pl)) p_graph = self.edgelist_to_graph(final_cut.pl) if nx.is_connected(p_graph): break if not nx.classes.function.is_empty( p_graph) and self.support_graph( p_graph) >= self.support_count: list_cand_edge.remove(chose_edge) else: final_cut.pl = step_cut.pl.copy() break final_cut.cl = final_cut.pl step_cl = final_cut.cl.copy() while len(list_cand_edge) > 0: chose_edge = random.choice(list_cand_edge) final_cut.cl = step_cl + [chose_edge] final_cut.cl = list(sorted(final_cut.cl)) p_graph = self.edgelist_to_graph(final_cut.cl) if nx.is_connected(p_graph): list_cand_edge.remove(chose_edge) step_cl = final_cut.cl.copy() if self.support_graph(p_graph) < self.support_count: break cut.cl = final_cut.cl cut.pl = final_cut.pl
def expand_cut(self, initial: Cut) -> None: print("Expanding cut...") self.cut_stack.append(initial) self.set_hash_key(initial) while len(self.cut_stack) > 0: curr_cut = self.cut_stack.pop(-1) if len(curr_cut.pl) > self.biggest_cut: self.biggest_cut = len(curr_cut.pl) self.num_explored_cut += 1 all_parents = self.one_less_edge(curr_cut.cl) for parent in all_parents: if self.support_el(parent) >= self.support_count: print(parent) self.MF[self.mini_idx].append(parent) all_children = self.one_more_edge(parent) for child in all_children: if self.support_el(child) < self.support_count: add_cut = Cut() add_cut.set(child, parent) if not self.get_hash_key(add_cut): self.set_hash_key(add_cut) self.cut_stack.append(add_cut) else: cm_child = self.find_common_child( curr_cut.cl, child) add_cut = Cut() add_cut.set(cm_child, child) if not self.get_hash_key(add_cut): self.set_hash_key(add_cut) self.cut_stack.append(add_cut) else: grand_parents = self.one_less_edge(parent) for gp in grand_parents: if self.support_el(gp) >= self.support_count: add_cut = Cut() add_cut.set(parent, gp) if not self.get_hash_key(add_cut): self.set_hash_key(add_cut) self.cut_stack.append(add_cut) break
def get_hash_key(self, cut: Cut) -> bool: hash_str = cut.get_hash_str() return hash_str in self.hashmap
def set_hash_key(self, cut: Cut) -> None: hash_str = cut.get_hash_str() if hash_str not in self.hashmap: self.hashmap.append(hash_str)