java - 四色图定理递归回溯算法

标签 java algorithm recursion backtracking recursive-backtracking

我编写了以下程序以递归地实现四色 map 定理(简而言之,任何 map 都可以只用 4 种颜色着色,而没有任何相邻区域是相同的颜色)。一切都编译,但我的输出给我错误的数据(-1 代表每个区域的颜色,而不是现在的值 0-3)。我最大的问题是为什么输出不正确。

对于那些想知道的人,输入文件是一个邻接矩阵,如下所示:

0 1 0 1  
1 0 1 0  
0 1 0 1  
1 0 1 0  

这是我的代码:

public class FourColorMapThm 
{
    public static boolean acceptable(int[] map, int[][] matrix, int r, int c)
    {
        for(int i = 0; i < map.length; i++)
        {
            if(matrix[r][i] == 1)
            {
                if(map[i] == c) return false;
            }
        }
        return true;
    }

    public static boolean colorTheMap(int[] map, int[][] matrix, int r, int c)
    {
        boolean q = false;
        int i = 0;

        if(!acceptable(map, matrix, r, i)) return false;

        map[r] = i;
        boolean done = true;

        for(int j = 0; j < map.length; j++)
        {
            if(map[j] == -1)
            {
                done = false;
            }
        }
        if(done) return true;

        do
        {
            i++;
            q = colorTheMap(map, matrix, r+1, i);
            if(q) return true;
        }
        while(i <= 3);

        map[r] = -1;
        return false;
    }

    public static void main(String[] args) throws IOException
    {
        Scanner in = new Scanner(System.in);
        String snumRegions, fileName;
        int numRegions;

        System.out.print("Enter the number of regions: ");
        snumRegions = in.nextLine();
        numRegions = Integer.parseInt(snumRegions);

        System.out.print("\nEnter filename for adjacency matrix: ");
        fileName = in.nextLine();
        in.close();

        Scanner inFile = new Scanner(new FileReader(fileName));
        PrintWriter outFile = new PrintWriter("coloredMap.txt");
        int[][] matrix = new int[numRegions][numRegions];
        int[] map = new int[numRegions];

        //initializing matrix from file
        for(int row = 0; row < matrix.length; row++)
        {
            for(int col = 0; col < matrix.length; col++)
            {
                matrix[row][col] = inFile.nextInt();
            }
        }
        inFile.close();

        //initialize map vals to -1
        for(int i = 0; i < map.length; i++)
        {
                map[i] = -1;
        }

        colorTheMap(map, matrix, 0, 0);

        outFile.println("Region\t\tColor");
        for(int i = 0; i < map.length; i++)
        {
            outFile.println(i+1 + "\t\t" + map[i]);
        }
        outFile.close();
    }
}

最佳答案

您没有在 colorTheMap 中使用 c,而您可能想要这样做。

您获得包含所有条目 -1map 的原因是:

    int i = 0;
    if(!acceptable(map, matrix, r, i)) return false;
    map[r] = i;
    .
    .
    .
    do
    {
        i++;
        q = colorTheMap(map, matrix, r+1, i);
        if(q) return true;
    }
    while(i <= 3);
    map[r] = -1;

colorTheMap(map, matrix, r+1, i) 的每次调用都会在命中时返回 false

    int i = 0;
    if(!acceptable(map, matrix, r, i)) return false;

如果它的任何邻居已经用 0 着色(因为你从不使用 c,你总是将 0 分配给 map[r] in map[r] = i; if you reach the line), 所以在返回之后,我们在赋值之前立即返回 false r 的任何颜色。我假设您的输入图是连接的,因此任何 colorTheMap(map, matrix, r, c) 调用都会找到 r 的邻居,其颜色为 0 并且甚至不将 map[r] 设置为任何其他值,因为 if(!acceptable(map, matrix, r, i)) return false; 将返回立即,或者它只接收 q = false in

的赋值
    do
    {
        i++;
        q = colorTheMap(map, matrix, r+1, i);
        if(q) return true;
    }
    while(i <= 3);

并取消循环后面 map[r] = -1; 行中的着色。


另一个注意事项:我假设您正在尝试实现贪婪着色,但这不是最佳选择,即您可能会以这种方式结束无色区域。四色问题比简单的“只要给每样东西都涂上一种颜色,它的邻居都没有被分配,你就很好”要复杂得多,否则不需要一个多世纪的时间来证明四种颜色足以出现。 Here看起来像是需要二次时间的正确算法的概述(引用以防链接丢失:Neil Robertson、Daniel P. Sanders、Paul Seymour 和 Robin Thomas。1996 年。高效四色平面图。在第 28 届年度 ACM 计算理论研讨会论文集 (STOC '96)。计算机协会,纽约,纽约,美国,571–575。DOI:https://doi.org/10.1145/237814.238005 )。在证明四色平面图总是可能的之后又过了 20 年。

关于java - 四色图定理递归回溯算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32642775/

相关文章:

objective-c - Float 和 int 数据类型影响性能

python - 2个字符串之间的删除距离

XML XSD 递归和命名空间

recursion - OCaml 中的匿名递归函数

java - 检索有向图每个顶点的 "out degree"(jgrapht)

java - 将 double int[][] 图像减小到更小的尺寸

java - 从正在运行的线程返回值并保持线程运行

java - 将文件写入本地系统时,签名的 Java 小程序会导致 AccessControlException

javascript - 返回二叉树中从根到节点的路径

linux - 管道查找到 `file`(命令)linux 命令递归