From 5a030a4f762432bd08cd3f2eaa373f7ed752e167 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Mon, 29 Jun 2020 19:05:11 -0400 Subject: [PATCH 1/3] make he code neat and clean without global variables --- backtracking/graph_coloring.cpp | 64 +++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/backtracking/graph_coloring.cpp b/backtracking/graph_coloring.cpp index f308220ad..745fd99f0 100644 --- a/backtracking/graph_coloring.cpp +++ b/backtracking/graph_coloring.cpp @@ -1,18 +1,36 @@ +/** + * @file + * @brief brief one line description here + */ #include -/* - * Number of vertices in the graph +// created a namespace to ensure all globals and functions are +// restricted to this file +namespace { +/** A utility function to print solution + * @param color description + * @param V number of vertices in the graph */ -#define V 4 +void printSolution(const int *color, int V) { + std::cout << "Following are the assigned colors\n"; + for (int i = 0; i < V; i++) { + std::cout << color[i]; + } + std::cout << "\n"; +} -/** - * Prototypes +/** A utility function to check if the current color assignment is safe for + * vertex v + * @tparam V number of vertices in the graph + * @param v description + * @param graph description + * @param color description + * @param c description + * @returns `true` if .... + * @returns `false` if .... */ -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) { +template +bool isSafe(int v, const bool graph[V][V], const int *color, int c) { for (int i = 0; i < V; i++) { if (graph[v][i] && c == color[i]) { return false; @@ -21,12 +39,20 @@ bool isSafe(int v, bool graph[V][V], int color[], int c) { return true; } -/* A recursive utility function to solve m coloring problem */ -void graphColoring(bool graph[V][V], int m, int color[], int v) { +/* A recursive utility function to solve m coloring problem + * @tparam V number of vertices in the graph + * @param graph description + * @param m description + * @param [in,out] color description // used in,out to notify in documentation + * that this parameter gets modified by the function + * @param v description + */ +template +void graphColoring(bool graph[V][V], int m, int *color, int v) { // base case: // If all vertices are assigned a color then return true if (v == V) { - printSolution(color); + printSolution(color, V); return; } @@ -44,16 +70,7 @@ void graphColoring(bool graph[V][V], int m, int color[], int v) { } } } - -/* A utility function to print solution */ -void printSolution(int color[]) { - std::cout << "Following are the assigned colors\n"; - for (int i = 0; i < V; i++) { - std::cout << color[i]; - } - - std::cout << "\n"; -} +} // namespace /** * Main function @@ -66,6 +83,7 @@ int main() { // | / | // (0)---(1) + const int V = 4; // number of vertices in the graph bool graph[V][V] = { {0, 1, 1, 1}, {1, 0, 1, 0}, From 11ac67cc4430c33e21164d8c84732f07f060c538 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Mon, 29 Jun 2020 19:07:52 -0400 Subject: [PATCH 2/3] fix 2 stars in comment --- backtracking/graph_coloring.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/graph_coloring.cpp b/backtracking/graph_coloring.cpp index 745fd99f0..57c10d450 100644 --- a/backtracking/graph_coloring.cpp +++ b/backtracking/graph_coloring.cpp @@ -39,7 +39,7 @@ bool isSafe(int v, const bool graph[V][V], const int *color, int c) { return true; } -/* A recursive utility function to solve m coloring problem +/** A recursive utility function to solve m coloring problem * @tparam V number of vertices in the graph * @param graph description * @param m description From 2787dea011a3e1376bed2db556ec046b66f2d33b Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Mon, 29 Jun 2020 20:27:55 -0400 Subject: [PATCH 3/3] fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful --- backtracking/graph_coloring.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backtracking/graph_coloring.cpp b/backtracking/graph_coloring.cpp index 57c10d450..20e018cdb 100644 --- a/backtracking/graph_coloring.cpp +++ b/backtracking/graph_coloring.cpp @@ -48,7 +48,7 @@ bool isSafe(int v, const bool graph[V][V], const int *color, int c) { * @param v description */ template -void graphColoring(bool graph[V][V], int m, int *color, int v) { +void graphColoring(bool graph[V][V], int m, int color[V], int v) { // base case: // If all vertices are assigned a color then return true if (v == V) { @@ -59,11 +59,11 @@ void graphColoring(bool graph[V][V], int m, int *color, int v) { // 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 - if (isSafe(v, graph, color, c)) { + if (isSafe(v, graph, color, c)) { color[v] = c; // recur to assign colors to rest of the vertices - graphColoring(graph, m, color, v + 1); + graphColoring(graph, m, color, v + 1); // If assigning color c doesn't lead to a solution then remove it color[v] = 0; @@ -98,6 +98,6 @@ int main() { color[i] = 0; } - graphColoring(graph, m, color, 0); + graphColoring(graph, m, color, 0); return 0; }