1
1
/* *
2
2
* @file
3
3
* @brief prints the assigned colors
4
- * using [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorithm
4
+ * using [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring)
5
+ * algorithm
5
6
*
6
7
* @details
7
- * In graph theory, graph coloring is a special case of graph labeling;
8
- * it is an assignment of labels traditionally called "colors" to elements of a graph subject to certain constraints.
9
- * In its simplest form, it is a way of coloring the vertices of a graph such that no two adjacent vertices are of the same color;
10
- * this is called a vertex coloring. Similarly, an edge coloring assigns
11
- * a color to each edge so that no two adjacent edges are of the same color,
12
- * and a face coloring of a planar graph assigns a color to each face or
8
+ * In graph theory, graph coloring is a special case of graph labeling;
9
+ * it is an assignment of labels traditionally called "colors" to elements of a
10
+ * graph subject to certain constraints. In its simplest form, it is a way of
11
+ * coloring the vertices of a graph such that no two adjacent vertices are of
12
+ * the same color; this is called a vertex coloring. Similarly, an edge coloring
13
+ * assigns a color to each edge so that no two adjacent edges are of the same
14
+ * color, and a face coloring of a planar graph assigns a color to each face or
13
15
* region so that no two faces that share a boundary have the same color.
14
16
*
15
17
* @author [Anup Kumar Panwar](https://github.com/AnupKumarPanwar)
16
18
* @author [David Leal](https://github.com/Panquesito7)
17
19
*/
18
- #include < iostream>
19
20
#include < array>
21
+ #include < iostream>
20
22
#include < vector>
21
23
22
24
/* *
23
25
* @namespace
24
26
* @brief Backtracking algorithms
25
27
*/
26
28
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" << std::endl;
34
- for (auto &col : color) {
35
- std::cout << col;
36
- }
37
- std::cout << std::endl;
29
+ /* * A utility function to print solution
30
+ * @tparam V number of vertices in the graph
31
+ * @param color array of colors assigned to the nodes
32
+ */
33
+ template <size_t V>
34
+ void printSolution (const std::array<int , V>& color) {
35
+ std::cout << " Following are the assigned colors" << std::endl;
36
+ for (auto & col : color) {
37
+ std::cout << col;
38
38
}
39
+ std::cout << std::endl;
40
+ }
39
41
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
- }
42
+ /* * A utility function to check if the current color assignment is safe for
43
+ * vertex v
44
+ * @tparam V number of vertices in the graph
45
+ * @param v index of graph vertex to check
46
+ * @param graph matrix of graph nonnectivity
47
+ * @param color vector of colors assigned to the graph nodes/vertices
48
+ * @param c color value to check for the node `v`
49
+ * @returns `true` if the color is safe to be assigned to the node
50
+ * @returns `false` if the color is not safe to be assigned to the node
51
+ */
52
+ template <size_t V>
53
+ bool isSafe (int v, const std::array<std::array<int , V>, V>& graph,
54
+ const std::array< int , V>& color, int c ) {
55
+ for ( int i = 0 ; i < V; i++ ) {
56
+ if (graph[v][i] && c == color[i]) {
57
+ return false ;
56
58
}
57
- return true ;
58
59
}
60
+ return true ;
61
+ }
59
62
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
- }
63
+ /* * A recursive utility function to solve m coloring problem
64
+ * @tparam V number of vertices in the graph
65
+ * @param graph matrix of graph nonnectivity
66
+ * @param m number of colors
67
+ * @param [in,out] color description // used in,out to notify in documentation
68
+ * that this parameter gets modified by the function
69
+ * @param v index of graph vertex to check
70
+ */
71
+ template <size_t V>
72
+ void graphColoring (const std::array<std::array<int , V>, V>& graph, int m,
73
+ std::array<int , V> color, int v) {
74
+ // base case:
75
+ // If all vertices are assigned a color then return true
76
+ if (v == V) {
77
+ backtracking::printSolution<V>(color);
78
+ return ;
79
+ }
76
80
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;
81
+ // Consider this vertex v and try different colors
82
+ for (int c = 1 ; c <= m; c++) {
83
+ // Check if assignment of color c to v is fine
84
+ if (backtracking::isSafe<V>(v, graph, color, c)) {
85
+ color[v] = c;
82
86
83
- // recur to assign colors to rest of the vertices
84
- backtracking::graphColoring<V>(graph, m, color, v + 1 );
87
+ // recur to assign colors to rest of the vertices
88
+ backtracking::graphColoring<V>(graph, m, color, v + 1 );
85
89
86
- // If assigning color c doesn't lead to a solution then remove it
87
- color[v] = 0 ;
88
- }
90
+ // If assigning color c doesn't lead to a solution then remove it
91
+ color[v] = 0 ;
89
92
}
90
93
}
94
+ }
91
95
} // namespace backtracking
92
96
93
97
/* *
@@ -102,15 +106,12 @@ int main() {
102
106
// (0)---(1)
103
107
104
108
const int V = 4 ; // number of vertices in the graph
105
- std::array <std::array <int , V>, V> graph = {
106
- std::array <int , V>({0 , 1 , 1 , 1 }),
107
- std::array <int , V>({1 , 0 , 1 , 0 }),
108
- std::array <int , V>({1 , 1 , 0 , 1 }),
109
- std::array <int , V>({1 , 0 , 1 , 0 })
110
- };
109
+ std::array<std::array<int , V>, V> graph = {
110
+ std::array<int , V>({0 , 1 , 1 , 1 }), std::array<int , V>({1 , 0 , 1 , 0 }),
111
+ std::array<int , V>({1 , 1 , 0 , 1 }), std::array<int , V>({1 , 0 , 1 , 0 })};
111
112
112
113
int m = 3 ; // Number of colors
113
- std::array <int , V> color{};
114
+ std::array<int , V> color{};
114
115
115
116
backtracking::graphColoring<V>(graph, m, color, 0 );
116
117
return 0 ;
0 commit comments