M coloring problem of graphs-the M coloring problem of the backtracking method of __ graphs

Source: Internet
Author: User

Problem Description:

Given a g= (V, E) and M different colors for a undirected connected graph, these colors are colored for each vertex of Fig g, and each vertex is a color. Is there a coloring method that makes the two vertices adjacent to G have different colors. This problem is the problem of the M-coloring determination of graphs. If a graph requires a minimum of M color in order to make each edge of the graph connect two vertices with different colors, the number m is called the color number of the graph. The problem of finding the chromatic number m of a graph is called the M-coloring optimization problem of graphs.

Algorithm Design:

Backtracking method: If the loading problem and the 0-1 backpack backtracking solution, this topic is easy to solve. The code is as follows:

Code:

#include <bits/stdc++.h> using namespace std;
int m;
int pointnum;
int edgenum;
int sum = 0;
int graph[100][100];
int x[100];
    void InPut () {int pos1, pos2;
    scanf ("%d%d", &pointnum, &m);
    scanf ("%d", &edgenum);
    memset (graph, 0, sizeof (graph));
        for (int i = 1; I <= edgenum ++i) {scanf ("%d%d", &AMP;POS1, &pos2);
    GRAPH[POS1][POS2] = graph[pos2][pos1] = 1;
            } bool IsOk (int i) {for (int j = 1; j < i; ++j) if (graph[i][j] = 1 && x[j] = = X[i])
    return false;
return true;
        } void BackTrack (int i) {if (i > Pointnum) {sum = 1;
        printf ("Method%d:", sum);
        for (int j = 1; j <= Pointnum; ++j) {printf ("%d", x[j]);
        printf ("\ n");
    Return
            else {for (int j = 1; j <= m; ++j) {x[i] = j;
            if (IsOk (i)) BackTrack (i + 1);
     X[i] = 0;   } void OutPut () {printf ("Total%d color scheme", sum);} int main () {InPut ();
    BackTrack (1);
OutPut (); }

Test Sample:

Input:

5 4
8
1 3
1 2
1 4
2 3
2 4
2 5
3 4

4 5

Output:

Method 1:1 2 3 4 1
Method 2:1 2 3 4 3
Method 3:1 2 4 3 1
Method 4:1 2 4 3 4
Method 5:1 3 2 4 1
Method 6:1 3 2 4 2
Method 7:1 3 4 2 1
Method 8:1 3 4 2 4
Method 9:1 4 2 3 1
Method 10:1 4 2 3 2
Method 11:1 4 3 2 1
Method 12:1 4 3 2 3
Method 13:2 1 3 4 2
Method 14:2 1 3 4 3
Method 15:2 1 4 3 2
Method 16:2 1 4 3 4
Method 17:2 3 1 4 1
Method 18:2 3 1 4 2
Method 19:2 3 4 1 2
Method 20:2 3 4 1 4
Method 21:2 4 1 3 1
Method 22:2 4 1 3 2
Method 23:2 4 3 1 2
Method 24:2 4 3 1 3
Method 25:3 1 2 4 2
Method 26:3 1 2 4 3
Method 27:3 1 4 2 3
Method 28:3 1 4 2 4
Method 29:3 2 1 4 1
Method 30:3 2 1 4 3
Method 31:3 2 4 1 3
Method 32:3 2 4 1 4
Method 33:3 4 1 2 1
Method 34:3 4 1 2 3
Method 35:3 4 2 1 2
Method 36:3 4 2 1 3
Method 37:4 1 2 3 2
Method 38:4 1 2 3 4
Method 39:4 1 3 2 3
Method 40:4 1 3 2 4
Method 41:4 2 1 3 1
Method 42:4 2 1 3 4
Method 43:4 2 3 1 3
Method 44:4 2 3 1 4
Method 45:4 3 1 2 1
Method 46:4 3 1 2 4
Method 47:4 3 2 1 2
Method 48:4 3 2 1 4

A total of 48 color painting scheme

Partial screenshot:


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.