Back, graph theory - N color map coloring problem

1. Problem Description

If we each area on the map are degenerate to a point adjacent regions can link them, the map becomes a non-connected graph, we colored equivalent to the map to the undirected FIG. coloring each point, connection point is not required to have the same color. This is a typical problem of coloring m of FIG. FIG undirected given G m colors, to find all the different coloring schemes, adjacent regions of different colors. As in the following example:

     

The figure, a total of seven areas, namely ABCDEFG, we numbered 1-7, with each region represented a vertex adjacent area with a cable, the map is transformed into a undirected connected graph.

If three colors to the colored map, then the problem with each of the nodes has three color choices, the seven nodes are combined color number is a feasible solution, for example: {1,2 , 3,2,1,2,3}.

2. Algorithm Design

Restrictions:

Assumed that the current node is extended solution space t tree layer, then 1 ~ t-1-node status has been determined. Subsequently extended along extended first branch node, this time need is determined coloration t-th node. T-th node to the previous colored t-1 nodes species, wherein the color of the nodes connected to either side of the same, if the same color, then the node t can not use the color, changing a color attempt.

For example:

3 nodes have been colored in the previous nodes and No. 4 No. 1, No. 3 side node has connected, then the node 4 can not select a color and the color of the same number of nodes 1,3.

Extension node propagates along a second branch, the constraint is determined, and if satisfied, the process proceeds deeper search continues, and if not, then the node generates extended to cut, a color number attempt replaced. If you have tried all the color number is completed, the node becomes a knot point, to back up from its recent slipknot point, continue the search. When searching for the leaf nodes, find a coloring scheme, the search process until all the points have become far slipknot knot point.

3. Source Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXX=111;//设定边界
int map[MAXX][MAXX]; //邻接矩阵存储图
int x[MAXX]; //解分量,记录色号
int sum=0; //记录有多少种方案
int n; //顶点个数
int m;//边的个数
int color_nums; //颜色数量

void createmap() //创建邻接矩阵
{
    int u; //顶点1
    int v; //顶点2
    cout << "请输入边的个数m:"<< endl;
    cin >> m;
    memset(map,0,sizeof(map));
    cout << "请输入有边相连的两个顶点u和v:"<< endl;
    for (int i=1;i<=m;i++)
    {
        cin >> u>>v;
        map[u][v]=1;
        map[v][u]=1;
    }
}

bool OK(int t) //判断色号是否相同的函数
{
    for(int j=1;j<t;j++) //判断现在扩展点t和前面t-1个顶点有没有相连的
    {
        if(map[t][j]) //如果相连
        {
            if(x[j]==x[t]) //且颜色一样
            {
                return false; //返回false,代表需要换个色号尝试
            }
        }
    }
    return true; //如果色号不一样就是true
}

void backtrack(int t) //回溯、递推函数
{
    if(t>n) //到达叶子节点
    {
        sum++; //方案个数
        cout << "第"<< sum << "种方案"<< endl;
        for (int i=1;i<=n;i++)
        {
            cout << x[i] << "    ";
        }
        cout << endl;
    }
    else
    {
        for (int i=1;i<=color_nums;i++) //尝试别的色号
        {
            x[t]=i;  //记录色号
            if(OK(t)) //如果色号没有撞
            {
                backtrack(t+1); //向下递推
            }
        }
    }
}


int main()
{
    cout << "请输入结点个数n:"<< endl;
    cin >> n;
    cout << "请输入颜色的数量:"<< endl;
    cin >> color_nums;
    cout << "请输入用邻接矩阵存储的图:"<< endl;
    createmap();
    backtrack(1);
    return 0;
}

 

4. Test results

Published 57 original articles · won praise 9 · views 3609

Guess you like

Origin blog.csdn.net/Jayphone17/article/details/102963554