def test_min_maximal_matching_bqm(self): bqm = dnx.min_maximal_matching_bqm(self.graph) if len(self.graph) == 0: self.assertEqual(len(bqm.linear), 0) return # the ground states should be exactly the minimum maximal matchings of # G sampleset = dimod.ExactSolver().sample(bqm) # we'd like to use sampleset.lowest() but it didn't exist in dimod # 0.8.0 ground_energy = sampleset.first.energy cardinalities = set() for sample, energy in sampleset.data(['sample', 'energy']): if energy > ground_energy: continue edges = set(v for v, val in sample.items() if val > 0) self.assertTrue(nx.is_maximal_matching(self.graph, edges)) cardinalities.add(len(edges)) # all ground have the same cardinality (or it's empty) self.assertEqual(len(cardinalities), 1) cardinality, = cardinalities # everything that's not ground has a higher energy for sample, energy in sampleset.data(['sample', 'energy']): edges = set(v for v, val in sample.items() if val > 0) if energy != sampleset.first.energy: if nx.is_maximal_matching(self.graph, edges): self.assertGreater(len(edges), cardinality)
def test_maximal_matching_bqm(self): bqm = dnx.maximal_matching_bqm(self.graph) # the ground states should be exactly the maximal matchings of G sampleset = dimod.ExactSolver().sample(bqm) for sample, energy in sampleset.data(['sample', 'energy']): edges = set(v for v, val in sample.items() if val > 0) self.assertEqual(nx.is_maximal_matching(self.graph, edges), energy == 0) self.assertGreaterEqual(energy, 0) # while we're at it, test deprecated is_maximal_matching with self.assertWarns(DeprecationWarning): self.assertEqual(nx.is_maximal_matching(self.graph, edges), dnx.is_maximal_matching(self.graph, edges))
def test_self_loops(self): # Create the path graph with two self-loops. G = nx.path_graph(3) G.add_edges_from([(0, 0), (1, 1)]) matching = nx.maximal_matching(G) assert_equal(len(matching), 1) # The matching should never include self-loops. assert_false(any(u == v for u, v in matching)) assert_true(nx.is_maximal_matching(G, matching))
def test_ordering(self): """Tests that a maximal matching is computed correctly regardless of the order in which nodes are added to the graph. """ for nodes in permutations(range(3)): G = nx.Graph() G.add_nodes_from(nodes) G.add_edges_from([(0, 1), (0, 2)]) matching = nx.maximal_matching(G) assert_equal(len(matching), 1) assert_true(nx.is_maximal_matching(G, matching))
def test_not_matching(self): G = nx.path_graph(4) assert_false(nx.is_maximal_matching(G, set([(0, 1), (1, 2), (2, 3)])))
def test_single_edge_matching(self): # In the star graph, any maximal matching has just one edge. G = nx.star_graph(5) matching = nx.maximal_matching(G) assert_equal(1, len(matching)) assert_true(nx.is_maximal_matching(G, matching))
def test_dict(self): G = nx.path_graph(4) assert_true(nx.is_maximal_matching(G, {0: 1, 1: 0, 2: 3, 3: 2}))
def test_not_maximal(self): G = nx.path_graph(4) assert_false(nx.is_maximal_matching(G, {(0, 1)}))
def test_valid_matching(self): edges = [(1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (3, 6), (5, 6)] G = nx.Graph(edges) matching = nx.maximal_matching(G) assert_true(nx.is_maximal_matching(G, matching))
def test_valid(self): G = nx.path_graph(4) assert_true(nx.is_maximal_matching(G, {(0, 1), (2, 3)}))
def is_matching(self, plays): """ check if the pulled edges form a matching in the given bipartite graph """ match = self.plays_to_edges(plays) return nx.is_maximal_matching(self.graph, match) # TODO
def test_not_matching(self): G = nx.path_graph(4) assert not nx.is_maximal_matching(G, {(0, 1), (1, 2), (2, 3)})
def test_min_maximal_matching(self): matching = dnx.min_maximal_matching(self.graph, dimod.ExactSolver()) self.assertTrue(nx.is_maximal_matching(self.graph, matching))