【问题标题】:Four Color Map Theorem Recursive Backtracking Algorithm四色映射定理递归回溯算法
【发布时间】:2015-09-18 02:10:36
【问题描述】:

我已经对以下程序进行了编码以递归地实现四色地图定理(简而言之,任何地图都只能用 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();
    }
}

【问题讨论】:

    标签: java algorithm recursion backtracking recursive-backtracking


    【解决方案1】:

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

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

        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] = i; 中的map[r]),等等之后返回时,我们立即返回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 年后,它又出现了。

    【讨论】:

    • 您与二次算法的链接已损坏 - 404
    猜你喜欢
    • 2015-01-21
    • 2018-10-08
    • 2011-12-27
    • 2019-10-11
    • 2021-04-28
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多