def test_keys_only(self): graph = {'a': {1, 3}, 'c': {1, 3}, 'd': {3, 6}, 'h': {8}} hk = HopcroftKarp(graph) max_matching = hk.maximum_matching(keys_only=True) self.assertTrue( max_matching == { 'a': 1, 'c': 3, 'd': 6, 'h': 8 } or max_matching == { 'a': 3, 'c': 1, 'd': 6, 'h': 8 }, 'maximum matching is incorrect')
def pick(problems,solved2): chosen=[] if len(problems) == 0: return chosen graph = {} co=0 for typ,l,r in problems: allp = getall(typ,l,r,solved2) if len(allp) == 0: continue random.shuffle(allp) graph[co] = allp co=co+1 if len(graph) == 0: return [] hk = HopcroftKarp(graph) max_matching = hk.maximum_matching() for i in range(0,co): chosen.append(max_matching[i]) random.shuffle(chosen) return chosen
def __init__(self, graph): self.graph = graph self.left = set(graph.keys()) self.right = set() for value in graph.values(): self.right.update(value) for vertex in self.left: for neighbour in graph[vertex]: if neighbour not in graph: graph[neighbour] = set() graph[neighbour].add(vertex) else: graph[neighbour].add(vertex) hk = HopcroftKarp(graph) self.matching = hk.maximum_matching() self.E_w = set() self.E_0 = set() self.E_1 = set()
def test_mainExample(self): graph = {'a': {1, 3}, 'c': {1, 3}, 'd': {3, 6}, 'h': {8}} hk = HopcroftKarp(graph) max_matching = hk.maximum_matching() self.assertTrue( max_matching == { 1: 'a', 'a': 1, 3: 'c', 'c': 3, 6: 'd', 'd': 6, 8: 'h', 'h': 8 } or max_matching == { 3: 'a', 'a': 3, 1: 'c', 'c': 1, 6: 'd', 'd': 6, 8: 'h', 'h': 8 }, 'maximum matching is incorrect')
for neighbour in self.graph[vertex]: if (vertex in left_star and neighbour in right_star) or \ (vertex in left_plus and neighbour in right_plus): self.E_w.add((vertex, neighbour)) graph_prime = {} for vertex in left_unlabelled: graph_prime[vertex] = set([neighbour for neighbour in self.graph[vertex] if neighbour in right_unlabelled]) for vertex in right_unlabelled: graph_prime[vertex] = set([neighbour for neighbour in self.graph[vertex] if neighbour in left_unlabelled]) self.edge_part_perfect(graph_prime, left_unlabelled, right_unlabelled) return self.E_1, self.E_w, self.E_0 # g = {'a': {1, 3}, 'b': {1, 2, 4}, 'c': {1, 3}, 'd': {3, 5, 6}, 'e': {5}, 'f': {4, 7}, # 'g': {5}, 1: {'a', 'b', 'c'}, 2: {'b'}, 3: {'a', 'b', 'c'}, 4: {'b', 'f', 'e'}, # 5: {'d', 'e', 'g'}, 6: {'d'}, 7: {'f'}} # l = {'a', 'b', 'c', 'd', 'e', 'f', 'g'} # r = set(range(1, 8)) g = {'S1': {'a', 'b', 'c'}, 'S2': {'a', 'b', 'c'}, 'S3': {'a', 'b', 'c'}} C = CostaImproved(g) m = HopcroftKarp(g) maximum_matching = m.maximum_matching() E1, Ew, E0 = C.edge_partitioning() #print('Bipartite graph: ' + str(m)) print('Maximum Matching:' + str(maximum_matching)) print('one-persistent edges: ' + str(E1)) print('weakly-persistent edges: ' + str(Ew)) print('zero-persistent edges: ' + str(E0))