-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
320 lines (293 loc) · 7.96 KB
/
Graph.cpp
File metadata and controls
320 lines (293 loc) · 7.96 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
#include "Graph.h"
#include "DijkstraQueue.h"
#include <queue>
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
//Read the graph from the file and create the graph object
Graph::Graph(string file, bool hlQueue)
{
cout << endl << "Creating graph from file " << file <<
". Using " << (hlQueue ? "HL" : "FIFO") << " queue method" << endl;
readGraph(file);
if (hlQueue)
pool = new HighLabelQueue(nodesNum);
else
pool = new FifoQueue();
}
//Graph destructor
Graph::~Graph(void)
{
free(pool);
free(nodeArray);
free(prevArray);
}
//Takes a string and eliminates consecutive spaces and leading spaces
void stripSpaces(string & str)
{
int startpos = str.find_first_not_of(" \t");
int endpos = str.find_last_not_of(" \t");
if (startpos >= endpos)
{
str = "";
}
else
str = str.substr( startpos, endpos-startpos+1 );
startpos = str.find_first_of(" \t");
endpos = str.find_first_not_of(" \t", startpos);
while ((string::npos != startpos) && (string::npos != endpos))
{
if (endpos - 1 > startpos){
str.erase(startpos + 1, endpos - 1 - startpos);
}
startpos = str.find_first_of(" \t", startpos + 1);
endpos = str.find_first_not_of(" \t", startpos);
}
}
//Read the graph from the file
//File is assumed to be in the DIMACS format
int Graph::readGraph(string file)
{
ifstream graphFile;
char c;
int edges, degree, vlabel, elabel, adjNode, i, j;
int pos, pos2;
int xcoord, ycoord;
int sourceId;
int sinkId;
string line;
EdgeEntry *edge, *reverse;
//Empty file name
if (file.length() == 0)
{
cerr << "readGraph: File name must exist" << endl;
exit(1);
}
else //Try to open the file
graphFile.open (file.c_str());
if (graphFile.is_open())
{
char ch;
int nodeId, to, cap;
int numEdges, curNode;
char* str = new char[10];
//Read the graph line by line and parse each line
while (! graphFile.eof() )
{
getline (graphFile, line);
stripSpaces(line);
if (DEBUG >= LOG_3)
cout << line << " " << line[0] << endl;
if (line.length() == 0) //Ignore empty lines
continue;
switch (line.at(0))
{
//Comments lines - ignore
case 'c':
case 's':
break;
//Source and target
case 'n':
ch = line[line.find_last_of(" ") + 1];
nodeId = atoi(line.substr(line.find_first_of(" "), line.find_last_of(" ")).c_str());
if (DEBUG >= LOG_1)
cout << ch << " " << nodeId << endl;
if (ch == 's')
sourceID = nodeId;
else if (ch == 't')
targetID = nodeId;
else
{
cerr << "readGraph: Bad Format!" << endl;
return 1;
}
break;
//Number of edges and nodes
case 'p':
pos = line.find_first_of(" ");
pos = line.find_first_of(" ", pos + 1);
pos2 = line.find_first_of(" ", pos + 1);
nodesNum = atoi(line.substr(pos, pos2).c_str());
pos = pos2;
pos2 = line.find_first_of(" ", pos + 1);
edgesNum = atoi(line.substr(pos, line.length()).c_str());
if (DEBUG >= LOG_1)
cout << nodesNum << " " << edgesNum << endl;
//Create the node array
nodeArray = new Node[nodesNum + 1]; //Adding 1 to conform with nodes in graph file
//The previous array is used for the dijkstra calculations
prevArray = new int[nodesNum + 1];
//Set the ID for each node
for (int i = 0; i <= nodesNum ; i++)
nodeArray[i].setID(i);
break;
//An edge
case 'a':
pos = line.find_first_of(" ");
pos2 = line.find_first_of(" ", pos + 1);
nodeId = atoi(line.substr(pos, pos2).c_str());
pos = pos2;
pos2 = line.find_first_of(" ", pos + 1);
to = atoi(line.substr(pos, pos2).c_str());
pos = pos2;
pos2 = line.find_first_of(" ", pos + 1);
cap = atoi(line.substr(pos, line.length()).c_str());
//Add the edges
edge = new EdgeEntry(to, cap, 0,
nodeArray[nodeId].getLastEdge());
reverse = new EdgeEntry(nodeId, cap, cap,
nodeArray[to].getLastEdge());
reverse->setReversed(edge);
edge->setReversed(reverse);
//Add the edges to the node edge list
nodeArray[nodeId].addEdge(edge);
nodeArray[to].addEdge(reverse);
}
}
//Finished reading the file - close it
graphFile.close();
}
else
{
cout << "readGraph: Unable to open file";
return 1;
}
return 0;
}
//Dijkstra's algorithms implemnted, find distances from 'source_id'
//run until you reach the other side (source->target, target->source)
//Save the path in the 'prev' array, max is used to save the max flow available
//on the path
int Graph::dijkstra(int source_id, int* &dist, int* &prev, int &max)
{
int i, tmp_dist;
Node *u, tmp_node, *relocation_node;
EdgeEntry* edge_ptr = NULL;
//The DijkstraQueue class is used here as the priority queue
DijkstraQueue *pool = new DijkstraQueue(nodesNum+2);
bool reverse = (source_id == targetID);
max = INFINITY;
//Reset the dist array for max distance
for (i=1; i<nodesNum+1; i++)
dist[i] = nodesNum;
//Init the source distance
dist[source_id] = 0;
//Add all the nodes to the dijkstra queue
for (i=1; i<nodesNum+1; i++)
{
pool->addNode(&nodeArray[i],dist[i]);
}
//The main loop
while (!pool->isEmpty())
{
//Get the node with lowest distance
u = pool->getNode();
if (u->getID() == targetID)
break;
//Get all the edges
edge_ptr = u->getAdjList();
if (edge_ptr != NULL)
edge_ptr = edge_ptr->getNext(); //1st call skips dummy
//Check all edges
while (edge_ptr != NULL)
{
if (!reverse)
{
if (edge_ptr->isReverseEdge())
{
edge_ptr = edge_ptr->getNext();
continue;
}
else if (edge_ptr->getResCapacity() == 0)
{
edge_ptr = edge_ptr->getNext();
continue;
}
}
else //reverse
{
if (!edge_ptr->isReverseEdge())
{
edge_ptr = edge_ptr->getNext();
continue;
}
else if (edge_ptr->getFlow() == 0)
{
edge_ptr = edge_ptr->getNext();
continue;
}
}
tmp_node = nodeArray[edge_ptr->getEndPoint()];
tmp_dist = dist[u->getID()] + 1;
//If we found a shorter path we update the node
if (dist[tmp_node.getID()] > tmp_dist)
{
//Get the node to update from the queue
relocation_node = pool->getNode(dist[tmp_node.getID()],tmp_node.getID());
dist[tmp_node.getID()] = tmp_dist;
prev[tmp_node.getID()] = u->getID();
//Insert the node in its new place
pool->addNode(relocation_node,tmp_dist);
}
//Move to the next edge
edge_ptr = edge_ptr->getNext();
}
}
//Now that the 'prev' array is ready, we can find the max flow available on
//the path between the source and the sink
int t = targetID;
EdgeEntry *tmp_edge;
while(t != sourceID)
{
tmp_edge = nodeArray[prevArray[t]].getAdjList();
while (tmp_edge->getEndPoint() != t)
tmp_edge = tmp_edge->getNext();
if (tmp_edge->getResCapacity() < max)
max = tmp_edge->getResCapacity();
t = prevArray[t];
}
//Free the pool
free(pool);
return 0;
}
//Print the graph
int Graph::printGraph(string file)
{
ofstream output;
output.open(file.c_str());
output << "Source ID: " << sourceID << ", Target ID: " << targetID << endl;
for (int i = 0; i < nodesNum; i++)
{
nodeArray[i].printNode(output);
}
output.close();
return 0;
}
//Show debug information
int Graph::debugDump()
{
cout << "src: " << sourceID << ", dst: " << targetID << endl;
for (int i = 0; i < nodesNum; i++)
{
nodeArray[i].debugNodeDump();
}
cout << "Debug end" << endl;
return 0;
}
//Increase the capacity of path from source to sink by 'val'
int Graph::incPrevPathCap(int val)
{
int i = targetID;
EdgeEntry *tmp_edge;
while(i != sourceID)
{
tmp_edge = nodeArray[prevArray[i]].getAdjList();
while (tmp_edge->getEndPoint() != i)
tmp_edge = tmp_edge->getNext();
tmp_edge->incCapacity(val);
tmp_edge->getReverse()->incCapacity(val);
i = prevArray[i];
}
return 0;
}