def test_invalid_edge(self): G = nx.path_graph(4) assert not nx.is_matching(G, {(0, 3), (1, 2)}) assert not nx.is_matching(G, {(0, 4)}) G = nx.path_graph(4, create_using=nx.DiGraph) assert nx.is_matching(G, {(0, 1)}) assert not nx.is_matching(G, {(1, 0)})
def test_matching_bqm(self): bqm = dnx.matching_bqm(self.graph) # the ground states should be exactly the matchings of G sampleset = dimod.ExactSolver().sample(bqm) for sample, energy in sampleset.data(['sample', 'energy']): edges = [v for v, val in sample.items() if val > 0] self.assertEqual(nx.is_matching(self.graph, edges), energy == 0) self.assertTrue(energy == 0 or energy >= 1) # while we're at it, test deprecated is_matching with self.assertWarns(DeprecationWarning): self.assertEqual(nx.is_matching(self.graph, edges), dnx.is_matching(edges))
def check_component(component, structure): """ Check whether any maximum cardinality matching of the given connected component of the symmetrization graph can respect ground truth """ # Set the minimum observed distance of this component to be infinity minimum = float("inf") # Get the size of the maximum cardinality matching of this component max_matching_size = len( nx.max_weight_matching(component, maxcardinality=True)) # Iterate over all subsets of edges of the component that are the same # size as the maximum cardinality matching for matching in itertools.combinations(component.edges(), max_matching_size): # Check that this is in fact a matching if nx.is_matching(component, matching): # Use helper to compute the minimum length edge of this matching minimum = min(minimum, check_matching(matching, structure)) # Return the minimum observed distance for this component return minimum
def is_matching(G, Matching): """ Determines if given graph is matching to given matching params Returns Boolean """ if not G and not Matching: raise Exception("Please provide Graph and Matching params") return nx.is_matching(G, Matching)
def is_valid_matching(matching_graph: nx.Graph, solutions: List[MatchingSolution]) -> bool: for solution in solutions: if solution is None or not nx.is_matching(matching_graph, set(solution)): return False return True
def max_matchings(graph): """ Return an itertor over the maximum cardinality matchings of the graph """ matching_size = len(nx.max_weight_matching(graph, maxcardinality=True)) for matching in itertools.combinations(graph.edges(), matching_size): if nx.is_matching(graph, matching): yield matching
def test_respect_matching(self): """ Test that a maximum cardinality matching is activated """ active_graph = TestClusteringCSP.symgraph.active_graph() solution = TestClusteringCSP.formula.solve() active_edges = [(alpha, beta) for vtype, alpha, beta in solution if vtype == sat.Formula.ACT_VAR] # Iterate over the active complex components of the symgraph for cc in nx.connected_component_subgraphs(active_graph): edges = [(a, b) for a, b in active_edges if a in cc.nodes() and b in cc.nodes()] self.assertTrue(nx.is_matching(cc, edges))
def test_empty_matching(self): G = nx.path_graph(4) assert_true(nx.is_matching(G, set()))
def test_dict(self): G = nx.path_graph(4) assert_true(nx.is_matching(G, {0: 1, 1: 0, 2: 3, 3: 2}))
def test_invalid(self): G = nx.path_graph(4) assert_false(nx.is_matching(G, {(0, 1), (1, 2), (2, 3)}))
def test_valid(self): G = nx.path_graph(4) assert_true(nx.is_matching(G, {(0, 1), (2, 3)}))
def is_matching(G, s): return nx.is_matching(G, s)
def test_single_edge(self): G = nx.path_graph(4) assert_true(nx.is_matching(G, {(1, 2)}))
def test_edge_order(self): G = nx.path_graph(4) assert_true(nx.is_matching(G, {(0, 1), (2, 3)})) assert_true(nx.is_matching(G, {(1, 0), (2, 3)})) assert_true(nx.is_matching(G, {(0, 1), (3, 2)})) assert_true(nx.is_matching(G, {(1, 0), (3, 2)}))