-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
213 lines (170 loc) · 6.19 KB
/
main.cpp
File metadata and controls
213 lines (170 loc) · 6.19 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
#include <iostream>
#include <limits.h>
#include <queue>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <time.h>
#include <cstring>
using namespace std;
/* Returns true if there is a path from source 's' to sink
't' in residual graph. Also fills parent[] to store the
path */
bool bfs(vector<vector<int> > rGraph, int s, int t, int parent[], int nodes)
{
// Create a visited array and mark all vertices as not
// visited
bool visited[nodes];
memset(visited, 0, sizeof(visited));
// Create a queue, enqueue source vertex and mark source
// vertex as visited
queue<int> q;
q.push(s);
visited[s] = true;
parent[s] = -1;
// Standard BFS Loop
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v = 0; v < nodes; v++) {
if (visited[v] == false && rGraph[u][v] > 0) {
// If we find a connection to the sink node,
// then there is no point in BFS anymore We
// just have to set its parent and can return
// true
if (v == t) {
parent[v] = u;
return true;
}
q.push(v);
parent[v] = u;
visited[v] = true;
}
}
}
// We didn't reach sink in BFS starting from source, so
// return false
return false;
}
// Returns the maximum flow from s to t in the given graph
int fordFulkerson(vector<vector<int> > graph, int s, int t, int nodes)
{
int u, v;
// Create a residual graph and fill the residual graph
// with given capacities in the original graph as
// residual capacities in residual graph
vector<vector<int> > rGraph(nodes, vector<int> (nodes, 0));
for (u = 0; u < nodes; u++){
for (v = 0; v < nodes; v++)
{
// cout << "graph[u][v]: " << graph[u][v] << endl;
rGraph[u][v] = graph[u][v];
}
}
int parent[nodes]; // This array is filled by BFS and to
// store path
int max_flow = 0; // There is no flow initially
// Augment the flow while there is path from source to
// sink
while (bfs(rGraph, s, t, parent, nodes)) {
// Find minimum residual capacity of the edges along
// the path filled by BFS. Or we can say find the
// maximum flow through the path found.
int path_flow = INT_MAX;
for (v = t; v != s; v = parent[v]) {
u = parent[v];
path_flow = min(path_flow, rGraph[u][v]);
}
// update residual capacities of the edges and
// reverse edges along the path
for (v = t; v != s; v = parent[v]) {
u = parent[v];
rGraph[u][v] -= path_flow;
rGraph[v][u] += path_flow;
}
// Add path flow to overall flow
max_flow += path_flow;
// cout << "max_flow: " << max_flow << endl;
}
// Return the overall flow
return max_flow;
}
double interval(struct timespec start, struct timespec end){
struct timespec temp;
temp.tv_sec = end.tv_sec - start.tv_sec;
temp.tv_nsec = end.tv_nsec - start.tv_nsec;
if (temp.tv_nsec < 0) {
temp.tv_sec = temp.tv_sec - 1;
temp.tv_nsec = temp.tv_nsec + 1000000000;
}
return (((double)temp.tv_sec) + ((double)temp.tv_nsec)*1.0e-9);
}
int main()
{
struct timespec start, end;
double timeElapsed;
// ifstream input_file;
string files[10] = {"BostonCommonDirected.csv",
"BostonCommonUndirected.csv",
"BostonCommonUndirectedUnitCap.csv",
"BostonCommonDirectedUnitCap.csv",
"BostonCommonConsolidatedUndirected.csv",
"BostonCommonConsolidatedDirected.csv",
"flowBU.csv",
"flowNetDense.csv",
"flowNetMidBlob.csv",
"denseGraph.csv"};
FILE* cFile;
cFile = fopen ("output/RuntimeData.txt","w+");
fprintf(cFile,"Ford-Fulkerson Algorithm\n");
fprintf(cFile,"------------------------\n");
fprintf(cFile," Nodes | Edges | Flow | Time | File \n");
for (int i = 0; i < 10; i++)
{
ifstream input_file("input/" + files[i]);
string nodes_str, edges_str;
int max_nodes, max_edges, actual_num_nodes;
string line;
getline(input_file, line);
stringstream ss(line);
ss >> nodes_str >> actual_num_nodes >> edges_str >> max_edges;
fprintf(cFile,"%9.4d %13.3d ", actual_num_nodes, max_edges);
string source, sink;
int source_int, sink_int;
getline(input_file, line);
stringstream ss2(line);
ss2 >> source >> source_int >> sink >> sink_int;
max_nodes = sink_int;
// cout << "source: " << source_int << endl;
// cout << "sink: " << sink_int << endl;
source_int -= 1;
sink_int -= 1;
vector<vector<int> > network(max_nodes, vector<int> (max_nodes, 0));
while (getline(input_file, line)) {
int src, dst, capacity;
stringstream ss(line);
string cell;
if (getline(ss, cell, ','))
src = stoi(cell); // parse first number
if (getline(ss, cell, ','))
dst = stoi(cell); // parse second number
if (getline(ss, cell, ','))
capacity = stoi(cell); // parse third number
network[src-1][dst-1] = capacity;
}
input_file.close();
clock_gettime(CLOCK_MONOTONIC, &start);
// cout << "The maximum possible flow is "
int max_flow = fordFulkerson(network, source_int, sink_int, max_nodes);
fprintf(cFile, "%15.3d ", max_flow);
// << endl;
clock_gettime(CLOCK_MONOTONIC, &end);
timeElapsed = interval(start, end);
fprintf(cFile, "%16.3e ms\t%s\n", timeElapsed, files[i].c_str());
//cout << " " << timeElapsed << " ms";
//cout << " " << files[i] << endl;
network.clear();
}
return 0;
}