-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCycle.cpp
253 lines (237 loc) · 5.7 KB
/
Cycle.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
#include "Cycle.h"
Cycle::Cycle(vector<int> _capacities, vector<map<int, int>> _directed)
{
n = _directed.size();
capacities = _capacities;
directed = _directed;
for (int i = 0; i < n; i++)
reversedEdges.push_back(map<int, int>());
for (int i = 0; i < n; i++)
{
for (map<int, int>::iterator itr = directed[i].begin(); itr != directed[i].end(); itr++)
{
reversedEdges[itr->first].insert(pair<int, int>(i, itr->second));
}
}
}
void Cycle::Merge(int n1, int n2, int _capacity)
{
int NewVertex = n1;
int MergedVertices = n2;
if (n1 > n2)
{
NewVertex = n2;
MergedVertices = n1;
}
// 1- remove edges between merged vertices
directed[n1].erase(n2);
directed[n2].erase(n1);
reversedEdges[n1].erase(n2);
reversedEdges[n2].erase(n1);
//2- Move all edges from merged vertices to the first merged vertex
for (map<int, int>::iterator itr = directed[MergedVertices].begin(); itr != directed[MergedVertices].end(); itr++)
{
if (directed[NewVertex].find(itr->first) == directed[NewVertex].end())
directed[NewVertex].insert(*itr);
else
{
if(directed[NewVertex][itr->first] > itr->second)
directed[NewVertex][itr->first] = itr->second;
}
}
directed[MergedVertices].clear();
// 2b- Calculate the capacity of the macro-vertex
capacities[NewVertex] = _capacity;
// 3- modify all edges with the new vertex info
for (int i = 0; i < n; i++)
{
if (directed[i].find(MergedVertices) != directed[i].end())
{
if (directed[i].find(NewVertex) == directed[i].end())
directed[i].insert(pair<int, int>(NewVertex, directed[i][MergedVertices]));
else
{
if (directed[i][NewVertex] > directed[i][MergedVertices])
directed[i][NewVertex] = directed[i][MergedVertices];
}
directed[i].erase(MergedVertices);
}
}
// 4-
for (int i = MergedVertices + 1; i < n; i++)
{
directed[i - 1] = directed[i];
capacities[i - 1] = capacities[i];
}
directed.pop_back();
capacities.pop_back();
n--;
// 5- modify the edges according to the new order
for (int itr = MergedVertices + 1; itr <= n; itr++)
{
for (int i = 0; i < n; i++)
{
if (directed[i].find(itr) != directed[i].end())
{
directed[i].insert(pair<int, int>(itr - 1, directed[i][itr]));
directed[i].erase(itr);
}
}
}
// 6- update the remaing of the parameters
reversedEdges.clear();
for (int i = 0; i < n; i++)
reversedEdges.push_back(map<int, int>());
for (int i = 0; i < n; i++)
{
for (map<int, int>::iterator itr = directed[i].begin(); itr != directed[i].end(); itr++)
{
reversedEdges[itr->first].insert(pair<int, int>(i, itr->second));
}
}
}
bool Cycle::PerformFeasibleMergers(bool direction)
{
bool AnyFeasible = false;
for (int i = 0; i < n; i++)
{
int next = 0;
if (direction)
next = (i + 1) % n;
else
next = (n + i - 1) % n;
if (directed[i].find(next) != directed[i].end())
{
if (directed[i][next] <= capacities[next])
{
AnyFeasible = true;
break;
}
}
}
if (!AnyFeasible)
return false;
bool change = true;
while (change)
{
change = false;
for (int i = 0; i < n; i++)
{
int next = 0;
if (direction)
next = (i + 1) % n;
else
next = (n + i - 1) % n;
if (directed[i].find(next) != directed[i].end())
{
if (directed[i][next] <= capacities[next])
{
int capacity = capacities[i] + capacities[next] - directed[i][next];
if ((capacities[i] == INFINITY) || (capacities[next] == INFINITY))
capacity = INFINITY;
Merge(i, next, capacity);
change = true;
break;
}
}
}
}
return true;
}
bool Cycle::PerformProducerMergers(bool direction)
{
bool AnyProducer = false;
for (int i = 0; i < n; i++)
{
int next = 0;
if (direction)
next = (i + 1) % n;
else
next = (n + i - 1) % n;
if (directed[i].find(next) != directed[i].end())
{
if (directed[i][next] <= capacities[next])
{
if ((capacities[i] >= directed[i][next]) || (capacities[i] == INFINITY))
{
AnyProducer = true;
break;
}
}
}
}
if (!AnyProducer)
return false;
bool change = true;
while (change)
{
change = false;
for (int i = 0; i < n; i++)
{
int next = 0;
if (direction)
next = (i + 1) % n;
else
next = (n + i - 1) % n;
if (directed[i].find(next) != directed[i].end())
{
if (directed[i][next] <= capacities[next])
{
if (capacities[i] >= directed[i][next])
{
int capacity = capacities[i] + capacities[next] - directed[i][next];
Merge(i, next, capacity);
change = true;
break;
}
else if (capacities[i] == INFINITY)
{
int capacity = INFINITY;
Merge(i, next, capacity);
change = true;
break;
}
}
}
}
}
return true;
}
bool Cycle::AllEdgesInOneDirection()
{
for (int i = 0; i < n; i++)
if (directed[i].size() != 1)
return false;
return true;
}
bool Cycle::IsCollapsable(bool direction)
{
if (n == 1)
return true;
bool change = true;
while (change)
{
change = false;
change = change || PerformFeasibleMergers(direction);
change = change || PerformProducerMergers(!direction);
if (n == 1)
return true;
if (AllEdgesInOneDirection())
return true;
}
return false;
}
bool Cycle::IsProducer()
{
int temp_n = n;
vector<int> temp_capacities = capacities;
vector<map<int, int>> temp_directed = directed;
vector<map<int, int>> temp_reversedEdges = reversedEdges;
if (IsCollapsable(true))
return true;
n = temp_n;
capacities = temp_capacities;
directed = temp_directed;
reversedEdges = temp_reversedEdges;
return IsCollapsable(false);
}