FIG Coloring m

Description of the problem:

  Given an undirected graph, m different colors. So that the coloring of each of two vertices of G different colors each edge, if a minimum of m FIG colors in order to make FIG. 2 different colors of each vertices connected by edges, so that the number m for the number of color charts. Problems FIG m seeking a number of colors m may be referred to FIG optimization coloring.

2 algorithm design

  By adjacency matrix A represents a non-connected graph G to FIG = (V, E).

  If the connected edges is present, a [i] [j] = 1, or a [i] [j] = 0.

  Integers 1,2,3 .. . m is used to denote a full height of n + m is a tree.

  Each node in the i-th layer solution space tree has m sons, each corresponding to a son of one of the possible x [i] in the m coloration.

  The n + 1 layer is a leaf node.

 

In the algorithm Backtrack,

  When i> n, a search algorithm to the leaf node, obtain a new coloration schemes m, the current can find the program tree colored by 1 m.

  When i <= n, the current node Z is extended internal node in the solution space. The node has x [i] = 1,2,3 .. . m co m son nodes. For each of a son node of the current node Z of the extension, which checks the feasibility of the OK function, and the depth-first manner recursively subtree search feasible, infeasible, or cut subtree.

Algorithm Description:

 

#include <iostream>
using namespace std;
class Color{
    friend int mColoring(int,int,int* *);
private:
    bool Ok(int k);
    void Backtrack(int t);
    int n,
        m,
        * *a,
        * x;
    long sum;
};
bool Color::Ok(int k)
{
    for(int j=1;j<=n;j++)
        if((a[k][j] == 1)&&(x[j] == x[k]))
            return false;
    return true;
}
void Color::Backtrack(int t)
{
    if(t>n)
    {
        sum++;
        for(int i=1;i<=n;i++)
            cout<<x[i]<<" ";
        cout<<endl;
    }
    else
    {
        for(int i=1;i<=m;i++)
            x[t] = i;
        if(Ok(t))
            Backtrack(t+1);
        x[t] = 0;
    }
}
int mColoring(int n,int m,int * * a)
{
    Color X;
    X.n = n;
    X.m = m;
    X.a = a;
    X.sum = 0;
    int * p = new int [n+1];
    for(int i=0;i<=n;i++)
        p[i] = 0;
    X.x = p;

    X.Backtrack(2);

    delete [] p;
    return X.sum;
}
int main()
{
    int n,m;
    cout<<"请输入想要确定的m着色图中m的值:"<<endl;
    cin>>m;
    cout<<endl<<"共有n个结点,n的值为:"<<endl;
    cin>>n;
    int **arr = new int [n][n];
    for(int i=0;i<n;i++)
    {
        cout<<"请输入连接矩阵的第一行的数(0-1)"<<endl;
        for(int j=0;j<n;j++)
        {
            cin>>arr[i][j];
        }
    }
    mColoring(n,m,arr);
    return 0;
}

operation result:

--------------------Configuration: test102501 - Win32 Debug--------------------
Compiling...
test.cpp
E:\study file\ACM\test102501\test.cpp(63) : error C2540: non-constant expression as array bound
E:\study file\ACM\test102501\test.cpp(63) : error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int ** '
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

test.obj - 2 error(s), 0 warning(s)

Or an array of pointers will not pass parameters ... time to see, mark it

Reproduced in: https: //my.oschina.net/u/204616/blog/545461

Guess you like

Origin blog.csdn.net/weixin_33841503/article/details/91990162