-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathgraph-coloring-using-backtracking-algorithm.cpp
70 lines (70 loc) · 1.34 KB
/
graph-coloring-using-backtracking-algorithm.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
#include <bits/stdc++.h>
#include <fstream>
#define m 10
using namespace std;
int n, cm, c=1;
int graph[m][m], x[m];
void input(){
int x;
ifstream in ("abc.txt");
in >> n;
for (int i = 0; i < n; i ++){
for (int j = 0; j < n; j ++){
in >> x;
graph[i][j] = x;
}
}
}
void print(){
for (int i = 0; i < n; i ++){
for (int j = 0; j < n; j ++){
cout << "\t" << graph[i][j];
}
cout << "\n";
}
}
void next_Value(int k){
while (1){
x[k] = (x[k] + 1) % (cm + 1);
if (x[k] == 0){
return;
}
int j;
for (j = 1; j <= k-1; j ++){
if (graph[k-1][j-1] >= 1 && x[k] == x[j]){
break;
}
}
if (j == k){
return;
}
}
}
void graph_coloring(int k){
while(1){
next_Value(k);
if (x[k] == 0){
return;
}
if (k == n){
cout << "Solution No:: ";
cout << c << endl;
for (int i = 0; i < n; i ++){
cout << char(i+65) << "-->" << x[i+1] << endl;
}
c ++;
}
else{
graph_coloring(k + 1);
}
}
}
int main(){
input();
cout << "The graph is :: " << endl;
print();
cout << "Enter the chromatic number :: " << endl;
cin >> cm;
graph_coloring(1);
return 0;
}