-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathColoringGraph.cpp
More file actions
227 lines (203 loc) · 5.51 KB
/
ColoringGraph.cpp
File metadata and controls
227 lines (203 loc) · 5.51 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "stdafx.h"
#include "ColoringGraph.h"
#include <set>
#include <algorithm>
#define NO_COLOR -1
namespace Coloring
{
// ********************************************************
// ColorEdge::SetNodes()
// ********************************************************
void ColorEdge::SetNodes(ColorNode* node1, ColorNode* node2)
{
n1 = node1;
n2 = node2;
n1->edges.push_back(this);
n2->edges.push_back(this);
}
// ********************************************************
// ColorEdge::GetNeighbor()
// ********************************************************
ColorNode* ColorEdge::GetNeighbor(ColorNode* n)
{
return n1->id != n->id ? n1 : n2;
}
// ********************************************************
// ColorNode::IncrementSpentCount()
// ********************************************************
void ColorNode::IncrementSpentCount()
{
if (this->color == NO_COLOR)
this->spent++;
}
// ********************************************************
// ColorNode::UpdateNeighbours()
// ********************************************************
void ColorNode::UpdateNeighbours()
{
for(size_t i = 0; i < this->edges.size(); i++)
{
this->GetNeighbor(i)->IncrementSpentCount();
}
}
// ********************************************************
// ColorNode::GetNeighbor()
// ********************************************************
ColorNode* ColorNode::GetNeighbor(int i)
{
return this->edges[i]->GetNeighbor(this);
}
// ********************************************************
// ColorNode::AssignColor()
// ********************************************************
void ColorNode::AssignColor(int step)
{
int newColor = 0;
bool found = false;
do
{
found = false;
// choose the smallest one not used by neighbors
for(size_t i = 0; i < edges.size(); i++)
{
ColorNode* n = edges[i]->GetNeighbor(this);
if (n->color == newColor)
{
newColor++;
found = true;
break;
}
}
}
while(found);
this->spent = -1;
this->color = newColor;
this->step = step;
UpdateNeighbours();
}
// ********************************************************
// ColorGraph::InsertNode()
// ********************************************************
void ColorGraph::InsertNode(int id)
{
if (nodesMap.find(id) == nodesMap.end())
{
ColorNode* node = new ColorNode();
node->id = id;
nodesMap[id] = node;
nodes.push_back(node);
}
}
// ********************************************************
// ColorGraph::InsertEdge()
// ********************************************************
void ColorGraph::InsertEdge(int id1, int id2, double angle)
{
if (nodesMap.find(id1) == nodesMap.end())
InsertNode(id1);
if (nodesMap.find(id2) == nodesMap.end())
InsertNode(id2);
ColorEdge* edge = new ColorEdge();
edge->SetNodes(nodesMap[id1], nodesMap[id2]);
edge->angle = angle;
edges.push_back(edge);
}
// ********************************************************
// ColorGraph::GetColorCount()
// ********************************************************
int ColorGraph::GetColorCount()
{
std::set<int> colors;
for(size_t i = 0; i < nodes.size(); i++)
{
if (nodes[i]->color == NO_COLOR)
continue;
if (colors.find(nodes[i]->color) == colors.end())
{
colors.insert(nodes[i]->color);
}
}
return colors.size();
}
// ********************************************************
// ColorGraph::HasNonColoredNodes()
// ********************************************************
bool ColorGraph::HasNonColoredNodes()
{
for(size_t i = 0; i < nodes.size(); i++)
{
if (nodes[i]->color == NO_COLOR)
return true;
}
return false;
}
// ********************************************************
// ColorGraph::AddCandidates()
// ********************************************************
void ColorGraph::AddCandidates(ColorNode* n)
{
for(size_t i = 0; i < n->edges.size(); i++)
{
ColorNode* nb = n->GetNeighbor(i);
if (nb->spent == 0)
candidates.push_back(nb);
}
}
// ********************************************************
// ColorGraph::DoColoring()
// ********************************************************
bool ColorGraph::DoColoring()
{
if (nodes.size() == 0)
return false;
candidates.clear();
int step = 0;
int count = 0;
do
{
// setting a seed
int seedId = 0;
if (count == 0) {
seedId = rand() % (nodes.size() - 1);
}
else {
std::vector<int> nonColored;
for(size_t i = 0; i < nodes.size(); i++)
{
if (nodes[i]->color == -1)
nonColored.push_back(i);
}
seedId = nonColored.size() > 10 ? nonColored[rand() % (nonColored.size() - 1)] : nonColored[0];
}
ColorNode* n = nodes[seedId];
n->AssignColor(step++);
AddCandidates(n);
bool proceed = true;
do
{
proceed = false;
ColorNode* maxNode;
int max = -2;
std::list<ColorNode*>::iterator it = candidates.begin();
while(it != candidates.end())
{
if ((*it)->spent > max)
{
max = (*it)->spent;
maxNode = *it;
proceed = true;
}
it++;
}
maxNode->AssignColor(step++);
maxNode->count = count++;
candidates.remove(maxNode);
AddCandidates(maxNode);
}
while(proceed);
}
while(HasNonColoredNodes());
candidates.clear();
return true;
}
}