Repository navigation
Expand file tree
/
Copy pathgraph-coloring.cpp
More file actions
executable file
·174 lines (126 loc) · 2.84 KB
/
Copy pathgraph-coloring.cpp
File metadata and controls
executable file
·174 lines (126 loc) · 2.84 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
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
163
164
165
166
167
168
169
170
171
172
173
174
/*
author: Hana B Pasandi (barahoueipash@vcu.edu)
file: graph-coloring.cpp
This is a programming assignment for CMSC501
*/
#include <iostream>
#include <list>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
# include <fstream>
using namespace std;
class Graph
{
int V;
list<int> *adj;
public:
Graph(int V) { this->V = V; adj = new list<int>[V]; }
~Graph() { delete[] adj; }
void addEdge(int v, int w);
void greedyColoring(vector<int> tokens);
};
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
struct vertexDegree
{
int v;
int degree;
};
bool compareByDegree(const vertexDegree& x, const vertexDegree& y)
{
return x.degree > y.degree;
}
void Graph::greedyColoring(vector<int> tokens)
{
int maxin = 0;
int *result = new int[V];
if (tokens.size() == V){
for (int j = 0; j <= V; ++j)
{
if (tokens[j] >= 0)
result[j] = tokens[j];
}
}
for (int i = 0; i<V; i++)
{
if (result[i] > maxin)
maxin = result[i];
}
int max_degree=0;
vertexDegree arr[V];
for (int i = 0; i < V; i++)
{
arr[i].v = i;
arr[i].degree = adj[i].size();
if(arr[i].degree>=max_degree){
max_degree = arr[i].degree;
}
}
cout <<"max degree: "<< max_degree <<endl;
bool *colored = new bool[V];
for (int c = 0; c < V; c++)
colored[c] = false;
for (int u = 0; u < V; u++)
{
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (result[*i] != -1)
colored[result[*i]] = true;
int c;
for (c = 0; c < V; c++)
if (colored[c] == false)
break;
if (result[u] <= 0)
result[u] = c;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (result[*i] != -1)
colored[result[*i]] = false;
}
int maxout = 0;
for (int i = 0; i<V; i++)
{
if (result[i]+1 > maxout)
maxout = result[i]+1;
}
ofstream myfile2;
myfile2.open("output.txt");
myfile2 << abs (maxout-1) << endl;
cout << "# of colors: "<<(maxout-1)<<endl;
for (int u = 0; u < V; u++)
myfile2 << result[u] << " ";
myfile2.close();
}
int main(int argc, char *argv[])
{
ifstream myfile(argv[1]);
string str;
if (myfile.is_open()) {
getline(myfile , str);
istringstream iss(str);
vector<int> tokens ((istream_iterator<int>(iss)), istream_iterator<int>());
int V = tokens[0];
int M = tokens[1];
Graph g(V);
while (getline(myfile, str)) {
istringstream iss(str);
vector<int> tokens ((istream_iterator<int>(iss)), istream_iterator<int>());
int v = tokens[0];
int w = tokens[1];
if (tokens.size() == 2)
g.addEdge(v - 1, w - 1);
if (tokens.size() < 2 || (tokens.size() > 2 && tokens.size() != V)) {
}
if (tokens.size() > 2) {
g.greedyColoring(tokens);
}
}
}
else cout << " Unable to open your file";
return 0;
}