Location: LEMON/LEMON-main/lemon/gomory_hu.h - annotation
Load file history
Merge #302
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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r546:d6b40ebb2617 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r546:d6b40ebb2617 r546:d6b40ebb2617 r546:d6b40ebb2617 r545:e72bacfea6b7 r546:d6b40ebb2617 r596:293551ad254f r596:293551ad254f r546:d6b40ebb2617 r545:e72bacfea6b7 r546:d6b40ebb2617 r546:d6b40ebb2617 r546:d6b40ebb2617 r546:d6b40ebb2617 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r545:e72bacfea6b7 r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r546:d6b40ebb2617 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r546:d6b40ebb2617 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r581:aa1804409f29 r581:aa1804409f29 r545:e72bacfea6b7 r581:aa1804409f29 r581:aa1804409f29 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r581:aa1804409f29 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r581:aa1804409f29 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r581:aa1804409f29 r581:aa1804409f29 r581:aa1804409f29 r581:aa1804409f29 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r581:aa1804409f29 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r581:aa1804409f29 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r546:d6b40ebb2617 r545:e72bacfea6b7 r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r545:e72bacfea6b7 r596:293551ad254f r545:e72bacfea6b7 r596:293551ad254f r545:e72bacfea6b7 r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r581:aa1804409f29 r545:e72bacfea6b7 r581:aa1804409f29 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r713:4ac30454f1c1 r545:e72bacfea6b7 r546:d6b40ebb2617 r713:4ac30454f1c1 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r546:d6b40ebb2617 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r713:4ac30454f1c1 r545:e72bacfea6b7 r545:e72bacfea6b7 r713:4ac30454f1c1 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r596:293551ad254f r596:293551ad254f r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r546:d6b40ebb2617 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r596:293551ad254f r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r546:d6b40ebb2617 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 r545:e72bacfea6b7 | /* -*- C++ -*-
*
* This file is a part of LEMON, a generic C++ optimization library
*
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
*
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
* purpose.
*
*/
#ifndef LEMON_GOMORY_HU_TREE_H
#define LEMON_GOMORY_HU_TREE_H
#include <limits>
#include <lemon/core.h>
#include <lemon/preflow.h>
#include <lemon/concept_check.h>
#include <lemon/concepts/maps.h>
/// \ingroup min_cut
/// \file
/// \brief Gomory-Hu cut tree in graphs.
namespace lemon {
/// \ingroup min_cut
///
/// \brief Gomory-Hu cut tree algorithm
///
/// The Gomory-Hu tree is a tree on the node set of a given graph, but it
/// may contain edges which are not in the original graph. It has the
/// property that the minimum capacity edge of the path between two nodes
/// in this tree has the same weight as the minimum cut in the graph
/// between these nodes. Moreover the components obtained by removing
/// this edge from the tree determine the corresponding minimum cut.
/// Therefore once this tree is computed, the minimum cut between any pair
/// of nodes can easily be obtained.
///
/// The algorithm calculates \e n-1 distinct minimum cuts (currently with
/// the \ref Preflow algorithm), thus it has \f$O(n^3\sqrt{e})\f$ overall
/// time complexity. It calculates a rooted Gomory-Hu tree.
/// The structure of the tree and the edge weights can be
/// obtained using \c predNode(), \c predValue() and \c rootDist().
/// The functions \c minCutMap() and \c minCutValue() calculate
/// the minimum cut and the minimum cut value between any two nodes
/// in the graph. You can also list (iterate on) the nodes and the
/// edges of the cuts using \c MinCutNodeIt and \c MinCutEdgeIt.
///
/// \tparam GR The type of the undirected graph the algorithm runs on.
/// \tparam CAP The type of the edge map containing the capacities.
/// The default map type is \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
#ifdef DOXYGEN
template <typename GR,
typename CAP>
#else
template <typename GR,
typename CAP = typename GR::template EdgeMap<int> >
#endif
class GomoryHu {
public:
/// The graph type of the algorithm
typedef GR Graph;
/// The capacity map type of the algorithm
typedef CAP Capacity;
/// The value type of capacities
typedef typename Capacity::Value Value;
private:
TEMPLATE_GRAPH_TYPEDEFS(Graph);
const Graph& _graph;
const Capacity& _capacity;
Node _root;
typename Graph::template NodeMap<Node>* _pred;
typename Graph::template NodeMap<Value>* _weight;
typename Graph::template NodeMap<int>* _order;
void createStructures() {
if (!_pred) {
_pred = new typename Graph::template NodeMap<Node>(_graph);
}
if (!_weight) {
_weight = new typename Graph::template NodeMap<Value>(_graph);
}
if (!_order) {
_order = new typename Graph::template NodeMap<int>(_graph);
}
}
void destroyStructures() {
if (_pred) {
delete _pred;
}
if (_weight) {
delete _weight;
}
if (_order) {
delete _order;
}
}
public:
/// \brief Constructor
///
/// Constructor.
/// \param graph The undirected graph the algorithm runs on.
/// \param capacity The edge capacity map.
GomoryHu(const Graph& graph, const Capacity& capacity)
: _graph(graph), _capacity(capacity),
_pred(0), _weight(0), _order(0)
{
checkConcept<concepts::ReadMap<Edge, Value>, Capacity>();
}
/// \brief Destructor
///
/// Destructor.
~GomoryHu() {
destroyStructures();
}
private:
// Initialize the internal data structures
void init() {
createStructures();
_root = NodeIt(_graph);
for (NodeIt n(_graph); n != INVALID; ++n) {
(*_pred)[n] = _root;
(*_order)[n] = -1;
}
(*_pred)[_root] = INVALID;
(*_weight)[_root] = std::numeric_limits<Value>::max();
}
// Start the algorithm
void start() {
Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID);
for (NodeIt n(_graph); n != INVALID; ++n) {
if (n == _root) continue;
Node pn = (*_pred)[n];
fa.source(n);
fa.target(pn);
fa.runMinCut();
(*_weight)[n] = fa.flowValue();
for (NodeIt nn(_graph); nn != INVALID; ++nn) {
if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
(*_pred)[nn] = n;
}
}
if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
(*_pred)[n] = (*_pred)[pn];
(*_pred)[pn] = n;
(*_weight)[n] = (*_weight)[pn];
(*_weight)[pn] = fa.flowValue();
}
}
(*_order)[_root] = 0;
int index = 1;
for (NodeIt n(_graph); n != INVALID; ++n) {
std::vector<Node> st;
Node nn = n;
while ((*_order)[nn] == -1) {
st.push_back(nn);
nn = (*_pred)[nn];
}
while (!st.empty()) {
(*_order)[st.back()] = index++;
st.pop_back();
}
}
}
public:
///\name Execution Control
///@{
/// \brief Run the Gomory-Hu algorithm.
///
/// This function runs the Gomory-Hu algorithm.
void run() {
init();
start();
}
/// @}
///\name Query Functions
///The results of the algorithm can be obtained using these
///functions.\n
///\ref run() should be called before using them.\n
///See also \ref MinCutNodeIt and \ref MinCutEdgeIt.
///@{
/// \brief Return the predecessor node in the Gomory-Hu tree.
///
/// This function returns the predecessor node of the given node
/// in the Gomory-Hu tree.
/// If \c node is the root of the tree, then it returns \c INVALID.
///
/// \pre \ref run() must be called before using this function.
Node predNode(const Node& node) const {
return (*_pred)[node];
}
/// \brief Return the weight of the predecessor edge in the
/// Gomory-Hu tree.
///
/// This function returns the weight of the predecessor edge of the
/// given node in the Gomory-Hu tree.
/// If \c node is the root of the tree, the result is undefined.
///
/// \pre \ref run() must be called before using this function.
Value predValue(const Node& node) const {
return (*_weight)[node];
}
/// \brief Return the distance from the root node in the Gomory-Hu tree.
///
/// This function returns the distance of the given node from the root
/// node in the Gomory-Hu tree.
///
/// \pre \ref run() must be called before using this function.
int rootDist(const Node& node) const {
return (*_order)[node];
}
/// \brief Return the minimum cut value between two nodes
///
/// This function returns the minimum cut value between the nodes
/// \c s and \c t.
/// It finds the nearest common ancestor of the given nodes in the
/// Gomory-Hu tree and calculates the minimum weight edge on the
/// paths to the ancestor.
///
/// \pre \ref run() must be called before using this function.
Value minCutValue(const Node& s, const Node& t) const {
Node sn = s, tn = t;
Value value = std::numeric_limits<Value>::max();
while (sn != tn) {
if ((*_order)[sn] < (*_order)[tn]) {
if ((*_weight)[tn] <= value) value = (*_weight)[tn];
tn = (*_pred)[tn];
} else {
if ((*_weight)[sn] <= value) value = (*_weight)[sn];
sn = (*_pred)[sn];
}
}
return value;
}
/// \brief Return the minimum cut between two nodes
///
/// This function returns the minimum cut between the nodes \c s and \c t
/// in the \c cutMap parameter by setting the nodes in the component of
/// \c s to \c true and the other nodes to \c false.
///
/// For higher level interfaces see MinCutNodeIt and MinCutEdgeIt.
///
/// \param s The base node.
/// \param t The node you want to separate from node \c s.
/// \param cutMap The cut will be returned in this map.
/// It must be a \c bool (or convertible) \ref concepts::ReadWriteMap
/// "ReadWriteMap" on the graph nodes.
///
/// \return The value of the minimum cut between \c s and \c t.
///
/// \pre \ref run() must be called before using this function.
template <typename CutMap>
Value minCutMap(const Node& s, ///<
const Node& t,
///<
CutMap& cutMap
///<
) const {
Node sn = s, tn = t;
bool s_root=false;
Node rn = INVALID;
Value value = std::numeric_limits<Value>::max();
while (sn != tn) {
if ((*_order)[sn] < (*_order)[tn]) {
if ((*_weight)[tn] <= value) {
rn = tn;
s_root = false;
value = (*_weight)[tn];
}
tn = (*_pred)[tn];
} else {
if ((*_weight)[sn] <= value) {
rn = sn;
s_root = true;
value = (*_weight)[sn];
}
sn = (*_pred)[sn];
}
}
typename Graph::template NodeMap<bool> reached(_graph, false);
reached[_root] = true;
cutMap.set(_root, !s_root);
reached[rn] = true;
cutMap.set(rn, s_root);
std::vector<Node> st;
for (NodeIt n(_graph); n != INVALID; ++n) {
st.clear();
Node nn = n;
while (!reached[nn]) {
st.push_back(nn);
nn = (*_pred)[nn];
}
while (!st.empty()) {
cutMap.set(st.back(), cutMap[nn]);
st.pop_back();
}
}
return value;
}
///@}
friend class MinCutNodeIt;
/// Iterate on the nodes of a minimum cut
/// This iterator class lists the nodes of a minimum cut found by
/// GomoryHu. Before using it, you must allocate a GomoryHu class
/// and call its \ref GomoryHu::run() "run()" method.
///
/// This example counts the nodes in the minimum cut separating \c s from
/// \c t.
/// \code
/// GomoryHu<Graph> gom(g, capacities);
/// gom.run();
/// int cnt=0;
/// for(GomoryHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt;
/// \endcode
class MinCutNodeIt
{
bool _side;
typename Graph::NodeIt _node_it;
typename Graph::template NodeMap<bool> _cut;
public:
/// Constructor
/// Constructor.
///
MinCutNodeIt(GomoryHu const &gomory,
///< The GomoryHu class. You must call its
/// run() method
/// before initializing this iterator.
const Node& s, ///< The base node.
const Node& t,
///< The node you want to separate from node \c s.
bool side=true
///< If it is \c true (default) then the iterator lists
/// the nodes of the component containing \c s,
/// otherwise it lists the other component.
/// \note As the minimum cut is not always unique,
/// \code
/// MinCutNodeIt(gomory, s, t, true);
/// \endcode
/// and
/// \code
/// MinCutNodeIt(gomory, t, s, false);
/// \endcode
/// does not necessarily give the same set of nodes.
/// However it is ensured that
/// \code
/// MinCutNodeIt(gomory, s, t, true);
/// \endcode
/// and
/// \code
/// MinCutNodeIt(gomory, s, t, false);
/// \endcode
/// together list each node exactly once.
)
: _side(side), _cut(gomory._graph)
{
gomory.minCutMap(s,t,_cut);
for(_node_it=typename Graph::NodeIt(gomory._graph);
_node_it!=INVALID && _cut[_node_it]!=_side;
++_node_it) {}
}
/// Conversion to \c Node
/// Conversion to \c Node.
///
operator typename Graph::Node() const
{
return _node_it;
}
bool operator==(Invalid) { return _node_it==INVALID; }
bool operator!=(Invalid) { return _node_it!=INVALID; }
/// Next node
/// Next node.
///
MinCutNodeIt &operator++()
{
for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {}
return *this;
}
/// Postfix incrementation
/// Postfix incrementation.
///
/// \warning This incrementation
/// returns a \c Node, not a \c MinCutNodeIt, as one may
/// expect.
typename Graph::Node operator++(int)
{
typename Graph::Node n=*this;
++(*this);
return n;
}
};
friend class MinCutEdgeIt;
/// Iterate on the edges of a minimum cut
/// This iterator class lists the edges of a minimum cut found by
/// GomoryHu. Before using it, you must allocate a GomoryHu class
/// and call its \ref GomoryHu::run() "run()" method.
///
/// This example computes the value of the minimum cut separating \c s from
/// \c t.
/// \code
/// GomoryHu<Graph> gom(g, capacities);
/// gom.run();
/// int value=0;
/// for(GomoryHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e)
/// value+=capacities[e];
/// \endcode
/// The result will be the same as the value returned by
/// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)".
class MinCutEdgeIt
{
bool _side;
const Graph &_graph;
typename Graph::NodeIt _node_it;
typename Graph::OutArcIt _arc_it;
typename Graph::template NodeMap<bool> _cut;
void step()
{
++_arc_it;
while(_node_it!=INVALID && _arc_it==INVALID)
{
for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {}
if(_node_it!=INVALID)
_arc_it=typename Graph::OutArcIt(_graph,_node_it);
}
}
public:
/// Constructor
/// Constructor.
///
MinCutEdgeIt(GomoryHu const &gomory,
///< The GomoryHu class. You must call its
/// run() method
/// before initializing this iterator.
const Node& s, ///< The base node.
const Node& t,
///< The node you want to separate from node \c s.
bool side=true
///< If it is \c true (default) then the listed arcs
/// will be oriented from the
/// nodes of the component containing \c s,
/// otherwise they will be oriented in the opposite
/// direction.
)
: _graph(gomory._graph), _cut(_graph)
{
gomory.minCutMap(s,t,_cut);
if(!side)
for(typename Graph::NodeIt n(_graph);n!=INVALID;++n)
_cut[n]=!_cut[n];
for(_node_it=typename Graph::NodeIt(_graph);
_node_it!=INVALID && !_cut[_node_it];
++_node_it) {}
_arc_it = _node_it!=INVALID ?
typename Graph::OutArcIt(_graph,_node_it) : INVALID;
while(_node_it!=INVALID && _arc_it == INVALID)
{
for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {}
if(_node_it!=INVALID)
_arc_it= typename Graph::OutArcIt(_graph,_node_it);
}
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
}
/// Conversion to \c Arc
/// Conversion to \c Arc.
///
operator typename Graph::Arc() const
{
return _arc_it;
}
/// Conversion to \c Edge
/// Conversion to \c Edge.
///
operator typename Graph::Edge() const
{
return _arc_it;
}
bool operator==(Invalid) { return _node_it==INVALID; }
bool operator!=(Invalid) { return _node_it!=INVALID; }
/// Next edge
/// Next edge.
///
MinCutEdgeIt &operator++()
{
step();
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
return *this;
}
/// Postfix incrementation
/// Postfix incrementation.
///
/// \warning This incrementation
/// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect.
typename Graph::Arc operator++(int)
{
typename Graph::Arc e=*this;
++(*this);
return e;
}
};
};
}
#endif
|