Python uses the Backtracking Method subset tree template to solve the m coloring problem example, python backtracking

Source: Internet
Author: User

Python uses the Backtracking Method subset tree template to solve the m coloring problem example, python backtracking

This article describes how Python solves the problem of m coloring based on the subset tree template of the Backtracking Method. We will share this with you for your reference. The details are as follows:

Problem

Figure m-coloring Determination

Different colors of the undirected connected graph G and m are given. These colors are used to color the vertices of graph G. Each vertex has a color. Is there a coloring method for any two adjacent vertices of graph G to have different colors?

Figure m-coloring Optimization

If a graph requires at least m colors to make any two adjacent vertices have different colors, then m is the color number of the graph. The problem of finding the minimum color number m of an image is called the m-coloring optimization problem.

Analysis

The length of the solution is fixed, n. If x is a solution to this problem, x [I] indicates the colored number of the I node.

M colors can be considered as the state space of each node. Each node traverses all colors, pruning, and backtracking.

It is not hard to see that we can apply the Backtracking Method to the subset tree template.

Code

'''M coloring problem of the graph ''' # represent the number of nodes a, B, c, d, e = range (n) in the graph using an adjacent table) # node name graph = [{B, c, d}, {a, c, d, e}, {a, B, d}, {a, B, c, e },{ B, d}] m = 4 # m colors x = [0] * n # A solution (n yuan array, fixed length) Note: the subscript for resolving x is a, B, c, d, e !!! X = [] # A group of solutions # conflict Detection def conflict (k): global n, graph, x # Find the adjacent node nodes that have been colored before the k node = [node for node in range (k) if node in graph [k] if x [k] in [x [node] for node in nodes]: # return True return False # No conflict has been applied to adjacent nodes # image m coloring (all solutions) def dfs (k): # arrival (solution x) global n, m, graph, x, X if k = n: # The length of the solution exceeds print (x) # X. append (x [:]) else: for color in range (m): # color numbers (State Space) that can be painted to traverse node k ), all the same x [k] = color if not conflict (k): # pruning dfs (k + 1) # testing dfs (a) # Starting from node

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.