Beispiel #1
0
    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))
Beispiel #2
0
    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))
Beispiel #3
0
 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))
Beispiel #4
0
 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))
Beispiel #5
0
def solve(grid, use_dwave=False, max_iter=10, convergence=3):
    clues = make_clues(grid)
    G = make_sudoku_graph(clues)
    if use_dwave:
        coloring = dnx.min_vertex_coloring(G,
                                           sampler=KerberosSampler(),
                                           chromatic_lb=9,
                                           chromatic_ub=9,
                                           max_iter=max_iter,
                                           convergence=convergence)
    else:
        coloring = nx.coloring.greedy_color(G, 'saturation_largest_first')
    solution = decode(coloring, clues)
    disp(solution)
    print('Cost: %d\n' % cost(solution))
Beispiel #6
0
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()
Beispiel #7
0
 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))
Beispiel #8
0
 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))
Beispiel #9
0
 def test_dimod_response_vs_list(self):
     # should be able to handle either a dimod response or a list of dicts
     G = dnx.chimera_graph(1, 1, 3)
     coloring = dnx.min_vertex_coloring(G, ExactSolver())
     coloring = dnx.min_vertex_coloring(G, SimulatedAnnealingSampler())
Beispiel #10
0
 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))
Beispiel #11
0
 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
Beispiel #12
0
#
#        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")