实现graph_coloring-m着色问题时出现问题
Problem implementing the graph_coloring - m coloring problem
我必须编写C++程序,它将决定我应该使用多少颜色来给无向图着色。
此外,我必须使用"使用C++伪代码的算法基础"一书中的算法来完成这项工作。
问题描述:确定无向图中顶点的所有着色方式,只使用m种颜色,以便相邻顶点的颜色不相同。
输入:正整数n和m,以及包含n个顶点的无向图。图由二维数组W表示,该数组的行和列都从1到n进行索引,其中如果在第i个顶点和第j个顶点之间有边,则W[i][j]为true,否则为false。
输出:图形的所有可能的颜色,最多使用m种颜色,这样就不会有两个相邻的顶点是相同的颜色。每个着色的输出是从1到n索引的数组vcolor,其中vcolor[i]是分配给第i个顶点的颜色(1到m之间的整数)。
我们有算法:
void m_coloring (index i)
{
int color;
if (promising (i))
if (i == n)
cout << vcolor [1] through vcolor [n];
else
for (color = 1; color <= m; color++){ // Try every
vcolor [i + 1] = color; // color for
m_coloring (i + 1); // next vertex.
}
}
bool promising (index i)
{
index j;
bool switch;
switch = true;
j = 1;
while (j && switch){ // Check if an
if (W[i][j] && vcolor[i] == vcolor[j]) // adjacent vertex
switch = false; // is already
j++; // this color.
}
return switch;
}
最后注释:按照我们通常的约定,n、m、W和vcolor都不是这两个例程的输入。在算法的实现中,例程将在一个简单的过程中本地定义,该过程将n、m和W作为输入,并在本地定义vcolor。对m_coloring的顶级调用是m_coloring(0)
我开始编写自己的实现。首先我想说的是,我不是一个好的C++程序员,而且,我通常使用JS和PHP这两种弱类型语言,所以我相信有很多事情我可以做得更好。但这并不是主要问题。
问题是:上面的程序开始工作,我写了一个简单的图:
4个顶点,4个边
1 2
1 32 33 4
接下来,程序开始使用checkFor()(我计划在for()中对下一个数量的颜色使用它,但出于测试目的,我以静态方式使用它,所以我使用了4。
不幸的是,程序启动m_coloring(),下一次启动promising()和。。。这就是结局。我花了最后三个小时来找出我做错了什么,也许任何更有经验的程序员都能解释我应该做什么和/或做错了什么。。。
请帮忙,非常感谢。
我的程序代码:
#include <iostream>
using namespace std;
bool **W;
int n, m = 0;
int v, e = 0;
int x, y = 0;
int *vcolor;
bool promising (int i)
{
int j = 1;
bool switcher = true;
while (j && switcher)
{
if ( W[i][j] && vcolor[i] == vcolor[j] )
{
switcher = false;
}
j++;
}
return switcher;
}
void m_coloring (int i)
{
int color;
if ( promising (i) )
{
if (i == n)
{
cout << vcolor [1] << " through " << vcolor [n];
}
else
{
for (color = 1; color <= m; color++)
{
vcolor [i + 1] = color;
m_coloring(i + 1);
}
}
}
}
void initArrays()
{
for( int i = 0; i < n; i++ )
{
W[ i ] = new bool[ n ];
vcolor[ i ] = 0;
}
}
void fillW()
{
for( int i = 0; i < n; i++ )
{
for( int j = 0; j < n; j++ )
{
if( !W[i][j] )
{
W[i][j] = false;
}
}
}
}
void askForEdges()
{
cout << "How many edges? ";
cin >> e;
cout << endl << "Write edges with pattern: [vertex_x][space][vertex_y]:" << endl;
for( int i = 0; i < e; i++ )
{
cin >> x >> y;
W[x][y] = true;
W[y][x] = true;
}
}
void specialMatrixPrint()
{
cout << endl;
int i, j;
for( i = 0; i < n; i++ )
{
for( int j = 0; j < n; j++ )
{
cout << W[i][j] << " ";
}
cout << endl;
}
}
void showEdgesMatrix()
{
int i, j = 0;
cout << endl << " "; for( i = 1; i < n; i++ ) { cout << i << " "; } cout << endl;
cout << endl << " "; for( i = 1; i < n; i++ ) { cout << "# "; } cout << endl;
for( i = 1; i < n; i++ )
{
cout << i << " # ";
for( int j = 1; j < n; j++ )
{
if( W[i][j] == true ) { cout << "1 "; }
else { cout << "0 "; }
}
cout << endl;
}
}
void showVcolor()
{
cout << endl;
for( int i = 1; i < n; i++ )
{
cout << i << ": " << vcolor[ i ] << endl;
}
}
void checkFor( int i )
{
m = i;
m_coloring( 0 );
}
int main()
{
cout << "How many vertexes? " ;
cin >> n;
n += 1;
W = new bool *[ n ];
vcolor = new int[ n ];
initArrays();
askForEdges();
showEdgesMatrix();
checkFor( 4 );
showVcolor();
cin >> y;
return 0;
}
您遇到了一大堆问题,大部分都是有希望的问题。需要记住的主要一点是,您只想比较已经设置了颜色的节点,而不想将任何节点与自身进行比较。此外,您还可以使用数组承诺的一个递归较浅的事实,并使用归纳推理来避免比较所有对。
扰流板:
http://ideone.com/Lk0mg
算法中有一个错误。在函数promising
中,有一个很好的数组边界溢出。它们可能是指在while statement
的条件下类似j < n
或j <= n
的东西。正如所写的那样,这种情况毫无意义。
第一次用i=1, vcolor[i+1] = color
调用函数m_coloring
时,即vcolor[2]=color
和vcolor[1]
丢失。。因此,下一次通过承诺开关进行检查时,将始终返回值true。。
算法错误。。。。查看算法:
void m_coloring (index i)
{
int color;
if (promising (i))
if (i == n)
cout << vcolor [1] through vcolor [n];
else
for (color = 1; color <= m; color++){ // Try every
vcolor [i + 1] = color; // color for
m_coloring (i + 1); // next vertex.
}
/* HERE..................................*/
}
我们应该在算法的底部有另一个else语句来表示prodius,w[i]的另一种颜色,但它进入了下一个层次!!!!!!!!!。我认为这就是的问题
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- Boost Graph Library,修复节点大小
- 基于boost的程序的静态链接——zlib问题
- Boost Graph Library:如何使用depth_first_visit,ColorMap的问题