C++ 使用迭代BFS的M色图-如何回溯

C++ 使用迭代BFS的M色图-如何回溯,c++,graph,iteration,breadth-first-search,C++,Graph,Iteration,Breadth First Search,我试图在解决m-着色问题的同时进行回溯。我知道存在递归解决方案,但我想尝试一种迭代解决方案。代码中显示了我的回溯尝试。该程序为我运行的测试用例提供了正确答案。我想知道我的回溯方法是否正确 当我必须回溯时,我会在将当前顶点放入队列之前弹出当前队列的所有元素 #include<stdio.h> #include<queue> #include <iostream> // Number of vertices in the graph #define V 4 in

我试图在解决m-着色问题的同时进行回溯。我知道存在递归解决方案,但我想尝试一种迭代解决方案。代码中显示了我的回溯尝试。该程序为我运行的测试用例提供了正确答案。我想知道我的回溯方法是否正确

当我必须回溯时,我会在将当前顶点放入队列之前弹出当前队列的所有元素

#include<stdio.h>
#include<queue>
#include <iostream> 
// Number of vertices in the graph
#define V 4
int colorArr[V];
bool graphColoring(bool graph[][V], int m)
{
    // Create a color array to store colors assigned to all veritces. Vertex 
    // number is used as index in this array. The value '-1' of  colorArr[i] 
    // is used to indicate that no color is assigned to vertex 'i'. 

    for (int i = 0; i < V; ++i)
        colorArr[i] = -1;

    int colors[V][3] = {{1,2,3},{1,2,3},{1,2,3},{1,2,3}}; //all possible colors for each vertex...if -1 means that color is impermissible
    // Assign first color to source
    colorArr[0] = colors[0][0];

    // Create a queue (FIFO) of vertex numbers and enqueue source vertex
    // for BFS traversal
    std::queue <int> q;
    q.push(0);

    // Run while there are vertices in queue (Similar to BFS)
    while (!q.empty())
    {
        // Dequeue a vertex from queue ( Refer http://goo.gl/35oz8 )
        int u = q.front();
        q.pop();
         // Find all non-colored adjacent vertices
        for (int v = 0; v < V; ++v)
        {
            // An edge from u to v exists and destination v is not colored
            if (graph[u][v] && colorArr[v] == -1)
            {
                // Assign alternate color to this adjacent v of u
                for (int c = 0; c < m; c++)
                {
                    if (((c+1) != colorArr[u]) && (colors[v][c] != -1)) {
                        colorArr[v] = colors[v][c];
                        break;
                    }
                    else if (c== (m-1)) // all colors are impermissible for vertex v therefore return false
                        return false;
                }    
                q.push(v);
            }

            //  An edge from u to v exists and destination v is colored with
            // same color as u
            else if (graph[u][v] && (colorArr[v] == colorArr[u])) 
            {
                colors[v][colorArr[u]-1] = -1; // mark this is impermissible
                colorArr[v] = -1; // recompute color again
                int count = 0;
                for (int c = 0; c < m; c++)
                {
                    if (colors[v][c] == -1)
                    {
                        count++;
                        if (count == m) // if all are impermissible, then return false  
                            return false;
                    }
                }
                // Pop all elements in preparation for backtracking
                while (!q.empty())
                    q.pop();
                q.push(u); //MY ATTEMPT TO BACKTRACK??? 
            }
        }
    }

    // If we reach here, then all adjacent vertices can be colored with 
    // alternate color
    return true;
}
/* A utility function to print solution */
void printSolution(int color[])
{
    printf("Solution Exists:"
            " Following are the assigned colors \n");
    for (int i = 0; i < V; i++)
      printf(" %d ", color[i]);
    printf("\n");
}

// driver program to test above function
int main()
{
    /* Create following graph and test whether it is 3 colorable
      (3)---(2)
       |   / |
       |  /  |
       | /   |
      (0)---(1)
    */
    bool graph[V][V] = {{0, 1, 1, 1},
        {1, 0, 1, 0},
        {1, 1, 0, 1},
        {1, 0, 1, 0},
    };
    int m = 3; // Number of colors
    bool solution = graphColoring (graph, m);
    if (solution)
        printSolution(colorArr);
   return 0;
}
#包括
#包括
#包括
//图中的顶点数
#定义v4
int colorArr[V];
布尔图颜色(布尔图[][V],整数m)
{
//创建一个颜色数组来存储分配给所有veritces的颜色。顶点
//数字用作此数组中的索引。colorArr[i]的值'-1'
//用于指示没有为顶点“i”指定颜色。
对于(int i=0;i
这里有人能帮忙吗?