-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphKColoring.cpp
More file actions
196 lines (159 loc) · 3.61 KB
/
GraphKColoring.cpp
File metadata and controls
196 lines (159 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
// QUESTION: https://www.techiedelight.com/print-k-colorable-configurations-graph-vertex-coloring-graph/
/*#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
// data structure to store graph edges
struct Edge {
int src, dest;
};
// class to represent a graph object
class Graph
{
public:
// An array of vectors to represent adjacency list
vector<vector<int>> adj;
// Constructor
Graph(vector<Edge> const &edges, int N)
{
// resize the vector to N elements of type vector<int>
adj.resize(N);
// add edges to the undirected graph
for (int i = 0; i < edges.size(); i++)
{
int src = edges[i].src;
int dest = edges[i].dest;
adj[src].push_back(dest);
adj[dest].push_back(src);
}
}
};
// string array to store colors (10-colorable graph)
string COLORS[] = { "", "BLUE", "GREEN", "RED", "YELLOW", "ORANGE",
"PINK", "BLACK", "BROWN", "WHITE", "PURPLE" };
// Function to check if it is safe to assign color c to vertex v
bool isSafe(Graph const &graph, vector<int> color, int v, int c)
{
// check color of every adjacent vertex of v
for (int u : graph.adj[v])
if (color[u] == c)
return false;
return true;
}
void kColorable(Graph const &graph, vector<int> &color, int k, int v, int N)
{
// if all colors are assigned, print the solution
if (v == N)
{
for (int v = 0; v < N; v++)
cout << setw(8) << left << COLORS[color[v]];
cout << endl;
return;
}
// try all possible combinations of available colors
for (int c = 1; c <= k; c++)
{
// if it is safe to assign color c to vertex v
if (isSafe(graph, color, v, c))
{
// assign color c to vertex v
color[v] = c;
// recur for next vertex
kColorable(graph, color, k, v + 1, N);
// backtrack
color[v] = 0;
}
}
}
// main function
int main()
{
// vector of graph edges as per above diagram
vector<Edge> edges = {
{ 0, 1 },{ 0, 4 },{ 0, 5 },{ 4, 5 },{ 1, 4 },{ 1, 3 },{ 2, 3 },{ 2, 4 }
};
// Number of vertices in the graph
int N = 6;
// create a graph from edges
Graph g(edges, N);
int k = 3;
vector<int> color(N, 0);
// print all k-colorable configurations of the graph
kColorable(g, color, k, 0, N);
return 0;
}*/
struct Edge
{
int src;
int dest;
};
class UndirectedGraph
{
private:
std::vector<std::vector<int>> adj;
std::vector<std::string> colors;
std::vector<int> coloredNodes;
int nodes;
public:
UndirectedGraph(int num, const std::vector<Edge>& edges)
: nodes(num)
{
adj.resize(nodes);
coloredNodes = std::vector<int>(nodes, 0);
for (const auto& iter : edges)
{
adj[iter.src].push_back(iter.dest);
adj[iter.dest].push_back(iter.src);
}
colors = { "", "BLUE", "GREEN", "RED", "YELLOW", "ORANGE", "PINK", "BLACK", "BROWN", "WHITE", "PURPLE" };
}
void printAllKColorable(int k)
{
printHelper(k, 0);
}
private:
void printHelper(int k, int nodeIndex)
{
if (nodeIndex == nodes)
{
for (auto iter : coloredNodes)
{
std::cout << colors[iter] << " ";
}
std::cout << std::endl;
return;
}
for (int i = 1; i <=k; i++)
{
if (isSafe(i, nodeIndex))
{
coloredNodes[nodeIndex] = i;
printHelper(k, nodeIndex + 1);
coloredNodes[nodeIndex] = 0;
}
}
}
bool isSafe(int color_index, int start)
{
for (auto j : adj[start])
{
if (coloredNodes[j] == color_index)
return false;
}
return true;
}
};
int main()
{
std::vector<Edge> edges{ {0, 1}, {0, 4}, {0, 5}, {4, 5}, {1, 4}, {1, 3}, {2, 3},{ 2, 4 } };
int num = 6;
int k = 3;
UndirectedGraph obj(num, edges);
obj.printAllKColorable(k);
return 0;
}