def test_vertex_color_basic(self): # all small enough for an exact solver to handle them reasonably G = dnx.chimera_graph(1, 2, 2) coloring = dnx.min_vertex_coloring(G, ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring)) G = nx.path_graph(5) coloring = dnx.min_vertex_coloring(G, ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring)) for __ in range(10): G = nx.gnp_random_graph(5, .5) coloring = dnx.min_vertex_coloring(G, ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring))
def test_vertex_color_no_edge_graph(self): """Graph with many nodes but no edges, should be caught before QUBO""" # this should get eliminated in software so be fast to run G = nx.Graph() G.add_nodes_from(range(100)) coloring = dnx.min_vertex_coloring(G, ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring))
def test_vertex_color_disconnected_graph(self): """One edge and one disconnected node""" G = nx.path_graph(2) G.add_node(3) coloring = dnx.min_vertex_coloring(G, ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring))
def test_4cycle(self): G = nx.cycle_graph('abcd') Q = dnx.vertex_color_qubo(G, 2) sampleset = dimod.ExactSolver().sample_qubo(Q) # check that the ground state is a valid coloring ground_energy = sampleset.first.energy colorings = [] for sample, en in sampleset.data(['sample', 'energy']): if en > ground_energy: break coloring = {} for (v, c), val in sample.items(): if val: coloring[v] = c self.assertTrue(dnx.is_vertex_coloring(G, coloring)) colorings.append(coloring) # there are two valid colorings self.assertEqual(len(colorings), 2) self.assertEqual(ground_energy, -len(G))
def test_4cycle_with_chord(self): G = nx.cycle_graph(4) G.add_edge(0, 2) # need 3 colors in this case coloring = dnx.vertex_color(G, 3, dimod.ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring))
def test_vertex_color_almost_complete(self): # this should get eliminated in software so be fast to run G = nx.complete_graph(10) mapping = dict(zip(G.nodes(), "abcdefghijklmnopqrstuvwxyz")) G = nx.relabel_nodes(G, mapping) n0, n1 = next(iter(G.edges())) G.remove_edge(n0, n1) coloring = dnx.min_vertex_coloring(G, ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring))
import networkx as nx import dwave_networkx as dnx from hybrid.reference.kerberos import KerberosSampler import matplotlib.pyplot as plt G = nx.read_adjlist('usa.adj', delimiter=',') coloring = dnx.min_vertex_coloring(G, sampler=KerberosSampler(), chromatic_ub=4, max_iter=10, convergence=3) set(coloring.values()) node_colors = [coloring.get(node) for node in G.nodes()] if dnx.is_vertex_coloring( G, coloring): # adjust the next line if using a different map nx.draw(G, pos=nx.shell_layout( G, nlist=[list(G.nodes)[x:x + 10] for x in range(0, 50, 10)] + [[list(G.nodes)[50]]]), with_labels=True, node_color=node_colors, node_size=400, cmap=plt.cm.rainbow) plt.show()
def test_vertex_color_odd_cycle_graph(self): """Graph that is an odd circle""" G = nx.cycle_graph(5) coloring = dnx.min_vertex_coloring(G, ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring))
def test_vertex_color_complete_graph(self): # this should get eliminated in software so be fast to run G = nx.complete_graph(101) coloring = dnx.min_vertex_coloring(G, ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring))
def test_vertex_color_disconnected_cycle_graph(self): G = nx.complete_graph(3) # odd 3-cycle G.add_node(4) # floating node coloring = dnx.min_vertex_coloring(G, ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring))
def test_5cycle(self): G = nx.cycle_graph(5) coloring = dnx.vertex_color(G, 3, dimod.ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring))
def test_5path(self): G = nx.path_graph(5) coloring = dnx.min_vertex_coloring(G, dimod.ExactSolver()) self.assertTrue(dnx.is_vertex_coloring(G, coloring)) self.assertEqual(len(set(coloring.values())), 2) # bipartite
# # 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 dwave_networkx as dnx import networkx as nx import dimod # Use basic simulated annealer sampler = dimod.SimulatedAnnealingSampler() # Set up a Networkx Graph G = nx.Graph() G.add_edges_from([(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (1, 4), (2, 4), (3, 4), (3, 5), (4, 5), (5, 2)]) # Get the minimum vertex coloring, which is known in this case to be of # length 6 candidate = dnx.min_vertex_coloring(G, sampler) if dnx.is_vertex_coloring(G, candidate) and len(candidate) == 6: print(candidate, " is a minimum vertex coloring") else: print(candidate, " is not a minimum vertex coloring")