diff --git a/backtracking/graph_coloring.cpp b/backtracking/graph_coloring.cpp index 19a983019..cb0eddcd7 100644 --- a/backtracking/graph_coloring.cpp +++ b/backtracking/graph_coloring.cpp @@ -1,39 +1,45 @@ -#include +#include -// Number of vertices in the graph +/* + * Number of vertices in the graph + */ #define V 4 +/** + * Prototypes + */ void printSolution(int color[]); /* A utility function to check if the current color assignment is safe for vertex v */ bool isSafe(int v, bool graph[V][V], int color[], int c) { - for (int i = 0; i < V; i++) - if (graph[v][i] && c == color[i]) + for (int i = 0; i < V; i++) { + if (graph[v][i] && c == color[i]) { return false; + } + } return true; } /* A recursive utility function to solve m coloring problem */ void graphColoring(bool graph[V][V], int m, int color[], int v) { - /* base case: If all vertices are assigned a color then - return true */ + // base case: + // If all vertices are assigned a color then return true if (v == V) { printSolution(color); return; } - /* Consider this vertex v and try different colors */ + // Consider this vertex v and try different colors for (int c = 1; c <= m; c++) { - /* Check if assignment of color c to v is fine*/ + // Check if assignment of color c to v is fine if (isSafe(v, graph, color, c)) { color[v] = c; - /* recur to assign colors to rest of the vertices */ + // recur to assign colors to rest of the vertices graphColoring(graph, m, color, v + 1); - /* If assigning color c doesn't lead to a solution - then remove it */ + // If assigning color c doesn't lead to a solution then remove it color[v] = 0; } } @@ -41,9 +47,12 @@ void graphColoring(bool graph[V][V], int m, int color[], int v) { /* A utility function to print solution */ void printSolution(int color[]) { - printf(" Following are the assigned colors \n"); - for (int i = 0; i < V; i++) printf(" %d ", color[i]); - printf("\n"); + std::cout << "Following are the assigned colors\n"); + for (int i = 0; i < V; i++) { + std::cout << color[i]; + } + + std::cout << "\n"; } // driver program to test above function @@ -65,7 +74,9 @@ int main() { int color[V]; - for (int i = 0; i < V; i++) color[i] = 0; + for (int i = 0; i < V; i++) { + color[i] = 0; + } graphColoring(graph, m, color, 0); return 0;