def test_issue_2127(self): """Test from issue 2127""" # Build the example DAG G = nx.DiGraph() G.add_edge("A", "C") G.add_edge("A", "B") G.add_edge("C", "E") G.add_edge("C", "D") G.add_edge("E", "G") G.add_edge("E", "F") G.add_edge("G", "I") G.add_edge("G", "H") tc = nx.transitive_closure(G) btc = nx.Graph() # Create a bipartite graph based on the transitive closure of G for v in tc.nodes(): btc.add_node((0, v)) btc.add_node((1, v)) for u, v in tc.edges(): btc.add_edge((0, u), (1, v)) top_nodes = {n for n in btc if n[0] == 0} matching = hopcroft_karp_matching(btc, top_nodes) vertex_cover = to_vertex_cover(btc, matching, top_nodes) independent_set = set(G) - {v for _, v in vertex_cover} assert_equal({'B', 'D', 'F', 'I', 'H'}, independent_set)
def test_issue_2127(self): """Test from issue 2127""" # Build the example DAG G = nx.DiGraph() G.add_edge("A", "C") G.add_edge("A", "B") G.add_edge("C", "E") G.add_edge("C", "D") G.add_edge("E", "G") G.add_edge("E", "F") G.add_edge("G", "I") G.add_edge("G", "H") tc = nx.transitive_closure(G) btc = nx.Graph() # Create a bipartite graph based on the transitive closure of G for v in tc.nodes(): btc.add_node((0, v)) btc.add_node((1, v)) for u, v in tc.edges(): btc.add_edge((0, u), (1, v)) top_nodes = set([n for n in btc if n[0] == 0]) matching = hopcroft_karp_matching(btc, top_nodes) vertex_cover = to_vertex_cover(btc, matching, top_nodes) independent_set = set(G) - set([v for _, v in vertex_cover]) assert_equal(set(['B', 'D', 'F', 'I', 'H']), independent_set)
def filtering(GG, u): # filter GG with constraint u and return if GG is still satisfiable v = set() for i in unitlist[u]: if not set(GG[i]): return False v |= set(GG[i]) G = GG.subgraph(unitlist[u] + list(v)) max_matching = hopcroft_karp_matching(G, unitlist[u]) max_matching = [(k, v) for k, v in max_matching.items()] v2x = [ p if len(p[1]) == 2 else p[::-1] for p in list( map( tuple, set(map(frozenset, G.edges)) - set(map(frozenset, max_matching)))) ] x2v = [p if len(p[0]) == 2 else p[::-1] for p in max_matching] assert {i for p in G.edges for i in p} == set(G.nodes) GM = nx.DiGraph(v2x + x2v) used = set(map(frozenset, max_matching)) for i in nx.strongly_connected_components(GM): used |= set(map(frozenset, GM.subgraph(i).edges)) m_free = list(set(G.nodes) - {i for p in max_matching for i in p}) if m_free: for i in nx.bfs_successors(GM, m_free[0]): for j in i[1]: used.add(frozenset({i[0], j})) unused = list(map(tuple, set(map(frozenset, G.edges)) - used)) GG.remove_edges_from(unused) affected_units = set() for e in unused: affected_units |= units[e[0] if len(e[0]) == 2 else e[1]] for unit in list(affected_units - {u}): if not filtering(GG, unit): return False return True
def main(): T = int(raw_input()) for i in xrange(T): # print '=== case', i N = int(raw_input()) l = [] left_nodes = set() right_nodes = set() edges = [] for n in xrange(N): topic = raw_input().split() topic[0] += '_left' topic[1] += '_right' left_nodes.add(topic[0]) right_nodes.add(topic[1]) edges.append(tuple(topic)) # print left_nodes, right_nodes, edges edges = set(edges) G = nx.Graph() G.add_nodes_from(left_nodes, bipartite=0) G.add_nodes_from(right_nodes, bipartite=1) G.add_edges_from(edges) matching = hopcroft_karp_matching(G) min_cover = set(matching.items()) uncovered_nodes = set(G) - {v for u, v in min_cover} for v in uncovered_nodes: u = next(iter(G[v])) min_cover.add((u, v)) min_cover.add((v, u)) min_cover = [(u, v) for u, v in min_cover if (u, v) in edges] # print min_cover output(i, N - len(min_cover))
def test_hopcroft_karp_matching_disconnected(self): match = hopcroft_karp_matching(self.disconnected_graph)
def test_hopcroft_karp_matching_simple(self): match = hopcroft_karp_matching(self.simple_graph) assert_equal(match, self.simple_solution)
def test_hopcroft_karp_matching(self): """Tests that the Hopcroft--Karp algorithm produces a maximum cardinality matching in a bipartite graph. """ self.check_match(hopcroft_karp_matching(self.graph, self.top_nodes))
def test_hopcroft_karp_matching(self): """Tests that the Hopcroft--Karp algorithm produces a maximum cardinality matching in a bipartite graph. """ self.check_match(hopcroft_karp_matching(self.graph))
def test_hopcroft_karp_matching_disconnected(self): with pytest.raises(nx.AmbiguousSolution): match = hopcroft_karp_matching(self.disconnected_graph)
def test_hopcroft_karp_matching_disconnected(self): match = hopcroft_karp_matching(self.disconnected_graph)
def test_hopcroft_karp_matching_simple(self): match = hopcroft_karp_matching(self.simple_graph) assert_equal(match, self.simple_solution)