使用迭代BFS的M-Color图 - 如何回溯

时间:2015-03-15 18:41:16

标签: c++ graph iteration breadth-first-search

我试图在尝试解决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;
}

0 个答案:

没有答案