-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
162 lines (124 loc) · 2.7 KB
/
main.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
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
/* Marcin Sienkiewicz
* nr. indeksu 186475
* Kolorowanie map, na podstawie algorytmu ze strony
* http://www.authorstream.com/Presentation/ankush85-159098-backtracking-126-134-education-ppt-powerpoint/
* strony 48-58
*/
#include "header.h"
//zmienne globalne
int my_array[MAX][MAX]; // Graf jako macierz incydencji
vector <int> X; // tymczasowy wektor do zapisu informacji o kolorach
int n; // n -liczba wierzcholkow
int paleta = 4; // cztery kolory, nie więcej!
string KOLORY[]= {"black","red","blue","green", "yellow"};
void wyswietl_info(){
int a, m;
for(m=0; m < n; m++) {
cout <<"\nWierzcholek "<< m <<" ma kolor " << KOLORY[my_array[m][m]] << ", i sasiaduje z: ";
for(a = 0; a < n; a++) {
if(my_array[m][a] !=0 && a != m) {
cout << a << "(" << KOLORY[my_array[a][a]]<<"), ";
}
}
}
cout<<endl;
}
void NextValue(int k) {
int j;
do {
X[k] = (X[k]+1) % (paleta+1); //nastepny kolor
if(X[k] == 0) {
return; //wszystkie kolory zostaly wykorzystane...
}
for(j=0; j < n; j++) {
if((my_array[k][j] != 0) && (X[k] == X[j])){
break;
}
}
if(j == n)
return;
}while(!0);
}
void Mcoloring(int k) {
int i;
do
{
NextValue(k); //przypisz wierzcholkowi kolor
if(X[k] == 0) {
return; //brak kolorow
}
if(k == n-1) {
for(i=0; i < n; i++) {
my_array[i][i] = X[i];
}
wyswietl_info();
}
else {
Mcoloring(k+1);
}
} while(!0);
}
void wczytaj_macierz(char *filename) {
int row, col;
string line;
ifstream file (filename);
getline(file, line);
stringstream ss(line);
ss >> n;
if(file.is_open()) {
row = 0;
while(file.good()) {
getline (file,line);
stringstream ss(line);
col=0;
while(ss >> my_array[row][col]) {
col++;
}
row++;
}
file.close();
}
for(int i=0; i < n; i++) {
X.push_back(0);
}
}
void print_array() {
int i, j;
cout << "wierzcholkow: " << n <<endl;
cout << "macierz:"<<endl;
for(i=0; i<n; i++) {
for(j=0;j<n;j++) {
cout << my_array[i][j];
}
cout << endl;
}
cout<<endl;
}
void wygeneruj_graf() {
ofstream file("graf.dot");
file << "Graph {\n";
for(int i=0; i<n; i++) {
file << "vertex" << i+1 << " [style=filled, color="<< KOLORY[my_array[i][i]]<<"]\n";
}
//teraz relacje?
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(my_array[i][j] !=0 && j != i) {
file << "vertex" << i+1 << " -- " << "vertex" << j << "\n";
}
my_array[j][i] = 0;
}
file << "\n";
}
file << "}\n";
file.close();
cout<< "wygenerowalem graf!\n";
}
int main(int argc, char *argv[]) {
wczytaj_macierz(argv[1]);
Mcoloring(0);
print_array();
wygeneruj_graf();
cout << endl;
return 0;
}