-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph_yu.cpp
More file actions
344 lines (305 loc) · 6.53 KB
/
graph_yu.cpp
File metadata and controls
344 lines (305 loc) · 6.53 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "graph.h"
#include <iostream>
#include <string>
#include <ctype.h>
Graph* graph_ = 0;
Edge::Edge(Shape *a, Shape *b)
{
if ( a->_id <= b->_id ) { shape[0] = a; shape[1] = b; }
else { shape[0] = b; shape[1] = a; }
}
bool Edge::operator < (const Edge& rhs) const{
int id1a = shape[0]->_id;
int id1b = shape[1]->_id;
int id2a = rhs.shape[0]->_id;
int id2b = rhs.shape[1]->_id;
if ( id1a == id2a ) return id1b < id2b;
if ( id1a == id2b ) return id1b < id2a;
if ( id1b == id2a ) return id1a < id2b;
if ( id1b == id2b ) return id1a < id2a;
return true;
}
Shape * Edge::getNeighbor(Shape *n)
{
if ( shape[0] == n ) return shape[1];
if ( shape[1] == n ) return shape[0];
return 0;
}
/*
Shape::Shape(const int& i)
{
_id = i;
traveled = false;
color = 0;
}*/
void Shape::addEdge(Edge *e)
{
edge.push_back(e);
}
bool edgeComp( const Edge* A, const Edge* B ){
if ( (*A) < (*B) ) return true;
return false;
}
void Shape::sortEdge()
{
sort(edge.begin(), edge.end(), edgeComp);
}
Graph::Graph(const string& n)
{
name = n;
}
Graph::~Graph()
{
vector<Edge *>::iterator itE;
for ( itE = edges.begin() ; itE != edges.end() ; itE++ )
{
delete (*itE);
(*itE) = 0;
}
map<int, Shape *>::iterator itN;
for ( itN = shapesMap.begin() ; itN != shapesMap.end() ; itN++ )
{
delete (*itN).second;
(*itN).second = 0;
}
vector<Shape *>::iterator itN2;
for ( itN2 = shapes.begin() ; itN2 != shapes.end() ; itN2++ )
{
(*itN2) = 0;
}
}
/*
void Graph::addEdge(const int& v1, const int& v2)
{
Shape *a, *b;
map<int, Shape *>::iterator it = shapesMap.find(v1);
if ( it != shapesMap.end() )
a = (*it).second;
else
{
a = new Shape(v1);
shapesMap[v1] = a;
shapes.push_back(a);
}
it = shapesMap.find(v2);
if ( it != shapesMap.end() )
b = (*it).second;
else
{
b = new Shape(v2);
shapesMap[v2] = b;
shapes.push_back(b);
}
Edge *e = new Edge(a, b);
edges.push_back(e);
a->edge.push_back(e);
b->edge.push_back(e);
}*/
void Graph::addEdge(const int& v1, const int& v2)
{
Shape *a, *b;
map<int, Shape*>::iterator it = shapesMap.find(v1);
if(it != shapesMap.end()) a = (*it).second;
else cerr << "Shape" << v1 << "doesn't exist. " << endl;
it = shapesMap.find(v2);
if(it != shapesMap.end()) a = (*it).second;
else cerr << "Shape" << v2 << "doesn't exist. " << endl;
Edge* e = new Edge(a, b);
edges.push_back(e);
a->edge.push_back(e);
b->edge.push_back(e);
}
void Graph::sortEdgesOfShape()
{
map<int, Shape *>::iterator it;
for ( it = shapesMap.begin() ; it != shapesMap.end() ; it++ )
{
Shape *shape = (*it).second;
shape->sortEdge();
}
}
bool ShapeCompByD( const Shape* A, const Shape* B ){
if ( A->edge.size() > B->edge.size() )
return true;
else if (A->edge.size() == B->edge.size()) {
if (A->_id < B->_id)
return true;
else
return false;
}
else
return false;
}
void Graph::sortShapesByDegree()
{
sort(shapes.begin(), shapes.end(), ShapeCompByD);
}
bool ShapeCompByID( const Shape* A, const Shape* B ){
if ( A->_id < B->_id ) return true;
else return false;
}
void Graph::sortShapesByID()
{
sort(shapes.begin(), shapes.end(), ShapeCompByID);
}
void Graph::init()
{
map<int, Shape *>::iterator itN;
for ( itN = shapesMap.begin() ; itN != shapesMap.end() ; itN++ )
{
Shape *shape = (*itN).second;
shape->traveled = false;
//shape->d = DIS_INF;
//shape->prev = 0;
shape->color = -1;
}
}
Shape * Graph::getShapeById(const int& id)
{
return shapesMap[id];
}
bool Graph::readFile( char* filename) {
fstream fin(filename);
if(fin.is_open()) {
char c; //get single char
char buf[1024]; //getline
int parameter[3];
int shape_count=0;
//set up alpha beta omega
for(int i=0; i<3; i++) {
fin.getline(buf,sizeof(buf));
int pos = 0;
while(buf[pos] != '=') {
pos++;
}
pos++;
int num_char=0;
while(isalnum(buf[pos])) {
int temp = buf[pos]-'0';
num_char *= 10;
num_char += temp;
pos++;
}
parameter[i] = num_char;
}
alpha = parameter[0];
beta = parameter[1];
omega = parameter[2];
//set up shapes
int index=0;
int coordinate[4]={0,0,0,0};
while(!fin.eof()) {
shape_count = 0;
while(shape_count < 4) {
fin.get(c);
int num_char=0;
while(isalnum(c)) {
int temp = c - '0';
num_char *= 10;
num_char += temp;
fin.get(c);
}
coordinate[shape_count] = num_char;
shape_count++;
}
// Shape (index, x0, y0, x1, y1)
Shape* s = new Shape(index, coordinate[0], coordinate[1], coordinate[2], coordinate[3] );
shapes.push_back(s);
shapesMap[index++] = s;
}
//shapes[0] is dummy shape (0,0) (0,0)
}
else {
return false;
}
return true;
}
void Graph::DFS(Shape* v, ostream& outfile, int& counter)
{
v->traveled = true;
if(v->edge.size() == 0) {
//return false;
}
else {
for(int i=0; i < v->edge.size();i++) {
if(v->edge[i] != 0) {
Shape* v_dfs = v->edge[i]->getNeighbor(v);
if(!v_dfs->traveled) {
outfile << "v" << v->_id << " -- v" << v_dfs->_id << ";" <<endl;
counter++;
DFS(v_dfs, outfile, counter);
}
}
}
}
}
void Graph::BFS(Shape* v, ostream& outfile, int& counter, queue<Shape*>& list)
{
v->traveled = true;
if(v->edge.size() == 0) {
//return false;
}
else {
for(int i=0; i < v->edge.size();i++) {
if(v->edge[i] != 0) {
Shape* v_bfs = v->edge[i]->getNeighbor(v);
if(!v_bfs->traveled) {
list.push(v_bfs);
v_bfs->traveled = true;
outfile << "v" << v->_id << " -- v" << v_bfs->_id << ";" << endl;
counter++;
}
}
}
}
if(list.size() != 0) {
Shape* next = list.front();
list.pop();
BFS(next, outfile, counter, list);
}
}
/*
void Graph::Color(Shape* v, queue<Shape*>& list)
{
if(list.size() != 0) {
Shape* next = list.front();
list.pop();
//coloring
int print = 1;
vector<int> colorlist;
for(int i=0; i<v->edge.size(); i++) {
colorlist.push_back(v->edge[i]->getNeighbor(v)->color);
}
sort(colorlist.begin(), colorlist.end());
for (int i=0;i<colorlist.size();i++) {
if(print == colorlist[i]) {
print++;
}
}
v->color = print;
Color(next, list);
}
else {
int print = 1;
vector<int> colorlist;
for(int i=0; i<v->edge.size(); i++) {
colorlist.push_back(v->edge[i]->getNeighbor(v)->color);
}
sort(colorlist.begin(), colorlist.end());
for (int i=0;i<colorlist.size();i++) {
if(print == colorlist[i]) {
print++;
}
}
}
}
*/
void Graph::reset_travel()
{
map<int, Shape *>::iterator it;
for ( it = shapesMap.begin() ; it != shapesMap.end() ; it++ )
{
Shape *shape = (*it).second;
shape->traveled = false;
}
}