gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Fix in HaoOrlin (#264)
0 2 0
default
2 files changed with 14 insertions and 24 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -181,96 +181,97 @@
181 181
        _last[bucket] = (*_prev)[i];
182 182
      }
183 183
      //lace
184 184
      (*_next)[i] = _first[bucket];
185 185
      (*_prev)[_first[bucket]] = i;
186 186
      (*_prev)[i] = INVALID;
187 187
      _first[bucket] = i;
188 188
    }
189 189

	
190 190
    void deactivate(const Node& i) {
191 191
      (*_active)[i] = false;
192 192
      int bucket = (*_bucket)[i];
193 193

	
194 194
      if ((*_next)[i] == INVALID || !(*_active)[(*_next)[i]]) return;
195 195

	
196 196
      //unlace
197 197
      (*_prev)[(*_next)[i]] = (*_prev)[i];
198 198
      if ((*_prev)[i] != INVALID) {
199 199
        (*_next)[(*_prev)[i]] = (*_next)[i];
200 200
      } else {
201 201
        _first[bucket] = (*_next)[i];
202 202
      }
203 203
      //lace
204 204
      (*_prev)[i] = _last[bucket];
205 205
      (*_next)[_last[bucket]] = i;
206 206
      (*_next)[i] = INVALID;
207 207
      _last[bucket] = i;
208 208
    }
209 209

	
210 210
    void addItem(const Node& i, int bucket) {
211 211
      (*_bucket)[i] = bucket;
212 212
      if (_last[bucket] != INVALID) {
213 213
        (*_prev)[i] = _last[bucket];
214 214
        (*_next)[_last[bucket]] = i;
215 215
        (*_next)[i] = INVALID;
216 216
        _last[bucket] = i;
217 217
      } else {
218 218
        (*_prev)[i] = INVALID;
219 219
        _first[bucket] = i;
220 220
        (*_next)[i] = INVALID;
221 221
        _last[bucket] = i;
222 222
      }
223 223
    }
224 224

	
225 225
    void findMinCutOut() {
226 226

	
227 227
      for (NodeIt n(_graph); n != INVALID; ++n) {
228 228
        (*_excess)[n] = 0;
229
        (*_source_set)[n] = false;
229 230
      }
230 231

	
231 232
      for (ArcIt a(_graph); a != INVALID; ++a) {
232 233
        (*_flow)[a] = 0;
233 234
      }
234 235

	
235 236
      int bucket_num = 0;
236 237
      std::vector<Node> queue(_node_num);
237 238
      int qfirst = 0, qlast = 0, qsep = 0;
238 239

	
239 240
      {
240 241
        typename Digraph::template NodeMap<bool> reached(_graph, false);
241 242

	
242 243
        reached[_source] = true;
243 244
        bool first_set = true;
244 245

	
245 246
        for (NodeIt t(_graph); t != INVALID; ++t) {
246 247
          if (reached[t]) continue;
247 248
          _sets.push_front(std::list<int>());
248 249

	
249 250
          queue[qlast++] = t;
250 251
          reached[t] = true;
251 252

	
252 253
          while (qfirst != qlast) {
253 254
            if (qsep == qfirst) {
254 255
              ++bucket_num;
255 256
              _sets.front().push_front(bucket_num);
256 257
              _dormant[bucket_num] = !first_set;
257 258
              _first[bucket_num] = _last[bucket_num] = INVALID;
258 259
              qsep = qlast;
259 260
            }
260 261

	
261 262
            Node n = queue[qfirst++];
262 263
            addItem(n, bucket_num);
263 264

	
264 265
            for (InArcIt a(_graph, n); a != INVALID; ++a) {
265 266
              Node u = _graph.source(a);
266 267
              if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
267 268
                reached[u] = true;
268 269
                queue[qlast++] = u;
269 270
              }
270 271
            }
271 272
          }
272 273
          first_set = false;
273 274
        }
274 275

	
275 276
        ++bucket_num;
276 277
        (*_bucket)[_source] = 0;
... ...
@@ -480,96 +481,97 @@
480 481
              }
481 482
            }
482 483
            new_target = _last[_sets.back().back()];
483 484
          }
484 485

	
485 486
          (*_bucket)[target] = 0;
486 487

	
487 488
          (*_source_set)[target] = true;
488 489
          for (OutArcIt a(_graph, target); a != INVALID; ++a) {
489 490
            Value rem = (*_capacity)[a] - (*_flow)[a];
490 491
            if (!_tolerance.positive(rem)) continue;
491 492
            Node v = _graph.target(a);
492 493
            if (!(*_active)[v] && !(*_source_set)[v]) {
493 494
              activate(v);
494 495
            }
495 496
            (*_excess)[v] += rem;
496 497
            (*_flow)[a] = (*_capacity)[a];
497 498
          }
498 499

	
499 500
          for (InArcIt a(_graph, target); a != INVALID; ++a) {
500 501
            Value rem = (*_flow)[a];
501 502
            if (!_tolerance.positive(rem)) continue;
502 503
            Node v = _graph.source(a);
503 504
            if (!(*_active)[v] && !(*_source_set)[v]) {
504 505
              activate(v);
505 506
            }
506 507
            (*_excess)[v] += rem;
507 508
            (*_flow)[a] = 0;
508 509
          }
509 510

	
510 511
          target = new_target;
511 512
          if ((*_active)[target]) {
512 513
            deactivate(target);
513 514
          }
514 515

	
515 516
          _highest = _sets.back().begin();
516 517
          while (_highest != _sets.back().end() &&
517 518
                 !(*_active)[_first[*_highest]]) {
518 519
            ++_highest;
519 520
          }
520 521
        }
521 522
      }
522 523
    }
523 524

	
524 525
    void findMinCutIn() {
525 526

	
526 527
      for (NodeIt n(_graph); n != INVALID; ++n) {
527 528
        (*_excess)[n] = 0;
529
        (*_source_set)[n] = false;
528 530
      }
529 531

	
530 532
      for (ArcIt a(_graph); a != INVALID; ++a) {
531 533
        (*_flow)[a] = 0;
532 534
      }
533 535

	
534 536
      int bucket_num = 0;
535 537
      std::vector<Node> queue(_node_num);
536 538
      int qfirst = 0, qlast = 0, qsep = 0;
537 539

	
538 540
      {
539 541
        typename Digraph::template NodeMap<bool> reached(_graph, false);
540 542

	
541 543
        reached[_source] = true;
542 544

	
543 545
        bool first_set = true;
544 546

	
545 547
        for (NodeIt t(_graph); t != INVALID; ++t) {
546 548
          if (reached[t]) continue;
547 549
          _sets.push_front(std::list<int>());
548 550

	
549 551
          queue[qlast++] = t;
550 552
          reached[t] = true;
551 553

	
552 554
          while (qfirst != qlast) {
553 555
            if (qsep == qfirst) {
554 556
              ++bucket_num;
555 557
              _sets.front().push_front(bucket_num);
556 558
              _dormant[bucket_num] = !first_set;
557 559
              _first[bucket_num] = _last[bucket_num] = INVALID;
558 560
              qsep = qlast;
559 561
            }
560 562

	
561 563
            Node n = queue[qfirst++];
562 564
            addItem(n, bucket_num);
563 565

	
564 566
            for (OutArcIt a(_graph, n); a != INVALID; ++a) {
565 567
              Node u = _graph.target(a);
566 568
              if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
567 569
                reached[u] = true;
568 570
                queue[qlast++] = u;
569 571
              }
570 572
            }
571 573
          }
572 574
          first_set = false;
573 575
        }
574 576

	
575 577
        ++bucket_num;
Ignore white space 6 line context
... ...
@@ -66,110 +66,98 @@
66 66
  CapMap cap;
67 67
  CutMap cut;
68 68
  Value v;
69 69

	
70 70
  HaoOrlin<Digraph, CapMap> ho_test(g, cap);
71 71
  const HaoOrlin<Digraph, CapMap>&
72 72
    const_ho_test = ho_test;
73 73

	
74 74
  ho_test.init();
75 75
  ho_test.init(n);
76 76
  ho_test.calculateOut();
77 77
  ho_test.calculateIn();
78 78
  ho_test.run();
79 79
  ho_test.run(n);
80 80

	
81 81
  v = const_ho_test.minCutValue();
82 82
  v = const_ho_test.minCutMap(cut);
83 83
}
84 84

	
85 85
template <typename Graph, typename CapMap, typename CutMap>
86 86
typename CapMap::Value 
87 87
  cutValue(const Graph& graph, const CapMap& cap, const CutMap& cut)
88 88
{
89 89
  typename CapMap::Value sum = 0;
90 90
  for (typename Graph::ArcIt a(graph); a != INVALID; ++a) {
91 91
    if (cut[graph.source(a)] && !cut[graph.target(a)])
92 92
      sum += cap[a];
93 93
  }
94 94
  return sum;
95 95
}
96 96

	
97 97
int main() {
98 98
  SmartDigraph graph;
99 99
  SmartDigraph::ArcMap<int> cap1(graph), cap2(graph), cap3(graph);
100 100
  SmartDigraph::NodeMap<bool> cut(graph);
101 101

	
102 102
  istringstream input(lgf);
103 103
  digraphReader(graph, input)
104 104
    .arcMap("cap1", cap1)
105 105
    .arcMap("cap2", cap2)
106 106
    .arcMap("cap3", cap3)
107 107
    .run();
108 108

	
109 109
  {
110 110
    HaoOrlin<SmartDigraph> ho(graph, cap1);
111 111
    ho.run();
112 112
    ho.minCutMap(cut);
113 113
    
114
    // BUG: The cut value should be positive
115
    check(ho.minCutValue() == 0, "Wrong cut value");
116
    // BUG: It should work
117
    //check(ho.minCutValue() == cutValue(graph, cap1, cut), "Wrong cut value");
114
    check(ho.minCutValue() == 1, "Wrong cut value");
115
    check(ho.minCutValue() == cutValue(graph, cap1, cut), "Wrong cut value");
118 116
  }
119 117
  {
120 118
    HaoOrlin<SmartDigraph> ho(graph, cap2);
121 119
    ho.run();
122 120
    ho.minCutMap(cut);
123
    
124
    // BUG: The cut value should be positive
125
    check(ho.minCutValue() == 0, "Wrong cut value");
126
    // BUG: It should work
127
    //check(ho.minCutValue() == cutValue(graph, cap2, cut), "Wrong cut value");
121

	
122
    check(ho.minCutValue() == 1, "Wrong cut value");
123
    check(ho.minCutValue() == cutValue(graph, cap2, cut), "Wrong cut value");
128 124
  }
129 125
  {
130 126
    HaoOrlin<SmartDigraph> ho(graph, cap3);
131 127
    ho.run();
132 128
    ho.minCutMap(cut);
133 129
    
134
    // BUG: The cut value should be positive
135
    check(ho.minCutValue() == 0, "Wrong cut value");
136
    // BUG: It should work
137
    //check(ho.minCutValue() == cutValue(graph, cap3, cut), "Wrong cut value");
130
    check(ho.minCutValue() == 1, "Wrong cut value");
131
    check(ho.minCutValue() == cutValue(graph, cap3, cut), "Wrong cut value");
138 132
  }
139 133
  
140 134
  typedef Undirector<SmartDigraph> UGraph;
141 135
  UGraph ugraph(graph);
142 136
  
143 137
  {
144 138
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap1);
145 139
    ho.run();
146 140
    ho.minCutMap(cut);
147 141
    
148
    // BUG: The cut value should be 2
149
    check(ho.minCutValue() == 1, "Wrong cut value");
150
    // BUG: It should work
151
    //check(ho.minCutValue() == cutValue(ugraph, cap1, cut), "Wrong cut value");
142
    check(ho.minCutValue() == 2, "Wrong cut value");
143
    check(ho.minCutValue() == cutValue(ugraph, cap1, cut), "Wrong cut value");
152 144
  }
153 145
  {
154 146
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap2);
155 147
    ho.run();
156 148
    ho.minCutMap(cut);
157 149
    
158
    // TODO: Check this cut value
159
    check(ho.minCutValue() == 4, "Wrong cut value");
160
    // BUG: It should work
161
    //check(ho.minCutValue() == cutValue(ugraph, cap2, cut), "Wrong cut value");
150
    check(ho.minCutValue() == 5, "Wrong cut value");
151
    check(ho.minCutValue() == cutValue(ugraph, cap2, cut), "Wrong cut value");
162 152
  }
163 153
  {
164 154
    HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap3);
165 155
    ho.run();
166 156
    ho.minCutMap(cut);
167 157
    
168
    // TODO: Check this cut value
169 158
    check(ho.minCutValue() == 5, "Wrong cut value");
170
    // BUG: It should work
171
    //check(ho.minCutValue() == cutValue(ugraph, cap3, cut), "Wrong cut value");
159
    check(ho.minCutValue() == cutValue(ugraph, cap3, cut), "Wrong cut value");
172 160
  }
173 161

	
174 162
  return 0;
175 163
}
0 comments (0 inline)