-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_coloring.cpp
73 lines (64 loc) · 1.87 KB
/
m_coloring.cpp
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
/*
Given an undirected graph and an integer M. The task is to determine if the graph can be colored
with at most M colors 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. Print 1 if it is possible to
colour vertices and 0 otherwise.
*/
#include <bits/stdc++.h>
using namespace std;
//function to check if it is safe to assign color c to vertex v
bool issafe(bool graph[100][100], int v, int n, int c, int color[]){
//check v's adjecent vertices whether they are assigned same color or not
for(int i = 0; i < n; i++){
if(graph[i][v] == 1 && color[i] == c){
return false;
}
}
return true;
}
bool solveColoring(bool graph[100][100], int v, int m, int n, int color[]){
//if all vertices are coloured return true
if(v == n){
//print all v vs their color
for(int i = 0; i < n; i++){
cout << i+1 << ": " << color[i] << endl;
}
return true;
}
//considering current vertex v try different colours
for(int i = 1; i <= m; i++){
//check if it is safe to assign color i to vertex v
if(issafe(graph, v, n, i, color)){
color[v] = i;
//recur for rest of the vertices
if(solveColoring(graph, v+1, m, n, color))
return true;
//if assigning color i to vertex v didn't lead to
//any solution them remove it
color[v] = 0;
}
}
return false;
}
bool graphColoring(bool graph[100][100], int m, int n){
//array to store colours assigned to the vertices
int color[n] = {0};
//start from first vertix
return solveColoring(graph, 0, m, n, color);
}
int main(){
int n, m, e;
cin >> n >> m >> e;
bool graph[100][100];
for(int i = 0; i < n; i++){
memset(graph[i], 0, sizeof(graph[i]));
}
for(int i = 0; i < e; i++){
int v1, v2;
cin >> v1 >> v2;
graph[v1-1][v2-1] = 1;
graph[v2-1][v1-1] = 1;
}
cout << graphColoring(graph, m, n) << "\n";
return 0;
}