def __call__(self, red: UGraph, blue: UGraph): f_weight = lambda v, u: self.stpg.graph.weight(v, u) union_g = UGraph() for v, u in red.gen_undirect_edges(): union_g.add_edge(v, u) for v, u in blue.gen_undirect_edges(): union_g.add_edge(v, u) queue = PriorityQueue() start = choice(tuple(self.stpg.terminals)) for u in union_g.adjacent_to(start): queue.push(f_weight(start, u), (start, u)) result = UGraph() while queue: start, end = queue.pop() if end not in result: result.add_edge(start, end) for w in union_g.adjacent_to(end): queue.push(f_weight(end, w), (end, w)) return result
def __call__(self, red: UGraph, blue: UGraph): assert isinstance( red, UGraph), f'red parent must be UGraph. Given {type(red)}' assert isinstance( blue, UGraph), f'blue parent must be UGraph. Given {type(blue)}' union_g = UGraph() for v, u in red.gen_undirect_edges(): union_g.add_edge(v, u) for v, u in blue.gen_undirect_edges(): union_g.add_edge(v, u) terminals = self.terminals.copy() done = set() result = UGraph() candidates_edges = set() vi = terminals.pop() done.add(vi) for u in union_g.adjacent_to(vi): candidates_edges.add((vi, u)) while candidates_edges and terminals: edge = sample(candidates_edges, k=1)[0] v, w = edge if w not in done: done.add(w) result.add_edge(v, w) terminals.discard(w) for u in union_g.adjacent_to(w): if u not in done: candidates_edges.add((w, u)) candidates_edges.discard((v, w)) return result
def __call__(self, parent_a, parent_b): assert isinstance( parent_a, EdgeSet ), f'parent_a has to be EdgeSet type. Give was {type(parent_a)}' assert isinstance( parent_b, EdgeSet ), f'parent_b has to be EdgeSet type. Give was {type(parent_b)}' stpg = self.stpg terminals = set(stpg.terminals) subgraph = UGraph() for edge in parent_a: u, v = edge subgraph.add_edge(u, v) for edge in parent_b: u, v = edge subgraph.add_edge(u, v) done = set() result = EdgeSet() v = terminals.pop() while terminals: done.add(v) adjacents = subgraph.adjacent_to(v, lazy=False) u = sample(adjacents, k=1)[0] if u not in done: result.add(v, u) terminals.discard(u) v = u return result
def __call__(self, red: UGraph, blue: UGraph): assert isinstance( red, UGraph), f'red parent must be UGraph. Given {type(red)}' assert isinstance( blue, UGraph), f'blue parent must be UGraph. Given {type(blue)}' terminals = self.terminals.copy() union_g = UGraph() for v, u in red.gen_undirect_edges(): union_g.add_edge(v, u) for v, u in blue.gen_undirect_edges(): union_g.add_edge(v, u) done = set() result = UGraph() v = terminals.pop() while terminals: done.add(v) adjacents = union_g.adjacent_to(v, lazy=False) u = sample(adjacents, k=1)[0] if u not in done: result.add_edge(v, u) terminals.discard(u) v = u return result
def __call__(self, red: UGraph, blue: UGraph): g_union = UGraph() for v, u in red.gen_undirect_edges(): g_union.add_edge(v, u) for v, u in blue.gen_undirect_edges(): g_union.add_edge(v, u) p_queue = PriorityQueue() vertices = list(g_union.vertices) v = choice(vertices) for u in g_union.adjacent_to(v): weight = self.f_weight(v, u) p_queue.push(weight, (v, u)) g_child = UGraph() done = set() done.add(v) while len(p_queue): v, u = p_queue.pop() if u not in done: g_child.add_edge(v, u) done.add(u) for w in g_union.adjacent_to(u): if w not in done: p_queue.push(self.f_weight(u, w), (u, w)) terminals = self.STPG.terminals prunne_leaves = deque([ v for v in g_child.vertices if ((g_child.degree(v) == 1) and (v not in terminals)) ]) while prunne_leaves: v = prunne_leaves.pop() prev = g_child.adjacent_to(v, lazy=False) g_child.remove_node(v) for w in prev: if g_child.degree(w) == 1 and w not in terminals: prunne_leaves.appendleft(w) return g_child
def __call__(self, parent_a, parent_b): assert isinstance( parent_a, EdgeSet ), f'parent_a has to be EdgeSet type. Give was {type(parent_a)}' assert isinstance( parent_b, EdgeSet ), f'parent_b has to be EdgeSet type. Give was {type(parent_b)}' stpg = self.stpg terminals = set(stpg.terminals) done = set() result = EdgeSet() subgraph = UGraph() for edge in parent_a: u, v = edge subgraph.add_edge(u, v) for edge in parent_b: u, v = edge subgraph.add_edge(u, v) vi = terminals.pop() done.add(vi) candidates_edges = set() for u in subgraph.adjacent_to(vi): candidates_edges.add((vi, u)) while candidates_edges and terminals: edge = sample(candidates_edges, k=1)[0] v, w = edge if w not in done: done.add(w) result.add(v, w) terminals.discard(w) for u in subgraph.adjacent_to(w): if u not in done: candidates_edges.add((w, u)) candidates_edges.discard((v, w)) return result
def __call__(self, treegraph: UGraph): terminals = self.terminals result = UGraph() for v, u in treegraph.gen_undirect_edges(): result.add_edge(v, u) leaves = deque([ v for v in result.vertices if (v not in terminals) and (result.degree(v) == 1) ]) while leaves: v = leaves.pop() for w in result.adjacent_to(v): if (w not in terminals) and (result.degree(w) == 2): leaves.appendleft(w) result.remove_node(v) return result