-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphVector.cpp
384 lines (326 loc) · 9.12 KB
/
graphVector.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* BetaCome: betacome of future network routing
*
* Code to accompany the competition:
* huawei software challenge : future network routing,
* Yuanyuan Qin, Song Tang, Meiying Wu, 2016
*
* Copyright (C) 2016 Yuanyuan Qin, Peking University, Shenzhen, China
*
* This file is part of BetaCome.
*
* BetaCome is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BetaCome is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BetaCome. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "graphVector.h"
#include "assist.h"
#include <queue>
#include <cassert>
#include <sstream>
#include <iostream>
#include <unordered_set>
using namespace std;
int MAXNODEID = 0;
int TIMELIMITNUM = 0;
//using topo and demand to initialize graph
GraphVector::GraphVector(char *topo[], int edge_num) :GraphBase()
{
graphKind = GRAPHVECTOR;
int i = 0;
int edgeId = 0;
int uu = 0;
int vv = 0;
int edgeWeight = 0;
char *pLine;
int maxNodeIdOld = 0;
unordered_set<int> nodeIdSet;
//input edge and node of graph
while (i<edge_num)
{
//change ',' to ' ' then we can use cin to get num
pLine = topo[i];
while (*pLine != '\0')
{
if (*pLine == ',') *pLine = ' ';
pLine++;
}
string strLine(topo[i]);
istringstream istr(strLine);
i++;
if (!(istr >> edgeId >> uu >> vv >> edgeWeight))//get in num
{
continue;
}
nodeIdSet.insert(uu);
nodeIdSet.insert(vv);
MAXNODEID = assistMax(MAXNODEID, uu);
MAXNODEID = assistMax(MAXNODEID, vv);
if (MAXNODEID > maxNodeIdOld)
{
graphNode.resize(MAXNODEID + 1);
maxNodeIdOld = MAXNODEID;
}
insertEdge(edgeId, uu, vv, edgeWeight);//insert edge
#ifdef DEBUG
cout << edgeId << " " << uu << " " << vv << " " << edgeWeight << " " << endl;
#endif
}
nodeNum = nodeIdSet.size();
TIMELIMITNUM = assistMin(((nodeNum / 100 + 3) * 1000), 9000);
}
GraphVector::GraphVector(char *topo[], int edge_num, bool reverse)
{
if (reverse)
{
graphKind = GRAPHVECTOR_REVERSE;
int i = 0;
int edgeId = 0;
int uu = 0;
int vv = 0;
int edgeWeight = 0;
char *pLine;
int maxNodeIdOld = 0;
unordered_set<int> nodeIdSet;
//input edge and node of graph
while (i<edge_num)
{
//change ',' to ' ' then we can use cin to get num
pLine = topo[i];
while (*pLine != '\0')
{
if (*pLine == ',') *pLine = ' ';
pLine++;
}
string strLine(topo[i]);
istringstream istr(strLine);
i++;
if (!(istr >> edgeId >> vv >> uu >> edgeWeight))//get in num
{
//here, we reversed uu and vv
continue;
}
nodeIdSet.insert(uu);
nodeIdSet.insert(vv);
MAXNODEID = assistMax(MAXNODEID, uu);
MAXNODEID = assistMax(MAXNODEID, vv);
if (MAXNODEID > maxNodeIdOld)
{
graphNode.resize(MAXNODEID + 1);
maxNodeIdOld = MAXNODEID;
}
insertEdge(edgeId, uu, vv, edgeWeight);//insert edge
#ifdef DEBUG
cout << edgeId << " " << uu << " " << vv << " " << edgeWeight << " " << endl;
#endif
}
nodeNum = nodeIdSet.size();
}
}
//reverse edge of srcGraph,get a new dstGraph
//just copy valid point, point invalid and delete, do not copy
//int GraphVector::reverseGraph(GraphBase &dstGraph)
//{
// int dstGraphNodeIndex = 0;
//
// //copy graph information
// dstGraph.setGraphKind(graphKind);
//
// for (auto iterPoint = graphNode.begin(); iterPoint != graphNode.end(); ++iterPoint)
// {
// Point &pointTemp = (*iterPoint);
//
// //if in srcgraph, this point is valid
// if (pointTemp.getPointId() != POINT_INVALID)
// {
// //copy point
// Point &dstPointTemp = dstGraph.getNode(dstGraphNodeIndex);
// dstPointTemp.setInoutDegree(pointTemp.getInoutDegree());
// dstPointTemp.setPointId(pointTemp.getPointId());
// dstPointTemp.setSccNum(pointTemp.getSccNum());
// dstPointTemp.setTopoOrder(pointTemp.getTopoOrder());
//
// //insert reverse edge
// vector<Edge> &srcEdgeVectorTemp = pointTemp.getEdgeVector();
// for (auto iterEdgeTemp = srcEdgeVectorTemp.begin(); iterEdgeTemp != srcEdgeVectorTemp.end(); ++iterEdgeTemp)
// {
// Edge &edgeTemp = (*iterEdgeTemp);
//
// dstGraph.insertEdge(edgeTemp.getEdgeId(), edgeTemp.getNodeTwo(), edgeTemp.getNodeOne(), edgeTemp.getEdgeWeight());
// }
//
// }
//
// dstGraphNodeIndex++;
// }
//
// return EXIT_SUCCESS;
//}
Edge& GraphVector::getEdge(int uu, int vv)//返回某条边
{
static Edge xx;
xx.setEdgeWeight(0, INFINITEWEIGHT);
Point &node = getNode(uu);
if (checkNode(uu) == INVALID)//point uu not exist
{
return xx;//invalid edge
}
else//point uu exist
{
vector<Edge> &edgeVector = node.getEdgeVector();
char flag = 0;
for (auto iter = edgeVector.begin(); iter != edgeVector.end(); ++iter)
{
if (((*iter).getNodeTwo() == vv))//edge (uu,vv) have existed
{
return (*iter);
}
}
//edge (uu,vv) not exist
{
return xx;//invalid edge
}
}
}
int GraphVector::insertEdge(int edgeId, int uu, int vv, int edgeWeight)//time complexity: O(8)
{
Point &node = getNode(uu);
if ((checkNode(uu) == INVALID) || (node.getEdgeVector().empty()))//point uu not exist or don't have edge out
{
node.setPointId(uu);//设置点的属性
//建立新边,设置新边的属性
Edge edge;
edge.setEdgeId(0, edgeId);
edge.setNodeOne(uu);
edge.setNodeTwo(vv);
edge.setEdgeWeight(0, edgeWeight);
node.insertEdge(edge);//insert new edge at the end of edgevector
}
else//point uu exist
{
#if 1
//save edge without repeated (uu,vv)
char flag = 0;
vector<Edge> &edgeVector = node.getEdgeVector();
//search, see whether edge (uu,vv) exists
for (auto iter = edgeVector.begin(); iter != edgeVector.end(); ++iter)
{
//edge (uu,vv) have existed
if (((*iter).getNodeTwo() == vv))
{
//if have existed 2 edge (uu,vv)
if ((*iter).getEdgeId(1) != INVALID)
{
//if edge weight is smaller, replace
if (edgeWeight < ((*iter).getEdgeWeight(0)))
{
(*iter).setEdgeWeight(0, edgeWeight);
(*iter).setEdgeId(0, edgeId);
}
else if (edgeWeight < ((*iter).getEdgeWeight(1)))
{
(*iter).setEdgeWeight(1, edgeWeight);
(*iter).setEdgeId(1, edgeId);
}
}
else//only existed 1 edge (uu,vv),insert to edge
{
(*iter).setEdgeWeight(1, edgeWeight);
(*iter).setEdgeId(1, edgeId);
}
flag = 1;
break;
}
}
if (!flag)//edge (uu,vv) not exist
{
Edge edge;
edge.setEdgeId(0,edgeId);
edge.setNodeOne(uu);
edge.setNodeTwo(vv);
edge.setEdgeWeight(0,edgeWeight);
node.insertEdge(edge);//insert new edge at the end of edgevector
}
#else
//save edge with repeated (uu,vv)
Edge edge;
edge.setEdgeId(edgeId);
edge.setNodeOne(uu);
edge.setNodeTwo(vv);
edge.setEdgeWeight(edgeWeight);
node.insertEdge(edge);//insert new edge at the end of edgevector
#endif
}
if (checkNode(vv) == INVALID)//point vv not exist
{
Point &nodeVv = getNode(vv);
//设置点的属性
nodeVv.setPointId(vv);
}
return EXIT_SUCCESS;
}
//检查node是否存在
inline int GraphVector::checkNode(int uu)
{
if (getNode(uu).getPointId() == POINT_INVALID) return INVALID;
else return VALID;
}
// time complexity : O(V + E),so : O(4800)
int GraphVector::bfs(int firstNode, int *color)
{
queue<int> pointQueue;
//initial start point
color[firstNode] = GRAY;
//push first point to queue
pointQueue.push(firstNode);
while (!pointQueue.empty())//time complexity:O(V+E), actually less than this
{
int uu = pointQueue.front();
pointQueue.pop();
vector<Edge> &edgeVector = getNode(uu).getEdgeVector();
for (auto iter = edgeVector.begin(); iter != edgeVector.end(); ++iter)//traverse every edge of point uu
{
int vv = (*iter).getNodeTwo();
Point &pointTemp = getNode(vv);
//(not valid) || (has visited)
if ((color[vv] != WHITE) || (pointTemp.getPointId() == POINT_INVALID)) continue;
//the rest can satisfy needs
color[vv] = GRAY;
pointQueue.push(vv);
}
color[uu] = BLACK;
}
return EXIT_SUCCESS;
}
int GraphVector::refreshInoutDegree()//刷新每个有效点的出入度属性,time complexity:O(V+E)
{
int degreeCnt[MAXPOINTNUM];
//initial memory
memset(degreeCnt, 0, MAXPOINTNUM*sizeof(int));
//traverse every edge of graph
for (int i = 0; i <= MAXNODEID; ++i)
{
vector<Edge> &edgeVector = getNode(i).getEdgeVector();
for (auto iter = edgeVector.begin(); iter != edgeVector.end(); ++iter)
{
degreeCnt[(*iter).getNodeOne()]++;
degreeCnt[(*iter).getNodeTwo()]++;
}
}
//traverse every node of graph,set inoutdegree
for (int i = 0; i <= MAXNODEID; ++i)
{
getNode(i).setInoutDegree(degreeCnt[i]);
}
return EXIT_SUCCESS;
}