Java中的暴力破解色数

发布于 2024-12-15 23:09:51 字数 1579 浏览 1 评论 0原文

这是对矩阵色数的强力尝试。从某种意义上说,它似乎有效,它为我提供了所需的正确颜色数量,但它不会显示 1,2,3,4,而是显示 1,2,3,6。由于某种原因,即使矩阵可能以较少的颜色成功,它仍然会失败并继续达到最大数量。是否存在持续失败的原因?

“n”是最大颜色数。 “v”是顶点,“m”是当前使用的颜色数。

伪代码: http://i42.tinypic.com/deoc2u.jpg

static int color(){ 
       int i;
       for(i = 1; i <= n; i++)
       {
               if(color(0,i)){
                       return i;}
       }
       return i;
}

    static boolean color(int v, int m) {

    if(v > n-1)
            return true;
    else
    {
            for(int i = 1; i <= m; i++)
            {
                    boolean match = false;
                    q[v] = i;

                            for(int j = 0; j < n; j++)
                            {
                                    if(input[v][j] == 1)                
                                    {
                                            if(q[v] == j+1)
                                                    match = true;
                                    }
                            }

                    if(match == false)
                    {
                            if(color(v+1,m)) 
                                    return true;
                    }
            }
            q[v] = 0;
            return false;
    }

}

示例输出:

文件名:file1< br> 输入
6
0 1 1 0 1 1
1 0 1 1 1 1
1 1 0 1 0 1
0 1 1 0 1 1
1 1 0 1 0 1
1 1 1 1 1 0
1 失败
2 失败
3 失败
4 失败
5 失败

颜色: 1 2 3 1 3 6

This is a brute force attempt at the chromatic number of a matrix. It seems to work in the sense that it gives me the correct number of colors required but instead of 1,2,3,4 it will display 1,2,3,6. For some reason even if the matrix is possibly successful with fewer colors it will still fail and keep going to the max number. Is there a reason why it continues to fail?

"n" is the max number of colors. "v" is the vertex, and "m" is the number of currently used colors.

pseudo code:
http://i42.tinypic.com/deoc2u.jpg

static int color(){ 
       int i;
       for(i = 1; i <= n; i++)
       {
               if(color(0,i)){
                       return i;}
       }
       return i;
}

    static boolean color(int v, int m) {

    if(v > n-1)
            return true;
    else
    {
            for(int i = 1; i <= m; i++)
            {
                    boolean match = false;
                    q[v] = i;

                            for(int j = 0; j < n; j++)
                            {
                                    if(input[v][j] == 1)                
                                    {
                                            if(q[v] == j+1)
                                                    match = true;
                                    }
                            }

                    if(match == false)
                    {
                            if(color(v+1,m)) 
                                    return true;
                    }
            }
            q[v] = 0;
            return false;
    }

}

sample output:

File Name: file1
Input
6
0 1 1 0 1 1
1 0 1 1 1 1
1 1 0 1 0 1
0 1 1 0 1 1
1 1 0 1 0 1
1 1 1 1 1 0
1 failed
2 failed
3 failed
4 failed
5 failed

Colors:
1 2 3 1 3 6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2024-12-22 23:09:51

嘎嘎,我该睡觉了!但我无法计算有多少次我把作业推迟到绝对最后一刻,所以我对你的截止日期感到同情。

这并不是真正的“蛮力”方法,因为这实际上归结为尝试每个可能的节点着色组合并检查哪些组合不冲突。您的方法是一种启发式方法,称为贪婪着色。它可以找到最佳结果,但也可能产生任意糟糕的解决方案。也就是说,我已经尝试手动检查您提供的示例输入(谢谢您,Microsoft Paint)并遵循该算法,从顶点 0 开始时,结果确实应该是 4。

所以这就是我认为您的可能有问题的地方代码。在下面的摘录中...

if(input[v][j] == 1)
{
    if(q[v] == j+1)
    match = true;
}

您似乎将当前顶点的颜色(无论如何,它只是i)与顶点索引进行比较,而不是与其他顶点的颜色进行比较。我认为您需要将内部测试更改为...

if(i == q[j])

或者,如果您愿意(不会产生影响)...

if(q[i] == q[j])

另外,您做了太多检查。可以为顶点 0 分配颜色 1。然后需要对照顶点 0 检查顶点 1 是否相邻。顶点 2 需要对照 0 和 1 检查它们是否相邻,依此类推。您正在检查尚未分配颜色的顶点。因此,不要将 j 限制为 n,而是将其限制为当前顶点 v

最后,使用像 if(match == false) 这样的构造相当混乱,而且没有必要。只需使用 if(!match) 即可。

希望这有帮助。如果您仍然遇到困难,而我碰巧及时看到了评论,我可以提供更多指导。

Gah, I should be asleep! But I can't count the number of times I've put off an assigment to the absolute last possible moment, so I'm feeling sympathetic regarding your deadline.

It's not really a "brute-force" approach, since that would actually come down to trying each and every possible node coloring combination and check which ones don't conflict. Your approach is a heuristic known as greedy coloring. It can find optimal results but can also yield an arbitrarily bad solution. That said, I've tried manually going through the sample input you've provided (thank you, Microsoft Paint) and following that algorithm the result should indeed be 4 when starting from vertex 0.

So here's what I believe might be wrong with your code. In the following extract...

if(input[v][j] == 1)
{
    if(q[v] == j+1)
    match = true;
}

You seem to be comparing the color of the current vertex (which will just be i anyway) to a vertex index, rather than the color of that other vertex. I think you'll need to alter the inner test to...

if(i == q[j])

or, if you wish (won't make a difference)...

if(q[i] == q[j])

Also, you're doing a bit too many checks. Vertex 0 can be assigned color 1. Vertex 1 then needs to be checked against vertex 0 if it's adjacent. Vertex 2 needs to be checked against 0 and 1 if they're adjacent, and so on. You're checking against vertices that couldn't have been assigned a color yet. So don't limit j to n but limit it to your current vertex v.

Finally, using a construct like if(match == false) is rather confusing and not necessary. Just use if(!match) instead.

Hope this helps. If you're still stuck and I happen to catch the comments in time, I can provide more pointers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文