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 test_vertex_cover_issue_3306(self): G = nx.Graph() edges = [(0, 2), (1, 0), (1, 1), (1, 2), (2, 2)] G.add_edges_from([((i, "L"), (j, "R")) for i, j in edges]) matching = maximum_matching(G) vertex_cover = to_vertex_cover(G, matching) for u, v in G.edges(): assert u in vertex_cover or v in vertex_cover
def test_unorderable_nodes(self): a = object() b = object() c = object() d = object() e = object() G = nx.Graph([(a, d), (b, d), (b, e), (c, d)]) matching = maximum_matching(G) vertex_cover = to_vertex_cover(G, matching) for u, v in G.edges(): assert_true(u in vertex_cover or v in vertex_cover)
def test_vertex_cover_issue_2384(self): G = nx.Graph([(0, 3), (1, 3), (1, 4), (2, 3)]) matching = maximum_matching(G) vertex_cover = to_vertex_cover(G, matching) for u, v in G.edges(): assert_true(u in vertex_cover or v in vertex_cover)
def test_to_vertex_cover(self): """Test for converting a maximum matching to a minimum vertex cover.""" matching = maximum_matching(self.graph, self.top_nodes) vertex_cover = to_vertex_cover(self.graph, matching, self.top_nodes) self.check_vertex_cover(vertex_cover)
def test_to_vertex_cover(self): """Test for converting a maximum matching to a minimum vertex cover.""" matching = maximum_matching(self.graph) vertex_cover = to_vertex_cover(self.graph, matching) self.check_vertex_cover(vertex_cover)