实现graph_coloring-m着色问题

如何解决实现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]的另一种颜色,但是它进入了下一个层次!!!!!!!!! ..我认为这就是问题所在     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-