forked from okaydemir/4-color-theorem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfour_color.cpp
137 lines (109 loc) · 2.7 KB
/
four_color.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
#include <stdio.h>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
struct Node{//using nodes for graph representation
int number=-1;
int color=-1;
vector <Node*> *links = new vector<Node*>;//addresses of all vertices that this vertice is linked to
};
//defining functions and global variables
void input(char* infileName);// Reads input and updates vertices and links
vector <Node*> sortbyValence(vector <Node*> *arr);//Sorts the vector with quicksort according to their adjacency list size;
int N;//Number of vertices
vector<Node*> vertices;//Vector of addresses of all vertices
int main(int argc, char *argv[]){
if(argc!=3){
printf("Usage: ./four_color infile outfile\n");
return 0;
}
input(argv[1]);
//beginning of Welsh-Powell algorithm
vector<Node*> vertices_sorted= sortbyValence(&vertices);
for(int color=1;color<5;color++){
for(int i=0;i<N;i++){
if(vertices_sorted[i]->color==-1){
for(size_t j=0;j<static_cast<size_t>(N);j++){
if(j<vertices_sorted[i]->links->size()){
if(vertices_sorted[i]->links->at(j)->color==color){goto skip;//if one of the links has the same color, dont color
}
}
}
vertices_sorted[i]->color=color;
skip:;
}
}
}
//end of Welsh-Powell algorithm, beginning of output process
ofstream myfile;//start file stream
myfile.open (argv[2]);
for(int i=0;i<N;i++){
if(vertices[i]->color==-1){
myfile << "Graph cannot be four colored";
goto over;
}
}
for(int i=0;i<N;i++){
if(vertices[i]->color==1){
myfile << "red" << endl;
}else if(vertices[i]->color==2){
myfile << "blue" << endl;
}else if(vertices[i]->color==3){
myfile << "green" << endl;
}else if(vertices[i]->color==4){
myfile << "orange" << endl;
}
}
over:
myfile.close();
return 0;
}
void input(char* infileName){
ifstream myfile(infileName);//start file stream
string line;
getline(myfile, line);
stringstream data(line);
int K;
data >> N;
for(int i=0; i<N; i++){
Node* x=new Node;
x->number=i;
vertices.push_back(x);
//;
}
for(int i=0; i<N; i++){
getline(myfile, line);
stringstream data(line);
for(int j=0; j<N; j++){
data >> K;
if(K){
vertices[i]->links->push_back(vertices[j]);
}
}
}}
vector <Node*> sortbyValence(vector<Node*> *arr){
vector <Node*> ls;
vector <Node*> eq;
vector <Node*> gr;
if(arr->size()<=1){
return *arr;
}
size_t pivot=arr->at(0)->links->size();
for(size_t i = 0; i<arr->size(); i++){
if(arr->at(i)->links->size()>pivot){
ls.push_back(arr->at(i));
}
if(arr->at(i)->links->size()==pivot){
eq.push_back(arr->at(i));
}
if(arr->at(i)->links->size()<pivot){
gr.push_back(arr->at(i));
}}
vector<Node*> lsf= sortbyValence(&ls);
vector<Node*> grf=sortbyValence(&gr);
lsf.insert( lsf.end(), eq.begin(), eq.end() );
lsf.insert(lsf.end(), grf.begin(),grf.end());
return lsf;
}