def test_path3(self): G = nx.path_graph(3) self.assertTrue(dnx.is_independent_set(G, [0])) self.assertTrue(dnx.is_independent_set(G, [0, 2])) self.assertTrue(dnx.is_independent_set(G, [])) self.assertFalse(dnx.is_independent_set(G, [0, 1]))
def test_maximum_independent_set_basic(self): """Runs the function on some small and simple graphs, just to make sure it works in basic functionality. """ G = dnx.chimera_graph(1, 2, 2) indep_set = dnx.maximum_independent_set(G, dimod.ExactSolver()) self.assertTrue(dnx.is_independent_set(G, indep_set)) G = nx.path_graph(5) indep_set = dnx.maximum_independent_set(G, dimod.ExactSolver()) self.assertTrue(dnx.is_independent_set(G, indep_set))
# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # ================================================================================================ from __future__ import print_function import networkx as nx import dwave_networkx as dnx import dimod # Use basic simulated annealer sampler = dimod.SimulatedAnnealingSampler() # Set up a Networkx Graph G = nx.Graph() G.add_edges_from([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (4, 5), (4, 6), (5, 6), (6, 7)]) # Find the maximum independent set, which is known in this case to be of length 3 candidate = dnx.maximum_independent_set(G, sampler) if dnx.is_independent_set(G, candidate) and len(candidate) == 3: print(candidate, " is a maximum independent set") else: print(candidate, " is not a minimum vertex coloring")
def test_K2(self): G = nx.complete_graph(2) self.assertTrue(dnx.is_independent_set(G, [0])) self.assertTrue(dnx.is_independent_set(G, [])) self.assertFalse(dnx.is_independent_set(G, [0, 1]))
def test_K1(self): G = nx.complete_graph(1) self.assertTrue(dnx.is_independent_set(G, [0])) self.assertTrue(dnx.is_independent_set(G, []))
def test_empty(self): G = nx.Graph() self.assertTrue(dnx.is_independent_set(G, []))
def set_independence_check(self, G, indep_set): """Check that the given set of nodes are in fact nodes in the graph and independent of eachother (that is there are no edges between them""" self.assertTrue(dnx.is_independent_set(G, indep_set))
maximum_clique_subgraph = gv.Graph() for i in range(0, len(maximum_clique_list)): maximum_clique_subgraph.node(maximum_clique_list[i]) for j in range(i + 1, len(maximum_clique_list)): if i != j: maximum_clique_subgraph.edge(maximum_clique_list[i], maximum_clique_list[j]) # maximum_clique_subgraph.view(filename='maximum_clique_subgraph') print("f", file=main) independent_set_graph = gv.Graph() maximum_independent_set = [ 'AD', 'CZ', 'EE', 'MK', 'HU', 'SM', 'NO', 'LU', 'MC', 'RO', 'LT', 'BA', 'DK', 'ME', 'NL', 'CH', 'PT', 'VA', 'TR' ] print("Independent set:", maximum_independent_set, file=main) print(dnx.is_independent_set(Main_graph, maximum_independent_set), file=main) color_the_vertexes(independent_set_graph, maximum_independent_set) # independent_set_graph.view(filename='independent_set', quiet_view=True) print("maximum_independent_set.size =", len(maximum_independent_set), file=main) print("g", file=main) maximum_matching = nx.max_weight_matching(Main_graph) maximum_matching_graph = gv.Graph() print("Maximum matching:", maximum_matching, file=main) print("maximum_matching.size =", len(maximum_matching), file=main) color_the_edges(maximum_matching_graph, maximum_matching) # maximum_matching_graph.view(filename='maximum_matching', quiet_view=True) print("h", file=main) part = [] minimum_vertex_cover_graph = gv.Graph()