实现graph_coloring-m着色问题

问题描述

我必须编写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 3   2 3   3 4 接下来,程序开始使用checkFor()(我计划在for()处将其用于接下来的每种颜色,但是出于测试目的,我以静态方式使用它,因此我使用了4。 不幸的是,程序启动了m_coloring(),下一次启动了promise(),...到此结束。我花了最后三个小时来找出我做错了什么,也许任何更有经验的程序员都能够向我解释我应该做什么和/或我做错了什么... 请帮助,非常感谢。 我的程序代码:
#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     ,        算法中存在错误。在函数“ 2”中,有一个很好的数组边界溢出。在the5ѭ的情况下,它们可能表示meant3ѭ或
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]的另一种颜色,但是它进入了下一个层次!!!!!!!!! ..我认为这就是问题所在     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...