-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcoloring.cpp
More file actions
66 lines (61 loc) · 1.88 KB
/
Copy pathMcoloring.cpp
File metadata and controls
66 lines (61 loc) · 1.88 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
/*
Problem stattement :
Given an undirected graph and a number m, determine if the graph can be coloured with at most m colours such that no two adjacent vertices of the graph are colored with the same color. Here coloring of a graph means the assignment of colors to all vertices.
*/
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> solutions;
bool isOkay(int color, vector<int> nodeColors, vector<int> row) {
bool answer = true;
for(int i=0; i<row.size(); ++i) {
if(nodeColors[row[i]] == color) {
answer = false;
break;
}
}
return answer;
}
void display(vector<vector<int>> solutions) {
for(vector<int> arr : solutions) {
for(int a : arr) {
cout << a << " ";
}
cout << endl;
}
}
void dfs(vector<vector<int>>& graph, vector<int>& nodeColors, vector<int>& colors, int index) {
if(index >= graph.size()) {
solutions.push_back(nodeColors);
return;
}
for(int i=0; i<colors.size(); ++i) {
if(isOkay(colors[i], nodeColors, graph[index])) {
nodeColors[index] = colors[i];
dfs(graph, nodeColors, colors, index+1);
nodeColors[index] = -1;
}
}
}
int main() {
int edges, from = 0, to = 0, c;
cout << "Enter the number of edges: ";
cin >> edges;
vector<vector<int>> graph(edges);
vector<int> nodeColors(edges, -1);
vector<bool> visited(edges, false);
cin >> from >> to;
while(from != -1 && to != -1) {
graph[from].push_back(to);
graph[to].push_back(from);
cin >> from >> to; //because it is an undirected graph
}
cout << "Enter the M colors: ";
cin >> c;
vector<int> colors(c);
for(int i=0; i<c; ++i) {
colors[i] = i;
}
dfs(graph, nodeColors, colors, 0);
display(solutions);
return 0;
}