回溯 图着色问题

来源:互联网 发布:中建三局西部投资知乎 编辑:程序博客网 时间:2024/05/01 23:42

图着色问题(Graph Coloring Problem, GCP),又称着色问题,是最著名的NP-完全问题之一。

给定一个无向图G=(V, E),其中V为顶点集合,E为边集合,图着色问题即为将V分为K个颜色组,每个组形成一个独立集,即其中没有相邻的顶点。其优化版本是希望获得最小的K值。

先考虑三着色问题。

思路:

1.对图中的每个顶点,选择三种颜色之一对其进行着色。

2.如果该着色合法,那么检测是否图中所有的顶点都已经着色,如果是,输出该着色方案;否则,选择另外一个顶点进行着色。

3.如果三种颜色都尝试过且没有成功,那么回溯到父节点。

图由Graph表示,定义如下:

struct Graph{        int **weight;//邻接矩阵,这里不需要权值,所以边全用1表示        size_t number_of_vertices;//顶点数        size_t number_of_edges;//边数        size_t *color;//保存每个顶点的着色};
递归版本:

void graph_coloring_recursive( Graph *thiz, size_t k, size_t num_of_color ){        if( k > thiz->number_of_vertices )                return;        int i = 1;        while( i <= num_of_color ){                thiz->color[k-1] = i;                if( if_valid( thiz, k, num_of_color ) )//合法                {                           if( k < thiz->number_of_vertices )//加上=后就不会调用graph_painting_print了。                                graph_coloring_recursive( thiz, k+1, num_of_color );                        else                                graph_painting_print( thiz );                }                   ++i;        }   }

非递归版本如下:

void graph_coloring( Graph *thiz, size_t num_of_color ){        int i = 1;        while( i >= 1 )//对每个顶点进行着色        {                while( thiz->color[i-1] < num_of_color )                {                        thiz->color[i-1] += 1;//初始时各顶点的color值都为0,表示没有进行着色.                        if( if_valid( thiz, i, num_of_color ) )//合法                        {                                if( i < thiz->number_of_vertices ) //部分解                                {                                    ++i;//下一个节点                                }else{//最后一个节点,为全部解,输出结果                                        graph_painting_print( thiz );                                }                        }                }                thiz->color[i-1] = 0;//此为回溯部分,将当前节点color值置为0,回到上一层节点。                --i;        }}

和八皇后的程序对比一下发现两个程序很像,当然都是回溯算法的例子,思路都一样。

程序文件下载:

原创粉丝点击