gpt4 book ai didi

c++ - 实现 graph_coloring - m 着色问题的问题

转载 作者:太空狗 更新时间:2023-10-29 23:20:46 28 4
gpt4 key购买 nike

我必须编写 C++ 程序,它将确定我应该使用多少种颜色来为无向图着色。

此外,我必须使用“使用 C++ 伪代码的算法基础”一书中的算法来执行此操作。

问题描述:确定无向图中顶点可以着色的所有方式,只使用m种颜色,使得相邻顶点的颜色不同。

输入:正整数 n 和 m,以及包含 n 个顶点的无向图。该图由二维数组 W 表示,其行和列的索引从 1 到 n,其中 W [i] [j] 如果在第 i 个顶点和第 j 个顶点之间存在边则为真,否则为假.

输出图的所有可能着色,最多使用 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 vertexes, 4 edges

1 2
1 3 2 3 3 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;
}

最佳答案

你有一大堆问题,主要是在 promise 方面。要记住的主要事情是您只想比较已设置颜色的节点,而不是将任何节点与其自身进行比较。您也可以使用数组 promise 一个更浅的递归这一事实,并使用归纳推理来避免比较所有对。

剧透:

http://ideone.com/Lk0mg

关于c++ - 实现 graph_coloring - m 着色问题的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6180064/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com