15
15
* @author [Anup Kumar Panwar](https://github.com/AnupKumarPanwar)
16
16
* @author [David Leal](https://github.com/Panquesito7)
17
17
*/
18
- #include < iostream>
19
- #include < array>
20
- #include < vector>
18
+ #include < iostream> // / for IO operations
19
+ #include < array> // / for std::array
20
+ #include < vector> // / for std::vector
21
21
22
22
/* *
23
- * @namespace
23
+ * @namespace backtracking
24
24
* @brief Backtracking algorithms
25
25
*/
26
26
namespace backtracking {
27
- /* * A utility function to print solution
28
- * @tparam V number of vertices in the graph
29
- * @param color array of colors assigned to the nodes
30
- */
31
- template <size_t V>
32
- void printSolution (const std::array <int , V>& color) {
33
- std::cout << " Following are the assigned colors\n " ;
34
- for (auto &col : color) {
35
- std::cout << col;
36
- }
37
- std::cout << " \n " ;
27
+ /* *
28
+ * @namespace graph_coloring
29
+ * @brief Functions for the [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorith,
30
+ */
31
+ namespace graph_coloring {
32
+ /* *
33
+ * @brief A utility function to print the solution
34
+ * @tparam V number of vertices in the graph
35
+ * @param color array of colors assigned to the nodes
36
+ */
37
+ template <size_t V>
38
+ void printSolution (const std::array <int , V>& color) {
39
+ std::cout << " Following are the assigned colors\n " ;
40
+ for (auto &col : color) {
41
+ std::cout << col;
38
42
}
43
+ std::cout << " \n " ;
44
+ }
39
45
40
- /* * A utility function to check if the current color assignment is safe for
41
- * vertex v
42
- * @tparam V number of vertices in the graph
43
- * @param v index of graph vertex to check
44
- * @param graph matrix of graph nonnectivity
45
- * @param color vector of colors assigned to the graph nodes/vertices
46
- * @param c color value to check for the node `v`
47
- * @returns `true` if the color is safe to be assigned to the node
48
- * @returns `false ` if the color is not safe to be assigned to the node
49
- */
50
- template < size_t V>
51
- bool isSafe ( int v, const std::array<std::array < int , V>, V>& graph, const std::array < int , V>& color, int c) {
52
- for ( int i = 0 ; i < V; i++ ) {
53
- if (graph[v][i] && c == color[i] ) {
54
- return false ;
55
- }
46
+ /* *
47
+ * @brief Utility function to check if the current color assignment is safe for
48
+ * vertex v
49
+ * @tparam V number of vertices in the graph
50
+ * @param v index of graph vertex to check
51
+ * @param graph matrix of graph nonnectivity
52
+ * @param color vector of colors assigned to the graph nodes/vertices
53
+ * @param c color value to check for the node `v`
54
+ * @returns `true ` if the color is safe to be assigned to the node
55
+ * @returns `false` if the color is not safe to be assigned to the node
56
+ */
57
+ template < size_t V>
58
+ bool isSafe ( int v, const std::array<std::array < int , V>, V>& graph, const std::array < int , V>& color, int c ) {
59
+ for ( int i = 0 ; i < V; i++ ) {
60
+ if (graph[v][i] && c == color[i]) {
61
+ return false ;
56
62
}
57
- return true ;
58
63
}
64
+ return true ;
65
+ }
59
66
60
- /* * A recursive utility function to solve m coloring problem
61
- * @tparam V number of vertices in the graph
62
- * @param graph matrix of graph nonnectivity
63
- * @param m number of colors
64
- * @param [in,out] color description // used in,out to notify in documentation
65
- * that this parameter gets modified by the function
66
- * @param v index of graph vertex to check
67
- */
68
- template <size_t V>
69
- void graphColoring (const std::array<std::array <int , V>, V>& graph, int m, std::array <int , V> color, int v) {
70
- // base case:
71
- // If all vertices are assigned a color then return true
72
- if (v == V) {
73
- backtracking::printSolution<V>(color);
74
- return ;
75
- }
67
+ /* *
68
+ * @brief Recursive utility function to solve m coloring problem
69
+ * @tparam V number of vertices in the graph
70
+ * @param graph matrix of graph nonnectivity
71
+ * @param m number of colors
72
+ * @param [in,out] color description // used in,out to notify in documentation
73
+ * that this parameter gets modified by the function
74
+ * @param v index of graph vertex to check
75
+ */
76
+ template <size_t V>
77
+ void graphColoring (const std::array<std::array <int , V>, V>& graph, int m, std::array <int , V> color, int v) {
78
+ // base case:
79
+ // If all vertices are assigned a color then return true
80
+ if (v == V) {
81
+ printSolution<V>(color);
82
+ return ;
83
+ }
76
84
77
- // Consider this vertex v and try different colors
78
- for (int c = 1 ; c <= m; c++) {
79
- // Check if assignment of color c to v is fine
80
- if (backtracking:: isSafe<V>(v, graph, color, c)) {
81
- color[v] = c;
85
+ // Consider this vertex v and try different colors
86
+ for (int c = 1 ; c <= m; c++) {
87
+ // Check if assignment of color c to v is fine
88
+ if (isSafe<V>(v, graph, color, c)) {
89
+ color[v] = c;
82
90
83
- // recur to assign colors to rest of the vertices
84
- backtracking:: graphColoring<V>(graph, m, color, v + 1 );
91
+ // recur to assign colors to rest of the vertices
92
+ graphColoring<V>(graph, m, color, v + 1 );
85
93
86
- // If assigning color c doesn't lead to a solution then remove it
87
- color[v] = 0 ;
88
- }
94
+ // If assigning color c doesn't lead to a solution then remove it
95
+ color[v] = 0 ;
89
96
}
90
97
}
98
+ }
99
+ } // namespace graph_coloring
91
100
} // namespace backtracking
92
101
93
102
/* *
94
- * Main function
103
+ * @brief Main function
104
+ * @returns 0 on exit
95
105
*/
96
106
int main () {
97
107
// Create following graph and test whether it is 3 colorable
@@ -112,6 +122,6 @@ int main() {
112
122
int m = 3 ; // Number of colors
113
123
std::array <int , V> color{};
114
124
115
- backtracking::graphColoring<V>(graph, m, color, 0 );
125
+ backtracking::graph_coloring:: graphColoring<V>(graph, m, color, 0 );
116
126
return 0 ;
117
127
}
0 commit comments