/usr/include/CGAL/internal/auxiliary/graph.h is in libcgal-dev 4.11-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 | // Copyright (c) 2001 Yuri Boykov
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
// You can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
/*
###################################################################
# #
# MAXFLOW - software for computing mincut/maxflow in a graph #
# Version 2.21 #
# http://www.cs.ucl.ac.uk/staff/V.Kolmogorov/software.html #
# #
# Yuri Boykov (yuri@csd.uwo.ca) #
# Vladimir Kolmogorov (v.kolmogorov@cs.ucl.ac.uk) #
# 2001 #
# #
###################################################################
1. Introduction.
This software library implements the maxflow algorithm
described in
An Experimental Comparison of Min-Cut/Max-Flow Algorithms
for Energy Minimization in Vision.
Yuri Boykov and Vladimir Kolmogorov.
In IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI),
September 2004
This algorithm was developed by Yuri Boykov and Vladimir Kolmogorov
at Siemens Corporate Research. To make it available for public use,
it was later reimplemented by Vladimir Kolmogorov based on open publications.
If you use this software for research purposes, you should cite
the aforementioned paper in any resulting publication.
Tested under windows, Visual C++ 6.0 compiler and unix (SunOS 5.8
and RedHat Linux 7.0, GNU c++ compiler).
##################################################################
2. Licence.
Copyright UCL Business PLC
This program is available under dual licence:
1) Under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
Note that any program that incorporates the code under this licence must, under the terms of the GNU GPL, be released under a licence compatible with the GPL. GNU GPL does not permit incorporating this program into proprietary programs. If you wish to do this, please see the alternative licence available below.
GNU General Public License can be found at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
2) Proprietary Licence from UCL Business PLC.
To enable programers to include the MaxFlow software in a proprietary system (which is not allowed by the GNU GPL), this licence gives you the right to incorporate the software in your program and distribute under any licence of your choosing. The full terms of the licence and applicable fee, are available from the Licensors at: http://www.uclb-elicensing.com/optimisation_software/maxflow_computervision.html
##################################################################
3. Graph representation.
There are two versions of the algorithm using different
graph representations (adjacency list and forward star).
The former one uses more than twice as much memory as the
latter one but is 10-20% faster.
Memory allocation (assuming that all capacities are 'short' - 2 bytes):
| Nodes | Arcs
------------------------------------------
Adjacency list | *24 bytes | *14 bytes
Forward star | *28 bytes | 6 bytes
(* means that often it should be rounded up to be a multiple of 4
- some compilers (e.g. Visual C++) seem to round up elements
of arrays unless the are structures containing only char[].)
Note that arcs are always added in pairs - in forward and reverse directions.
Arcs between nodes and terminals (the source and the sink) are
not stored as arcs, but rather as a part of nodes.
The assumption for the forward star representation is that
the maximum number of arcs per node (except the source
and the sink) is much less than ARC_BLOCK_SIZE (1024 by default).
Both versions have the same interface.
##################################################################
4. Example usage.
This section shows how to use the library to compute
a minimum cut on the following graph:
SOURCE
/ \
1/ \2
/ 3 \
node0 -----> node1
| <----- |
| 4 |
\ /
5\ /6
\ /
SINK
///////////////////////////////////////////////////
#include <stdio.h>
#include "graph.h"
void main()
{
Graph::node_id nodes[2];
Graph *g = new Graph();
nodes[0] = g -> add_node();
nodes[1] = g -> add_node();
g -> set_tweights(nodes[0], 1, 5);
g -> set_tweights(nodes[1], 2, 6);
g -> add_edge(nodes[0], nodes[1], 3, 4);
Graph::flowtype flow = g -> maxflow();
printf("Flow = %d\n", flow);
printf("Minimum cut:\n");
if (g->what_segment(nodes[0]) == Graph::SOURCE)
printf("node0 is in the SOURCE set\n");
else
printf("node0 is in the SINK set\n");
if (g->what_segment(nodes[1]) == Graph::SOURCE)
printf("node1 is in the SOURCE set\n");
else
printf("node1 is in the SINK set\n");
delete g;
}
///////////////////////////////////////////////////
*/
/* block.h */
/*
Template classes Block and DBlock
Implement adding and deleting items of the same type in blocks.
If there there are many items then using Block or DBlock
is more efficient than using 'new' and 'delete' both in terms
of memory and time since
(1) On some systems there is some minimum amount of memory
that 'new' can allocate (e.g., 64), so if items are
small that a lot of memory is wasted.
(2) 'new' and 'delete' are designed for items of varying size.
If all items has the same size, then an algorithm for
adding and deleting can be made more efficient.
(3) All Block and DBlock functions are inline, so there are
no extra function calls.
Differences between Block and DBlock:
(1) DBlock allows both adding and deleting items,
whereas Block allows only adding items.
(2) Block has an additional operation of scanning
items added so far (in the order in which they were added).
(3) Block allows to allocate several consecutive
items at a time, whereas DBlock can add only a single item.
Note that no constructors or destructors are called for items.
Example usage for items of type 'MyType':
///////////////////////////////////////////////////
#include "block.h"
#define BLOCK_SIZE 1024
#include <CGAL/license/Surface_mesh_segmentation.h>
typedef struct { int a, b; } MyType;
MyType *ptr, *array[10000];
...
Block<MyType> *block = new Block<MyType>(BLOCK_SIZE);
// adding items
for (int i=0; i<sizeof(array); i++)
{
ptr = block -> New();
ptr -> a = ptr -> b = rand();
}
// reading items
for (ptr=block->ScanFirst(); ptr; ptr=block->ScanNext())
{
printf("%d %d\n", ptr->a, ptr->b);
}
delete block;
...
DBlock<MyType> *dblock = new DBlock<MyType>(BLOCK_SIZE);
// adding items
for (int i=0; i<sizeof(array); i++)
{
array[i] = dblock -> New();
}
// deleting items
for (int i=0; i<sizeof(array); i+=2)
{
dblock -> Delete(array[i]);
}
// adding items
for (int i=0; i<sizeof(array); i++)
{
array[i] = dblock -> New();
}
delete dblock;
///////////////////////////////////////////////////
Note that DBlock deletes items by marking them as
empty (i.e., by adding them to the list of free items),
so that this memory could be used for subsequently
added items. Thus, at each moment the memory allocated
is determined by the maximum number of items allocated
simultaneously at earlier moments. All memory is
deallocated only when the destructor is called.
*/
#ifndef __BLOCK_H__
#define __BLOCK_H__
#include <stdlib.h>
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
template <class Type> class Block
{
public:
/* Constructor. Arguments are the block size and
(optionally) the pointer to the function which
will be called if allocation failed; the message
passed to this function is "Not enough memory!" */
Block(int size, void (*err_function)(const char *) = NULL) {
first = last = NULL;
block_size = size;
error_function = err_function;
}
/* Destructor. Deallocates all items added so far */
~Block() {
while (first) {
block *next = first -> next;
delete[] ((char*)first);
first = next;
}
}
/* Allocates 'num' consecutive items; returns pointer
to the first item. 'num' cannot be greater than the
block size since items must fit in one block */
Type *New(int num = 1) {
Type *t;
if (!last || last->current + num > last->last) {
if (last && last->next) last = last -> next;
else {
block *next = (block *) new char [sizeof(block) + (block_size-1)*sizeof(Type)];
if (!next) {
if (error_function) (*error_function)("Not enough memory!");
exit(1);
}
if (last) last -> next = next;
else first = next;
last = next;
last -> current = & ( last -> data[0] );
last -> last = last -> current + block_size;
last -> next = NULL;
}
}
t = last -> current;
last -> current += num;
return t;
}
/* Returns the first item (or NULL, if no items were added) */
Type *ScanFirst() {
for (scan_current_block=first; scan_current_block;
scan_current_block = scan_current_block->next) {
scan_current_data = & ( scan_current_block -> data[0] );
if (scan_current_data < scan_current_block -> current) return scan_current_data
++;
}
return NULL;
}
/* Returns the next item (or NULL, if all items have been read)
Can be called only if previous ScanFirst() or ScanNext()
call returned not NULL. */
Type *ScanNext() {
while (scan_current_data >= scan_current_block -> current) {
scan_current_block = scan_current_block -> next;
if (!scan_current_block) return NULL;
scan_current_data = & ( scan_current_block -> data[0] );
}
return scan_current_data ++;
}
/* Marks all elements as empty */
void Reset() {
block *b;
if (!first) return;
for (b=first; ; b=b->next) {
b -> current = & ( b -> data[0] );
if (b == last) break;
}
last = first;
}
/***********************************************************************/
private:
typedef struct block_st {
Type *current, *last;
struct block_st *next;
Type data[1];
} block;
int block_size;
block *first;
block *last;
block *scan_current_block;
Type *scan_current_data;
void (*error_function)(const char *);
};
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
template <class Type> class DBlock
{
public:
/* Constructor. Arguments are the block size and
(optionally) the pointer to the function which
will be called if allocation failed; the message
passed to this function is "Not enough memory!" */
DBlock(int size, void (*err_function)(const char *) = NULL) {
first = NULL;
first_free = NULL;
block_size = size;
error_function = err_function;
}
/* Destructor. Deallocates all items added so far */
~DBlock() {
while (first) {
block *next = first -> next;
delete[] ((char*)first);
first = next;
}
}
/* Allocates one item */
Type *New() {
block_item *item;
if (!first_free) {
block *next = first;
first = (block *) new char [sizeof(block) + (block_size-1)*sizeof(block_item)];
if (!first) {
if (error_function) (*error_function)("Not enough memory!");
exit(1);
}
first_free = & (first -> data[0] );
for (item=first_free; item<first_free+block_size-1; item++)
item -> next_free = item + 1;
item -> next_free = NULL;
first -> next = next;
}
item = first_free;
first_free = item -> next_free;
return (Type *) item;
}
/* Deletes an item allocated previously */
void Delete(Type *t) {
((block_item *) t) -> next_free = first_free;
first_free = (block_item *) t;
}
/***********************************************************************/
private:
typedef union block_item_st {
Type t;
block_item_st *next_free;
} block_item;
typedef struct block_st {
struct block_st *next;
block_item data[1];
} block;
int block_size;
block *first;
block_item *first_free;
void (*error_function)(const char *);
};
#endif
/* graph.h */
/*
This software library implements the maxflow algorithm
described in
An Experimental Comparison of Min-Cut/Max-Flow Algorithms
for Energy Minimization in Vision.
Yuri Boykov and Vladimir Kolmogorov.
In IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI),
September 2004
This algorithm was developed by Yuri Boykov and Vladimir Kolmogorov
at Siemens Corporate Research. To make it available for public use,
it was later reimplemented by Vladimir Kolmogorov based on open publications.
If you use this software for research purposes, you should cite
the aforementioned paper in any resulting publication.
----------------------------------------------------------------
For description, license, example usage, discussion of graph representation and memory usage see README.TXT.
*/
#ifndef __GRAPH_H__
#define __GRAPH_H__
//#include "block.h"
#include <stdio.h>
/*
Nodes, arcs and pointers to nodes are
added in blocks for memory and time efficiency.
Below are numbers of items in blocks
*/
#define NODE_BLOCK_SIZE 512
#define ARC_BLOCK_SIZE 1024
#define NODEPTR_BLOCK_SIZE 128
template <std::size_t size>
struct Int_to_ptr;
template<> struct Int_to_ptr<sizeof(int)> {
typedef int type;
};
#if INT_MAX != LONG_MAX
template<> struct Int_to_ptr<sizeof(long)> {
typedef long type;
};
#else
template<> struct Int_to_ptr<sizeof(long long)> {
typedef long long type;
};
#endif
class Graph
{
public:
typedef enum {
SOURCE = 0,
SINK = 1
} termtype; /* terminals */
/* Type of edge weights.
Can be changed to char, int, float, double, ... */
typedef double captype;
/* Type of total flow */
typedef double flowtype;
typedef void * node_id;
/* interface functions */
/* Constructor. Optional argument is the pointer to the
function which will be called if an error occurs;
an error message is passed to this function. If this
argument is omitted, exit(1) will be called. */
Graph(void (*err_function)(const char *) = NULL);
/* Destructor */
~Graph();
/* Adds a node to the graph */
node_id add_node();
/* Adds a bidirectional edge between 'from' and 'to'
with the weights 'cap' and 'rev_cap' */
void add_edge(node_id from, node_id to, captype cap, captype rev_cap);
/* Sets the weights of the edges 'SOURCE->i' and 'i->SINK'
Can be called at most once for each node before any call to 'add_tweights'.
Weights can be negative */
void set_tweights(node_id i, captype cap_source, captype cap_sink);
/* Adds new edges 'SOURCE->i' and 'i->SINK' with corresponding weights
Can be called multiple times for each node.
Weights can be negative */
void add_tweights(node_id i, captype cap_source, captype cap_sink);
/* After the maxflow is computed, this function returns to which
segment the node 'i' belongs (Graph::SOURCE or Graph::SINK) */
termtype what_segment(node_id i);
/* Computes the maxflow. Can be called only once. */
flowtype maxflow();
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
private:
/* internal variables and functions */
struct arc_forward_st;
struct arc_reverse_st;
typedef Int_to_ptr< sizeof(void*) >::type INTEGER;
#define IS_ODD(a) ((INTEGER)(a) & 1)
#define MAKE_ODD(a) ((arc_forward *) ((INTEGER)(a) | 1))
#define MAKE_EVEN(a) ((arc_forward *) ((INTEGER)(a) & (~1)))
#define MAKE_ODD_REV(a) ((arc_reverse *) ((INTEGER)(a) | 1))
#define MAKE_EVEN_REV(a) ((arc_reverse *) ((INTEGER)(a) & (~1)))
#define POINTER_TO_INTEGER(ptr) ((INTEGER) ptr)
/* node structure */
typedef struct node_st {
/*
Usually i->first_out is the first outgoing
arc, and (i+1)->first_out-1 is the last outgoing arc.
However, it is not always possible, since
arcs are allocated in blocks, so arcs corresponding
to two consecutive nodes may be in different blocks.
If outgoing arcs for i are last in the arc block,
then a different mechanism is used. i->first_out
is odd in this case; the first outgoing arc
is (a+1), and the last outgoing arc is
((arc_forward *)(a->shift))-1, where
a = (arc_forward *) (((char *)(i->first_out)) + 1);
Similar mechanism is used for incoming arcs.
*/
arc_forward_st *first_out; /* first outcoming arc */
arc_reverse_st *first_in; /* first incoming arc */
arc_forward_st *parent; /* describes node's parent
if IS_ODD(parent) then MAKE_EVEN(parent) points to 'arc_reverse',
otherwise parent points to 'arc_forward' */
node_st *next; /* pointer to the next active node
(or to itself if it is the last node in the list) */
int TS; /* timestamp showing when DIST was computed */
int DIST; /* distance to the terminal */
short is_sink; /* flag showing whether the node is in the source or in the sink tree */
captype tr_cap; /* if tr_cap > 0 then tr_cap is residual capacity of the arc SOURCE->node
otherwise -tr_cap is residual capacity of the arc node->SINK */
} node;
/* arc structures */
#define NEIGHBOR_NODE(i, shift) ((node *) ((char *)(i) + (shift)))
#define NEIGHBOR_NODE_REV(i, shift) ((node *) ((char *)(i) - (shift)))
typedef struct arc_forward_st {
INTEGER shift; /* node_to = NEIGHBOR_NODE(node_from, shift) */
captype r_cap; /* residual capacity */
captype r_rev_cap; /* residual capacity of the reverse arc*/
} arc_forward;
typedef struct arc_reverse_st {
arc_forward *sister; /* reverse arc */
} arc_reverse;
/* 'pointer to node' structure */
typedef struct nodeptr_st {
node_st *ptr;
nodeptr_st *next;
} nodeptr;
typedef struct node_block_st {
node *current;
struct node_block_st *next;
node nodes[NODE_BLOCK_SIZE];
} node_block;
#define last_node LAST_NODE.LAST_NODE
typedef struct arc_for_block_st {
char *start; /* the actual start address of this block.
May be different from 'this' since 'this'
must be at an even address. */
arc_forward *current;
struct arc_for_block_st *next;
arc_forward
arcs_for[ARC_BLOCK_SIZE]; /* all arcs must be at even addresses */
union {
arc_forward dummy;
node *LAST_NODE; /* used in graph consruction */
} LAST_NODE;
} arc_for_block;
typedef struct arc_rev_block_st {
char *start; /* the actual start address of this block.
May be different from 'this' since 'this'
must be at an even address. */
arc_reverse *current;
struct arc_rev_block_st *next;
arc_reverse
arcs_rev[ARC_BLOCK_SIZE]; /* all arcs must be at even addresses */
union {
arc_reverse dummy;
node *LAST_NODE; /* used in graph consruction */
} LAST_NODE;
} arc_rev_block;
node_block *node_block_first;
arc_for_block *arc_for_block_first;
arc_rev_block *arc_rev_block_first;
DBlock<nodeptr> *nodeptr_block;
void (*error_function)(const char
*); /* this function is called if a error occurs,
with a corresponding error message
(or exit(1) is called if it's NULL) */
flowtype flow; /* total flow */
/***********************************************************************/
node *queue_first[2], *queue_last[2]; /* list of active nodes */
nodeptr *orphan_first, *orphan_last; /* list of pointers to orphans */
int TIME; /* monotonically increasing global counter */
/***********************************************************************/
/* functions for processing active list */
void set_active(node *i);
node *next_active();
void prepare_graph();
void maxflow_init();
void augment(node *s_start, node *t_start, captype *cap_middle,
captype *rev_cap_middle);
void process_source_orphan(node *i);
void process_sink_orphan(node *i);
};
/* graph.cpp */
//#include <stdio.h>
//#include "graph.h"
inline Graph::Graph(void (*err_function)(const char *))
{
error_function = err_function;
node_block_first = NULL;
arc_for_block_first = NULL;
arc_rev_block_first = NULL;
orphan_first = NULL;
orphan_last = NULL;
flow = 0;
}
inline Graph::~Graph()
{
while (node_block_first) {
node_block *next = node_block_first -> next;
delete node_block_first;
node_block_first = next;
}
while (arc_for_block_first) {
arc_for_block *next = arc_for_block_first -> next;
delete[] arc_for_block_first -> start;
arc_for_block_first = next;
}
while (arc_rev_block_first) {
arc_rev_block *next = arc_rev_block_first -> next;
delete[] arc_rev_block_first -> start;
arc_rev_block_first = next;
}
}
inline Graph::node_id Graph::add_node()
{
node *i;
if (!node_block_first
|| node_block_first->current+1 > &node_block_first->nodes[NODE_BLOCK_SIZE-1]) {
node_block *next = node_block_first;
node_block_first = (node_block *) new node_block;
if (!node_block_first) {
if (error_function) (*error_function)("Not enough memory!");
exit(1);
}
node_block_first -> current = & ( node_block_first -> nodes[0] );
node_block_first -> next = next;
}
i = node_block_first -> current ++;
i -> first_out = (arc_forward *) 0;
i -> first_in = (arc_reverse *) 0;
i -> tr_cap = 0;
return (node_id) i;
}
inline void Graph::add_edge(node_id from, node_id to, captype cap,
captype rev_cap)
{
arc_forward *a_for;
arc_reverse *a_rev;
if (!arc_for_block_first
|| arc_for_block_first->current+1 >
&arc_for_block_first->arcs_for[ARC_BLOCK_SIZE]) {
arc_for_block *next = arc_for_block_first;
char *ptr = new char[sizeof(arc_for_block)+1];
if (!ptr) {
if (error_function) (*error_function)("Not enough memory!");
exit(1);
}
if (IS_ODD(ptr)) arc_for_block_first = (arc_for_block *) (ptr + 1);
else arc_for_block_first = (arc_for_block *) ptr;
arc_for_block_first -> start = ptr;
arc_for_block_first -> current = & ( arc_for_block_first -> arcs_for[0] );
arc_for_block_first -> next = next;
}
if (!arc_rev_block_first
|| arc_rev_block_first->current+1 >
&arc_rev_block_first->arcs_rev[ARC_BLOCK_SIZE]) {
arc_rev_block *next = arc_rev_block_first;
char *ptr = new char[sizeof(arc_rev_block)+1];
if (!ptr) {
if (error_function) (*error_function)("Not enough memory!");
exit(1);
}
if (IS_ODD(ptr)) arc_rev_block_first = (arc_rev_block *) (ptr + 1);
else arc_rev_block_first = (arc_rev_block *) ptr;
arc_rev_block_first -> start = ptr;
arc_rev_block_first -> current = & ( arc_rev_block_first -> arcs_rev[0] );
arc_rev_block_first -> next = next;
}
a_for = arc_for_block_first -> current ++;
a_rev = arc_rev_block_first -> current ++;
a_rev -> sister = (arc_forward *) from;
a_for -> shift = POINTER_TO_INTEGER(to);
a_for -> r_cap = cap;
a_for -> r_rev_cap = rev_cap;
((node *)from) -> first_out =
(arc_forward *) (POINTER_TO_INTEGER(((node *)from) -> first_out) + 1);
((node *)to) -> first_in =
(arc_reverse *) (POINTER_TO_INTEGER(((node *)to) -> first_in) + 1);
}
inline void Graph::set_tweights(node_id i, captype cap_source, captype cap_sink)
{
flow += (cap_source < cap_sink) ? cap_source : cap_sink;
((node*)i) -> tr_cap = cap_source - cap_sink;
}
inline void Graph::add_tweights(node_id i, captype cap_source, captype cap_sink)
{
captype delta = ((node*)i) -> tr_cap;
if (delta > 0) cap_source += delta;
else cap_sink -= delta;
flow += (cap_source < cap_sink) ? cap_source : cap_sink;
((node*)i) -> tr_cap = cap_source - cap_sink;
}
/*
Converts arcs added by 'add_edge()' calls
to a forward star graph representation.
Linear time algorithm.
No or little additional memory is allocated
during this process
(it may be necessary to allocate additional
arc blocks, since arcs corresponding to the
same node must be contiguous, i.e. be in one
arc block.)
*/
inline void Graph::prepare_graph()
{
node *i;
arc_for_block *ab_for, *ab_for_first;
arc_rev_block *ab_rev, *ab_rev_first, *ab_rev_scan;
arc_forward *a_for;
arc_reverse *a_rev, *a_rev_scan, *a_rev_tmp=new arc_reverse;
node_block *nb;
bool for_flag = false, rev_flag = false;
INTEGER k;
if (!arc_rev_block_first) {
node_id from = add_node(), to = add_node();
add_edge(from, to, 1, 0);
}
/* FIRST STAGE */
a_rev_tmp->sister = NULL;
for (a_rev=arc_rev_block_first->current;
a_rev<&arc_rev_block_first->arcs_rev[ARC_BLOCK_SIZE]; a_rev++) {
a_rev -> sister = NULL;
}
ab_for = ab_for_first = arc_for_block_first;
ab_rev = ab_rev_first = ab_rev_scan = arc_rev_block_first;
a_for = &ab_for->arcs_for[0];
a_rev = a_rev_scan = &ab_rev->arcs_rev[0];
for (nb=node_block_first; nb; nb=nb->next) {
for (i=&nb->nodes[0]; i<nb->current; i++) {
/* outgoing arcs */
k = POINTER_TO_INTEGER(i -> first_out);
if (a_for + k > &ab_for->arcs_for[ARC_BLOCK_SIZE]) {
if (k > ARC_BLOCK_SIZE) {
if (error_function) (*error_function)("# of arcs per node exceeds block size!");
exit(1);
}
if (for_flag) ab_for = NULL;
else {
ab_for = ab_for -> next;
ab_rev_scan = ab_rev_scan -> next;
}
if (ab_for == NULL) {
arc_for_block *next = arc_for_block_first;
char *ptr = new char[sizeof(arc_for_block)+1];
if (!ptr) {
if (error_function) (*error_function)("Not enough memory!");
exit(1);
}
if (IS_ODD(ptr)) arc_for_block_first = (arc_for_block *) (ptr + 1);
else arc_for_block_first = (arc_for_block *) ptr;
arc_for_block_first -> start = ptr;
arc_for_block_first -> current = & ( arc_for_block_first -> arcs_for[0] );
arc_for_block_first -> next = next;
ab_for = arc_for_block_first;
for_flag = true;
} else a_rev_scan = &ab_rev_scan->arcs_rev[0];
a_for = &ab_for->arcs_for[0];
}
if (ab_rev_scan) {
a_rev_scan += k;
i -> parent = (arc_forward *) a_rev_scan;
} else i -> parent = (arc_forward *) a_rev_tmp;
a_for += k;
i -> first_out = a_for;
ab_for -> last_node = i;
/* incoming arcs */
k = POINTER_TO_INTEGER(i -> first_in);
if (a_rev + k > &ab_rev->arcs_rev[ARC_BLOCK_SIZE]) {
if (k > ARC_BLOCK_SIZE) {
if (error_function) (*error_function)("# of arcs per node exceeds block size!");
exit(1);
}
if (rev_flag) ab_rev = NULL;
else ab_rev = ab_rev -> next;
if (ab_rev == NULL) {
arc_rev_block *next = arc_rev_block_first;
char *ptr = new char[sizeof(arc_rev_block)+1];
if (!ptr) {
if (error_function) (*error_function)("Not enough memory!");
exit(1);
}
if (IS_ODD(ptr)) arc_rev_block_first = (arc_rev_block *) (ptr + 1);
else arc_rev_block_first = (arc_rev_block *) ptr;
arc_rev_block_first -> start = ptr;
arc_rev_block_first -> current = & ( arc_rev_block_first -> arcs_rev[0] );
arc_rev_block_first -> next = next;
ab_rev = arc_rev_block_first;
rev_flag = true;
}
a_rev = &ab_rev->arcs_rev[0];
}
a_rev += k;
i -> first_in = a_rev;
ab_rev -> last_node = i;
}
/* i is the last node in block */
i -> first_out = a_for;
i -> first_in = a_rev;
}
/* SECOND STAGE */
for (ab_for=arc_for_block_first; ab_for; ab_for=ab_for->next) {
ab_for -> current = ab_for -> last_node -> first_out;
}
for ( ab_for=ab_for_first, ab_rev=ab_rev_first;
ab_for;
ab_for=ab_for->next, ab_rev=ab_rev->next )
for ( a_for=&ab_for->arcs_for[0], a_rev=&ab_rev->arcs_rev[0];
a_for<&ab_for->arcs_for[ARC_BLOCK_SIZE];
a_for++, a_rev++ ) {
arc_forward *af;
arc_reverse *ar;
node *from;
INTEGER shift = 0, shift_new;
captype r_cap=0, r_rev_cap=0, r_cap_new, r_rev_cap_new;
if (!(from=(node *)(a_rev->sister))) continue;
af = a_for;
ar = a_rev;
do {
ar -> sister = NULL;
shift_new = ((char *)(af->shift)) - (char *)from;
r_cap_new = af -> r_cap;
r_rev_cap_new = af -> r_rev_cap;
if (shift) {
af -> shift = shift;
af -> r_cap = r_cap;
af -> r_rev_cap = r_rev_cap;
}
shift = shift_new;
r_cap = r_cap_new;
r_rev_cap = r_rev_cap_new;
af = -- from -> first_out;
if ((arc_reverse *)(from->parent) != a_rev_tmp) {
from -> parent = (arc_forward *)(((arc_reverse *)(from -> parent)) - 1);
ar = (arc_reverse *)(from -> parent);
}
} while ( (from=(node *)(ar->sister)) );
af -> shift = shift;
af -> r_cap = r_cap;
af -> r_rev_cap = r_rev_cap;
}
for (ab_for=arc_for_block_first; ab_for; ab_for=ab_for->next) {
i = ab_for -> last_node;
a_for = i -> first_out;
ab_for -> current -> shift = a_for -> shift;
ab_for -> current -> r_cap = a_for -> r_cap;
ab_for -> current -> r_rev_cap = a_for -> r_rev_cap;
a_for -> shift = POINTER_TO_INTEGER(ab_for -> current + 1);
i -> first_out = (arc_forward *) (((char *)a_for) - 1);
}
/* THIRD STAGE */
for (ab_rev=arc_rev_block_first; ab_rev; ab_rev=ab_rev->next) {
ab_rev -> current = ab_rev -> last_node -> first_in;
}
for (nb=node_block_first; nb; nb=nb->next)
for (i=&nb->nodes[0]; i<nb->current; i++) {
arc_forward *a_for_first, *a_for_last;
a_for_first = i -> first_out;
if (IS_ODD(a_for_first)) {
a_for_first = (arc_forward *) (((char *)a_for_first) + 1);
a_for_last = (arc_forward *) ((a_for_first ++) -> shift);
} else a_for_last = (i + 1) -> first_out;
for (a_for=a_for_first; a_for<a_for_last; a_for++) {
node *to = NEIGHBOR_NODE(i, a_for -> shift);
a_rev = -- to -> first_in;
a_rev -> sister = a_for;
}
}
for (ab_rev=arc_rev_block_first; ab_rev; ab_rev=ab_rev->next) {
i = ab_rev -> last_node;
a_rev = i -> first_in;
ab_rev -> current -> sister = a_rev -> sister;
a_rev -> sister = (arc_forward *) (ab_rev -> current + 1);
i -> first_in = (arc_reverse *) (((char *)a_rev) - 1);
}
delete a_rev_tmp;
}
/* maxflow.cpp */
//#include <stdio.h>
//#include "graph.h"
/*
special constants for node->parent
*/
#define TERMINAL ( (arc_forward *) 1 ) /* to terminal */
#define ORPHAN ( (arc_forward *) 2 ) /* orphan */
#define INFINITE_D 1000000000 /* infinite distance to the terminal */
/***********************************************************************/
/*
Functions for processing active list.
i->next points to the next node in the list
(or to i, if i is the last node in the list).
If i->next is NULL iff i is not in the list.
There are two queues. Active nodes are added
to the end of the second queue and read from
the front of the first queue. If the first queue
is empty, it is replaced by the second queue
(and the second queue becomes empty).
*/
inline void Graph::set_active(node *i)
{
if (!i->next) {
/* it's not in the list yet */
if (queue_last[1]) queue_last[1] -> next = i;
else queue_first[1] = i;
queue_last[1] = i;
i -> next = i;
}
}
/*
Returns the next active node.
If it is connected to the sink, it stays in the list,
otherwise it is removed from the list
*/
inline Graph::node * Graph::next_active()
{
node *i;
while ( 1 ) {
if (!(i=queue_first[0])) {
queue_first[0] = i = queue_first[1];
queue_last[0] = queue_last[1];
queue_first[1] = NULL;
queue_last[1] = NULL;
if (!i) return NULL;
}
/* remove it from the active list */
if (i->next == i) queue_first[0] = queue_last[0] = NULL;
else queue_first[0] = i -> next;
i -> next = NULL;
/* a node in the list is active iff it has a parent */
if (i->parent) return i;
}
}
/***********************************************************************/
inline void Graph::maxflow_init()
{
node *i;
node_block *nb;
queue_first[0] = queue_last[0] = NULL;
queue_first[1] = queue_last[1] = NULL;
orphan_first = NULL;
for (nb=node_block_first; nb; nb=nb->next)
for (i=&nb->nodes[0]; i<nb->current; i++) {
i -> next = NULL;
i -> TS = 0;
if (i->tr_cap > 0) {
/* i is connected to the source */
i -> is_sink = 0;
i -> parent = TERMINAL;
set_active(i);
i -> TS = 0;
i -> DIST = 1;
} else if (i->tr_cap < 0) {
/* i is connected to the sink */
i -> is_sink = 1;
i -> parent = TERMINAL;
set_active(i);
i -> TS = 0;
i -> DIST = 1;
} else {
i -> parent = NULL;
}
}
TIME = 0;
}
/***********************************************************************/
inline void Graph::augment(node *s_start, node *t_start, captype *cap_middle,
captype *rev_cap_middle)
{
node *i;
arc_forward *a;
captype bottleneck;
nodeptr *np;
/* 1. Finding bottleneck capacity */
/* 1a - the source tree */
bottleneck = *cap_middle;
for (i=s_start; ; ) {
a = i -> parent;
if (a == TERMINAL) break;
if (IS_ODD(a)) {
a = MAKE_EVEN(a);
if (bottleneck > a->r_cap) bottleneck = a -> r_cap;
i = NEIGHBOR_NODE_REV(i, a -> shift);
} else {
if (bottleneck > a->r_rev_cap) bottleneck = a -> r_rev_cap;
i = NEIGHBOR_NODE(i, a -> shift);
}
}
if (bottleneck > i->tr_cap) bottleneck = i -> tr_cap;
/* 1b - the sink tree */
for (i=t_start; ; ) {
a = i -> parent;
if (a == TERMINAL) break;
if (IS_ODD(a)) {
a = MAKE_EVEN(a);
if (bottleneck > a->r_rev_cap) bottleneck = a -> r_rev_cap;
i = NEIGHBOR_NODE_REV(i, a -> shift);
} else {
if (bottleneck > a->r_cap) bottleneck = a -> r_cap;
i = NEIGHBOR_NODE(i, a -> shift);
}
}
if (bottleneck > - i->tr_cap) bottleneck = - i -> tr_cap;
/* 2. Augmenting */
/* 2a - the source tree */
*rev_cap_middle += bottleneck;
*cap_middle -= bottleneck;
for (i=s_start; ; ) {
a = i -> parent;
if (a == TERMINAL) break;
if (IS_ODD(a)) {
a = MAKE_EVEN(a);
a -> r_rev_cap += bottleneck;
a -> r_cap -= bottleneck;
if (!a->r_cap) {
/* add i to the adoption list */
i -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = i;
np -> next = orphan_first;
orphan_first = np;
}
i = NEIGHBOR_NODE_REV(i, a -> shift);
} else {
a -> r_cap += bottleneck;
a -> r_rev_cap -= bottleneck;
if (!a->r_rev_cap) {
/* add i to the adoption list */
i -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = i;
np -> next = orphan_first;
orphan_first = np;
}
i = NEIGHBOR_NODE(i, a -> shift);
}
}
i -> tr_cap -= bottleneck;
if (!i->tr_cap) {
/* add i to the adoption list */
i -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = i;
np -> next = orphan_first;
orphan_first = np;
}
/* 2b - the sink tree */
for (i=t_start; ; ) {
a = i -> parent;
if (a == TERMINAL) break;
if (IS_ODD(a)) {
a = MAKE_EVEN(a);
a -> r_cap += bottleneck;
a -> r_rev_cap -= bottleneck;
if (!a->r_rev_cap) {
/* add i to the adoption list */
i -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = i;
np -> next = orphan_first;
orphan_first = np;
}
i = NEIGHBOR_NODE_REV(i, a -> shift);
} else {
a -> r_rev_cap += bottleneck;
a -> r_cap -= bottleneck;
if (!a->r_cap) {
/* add i to the adoption list */
i -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = i;
np -> next = orphan_first;
orphan_first = np;
}
i = NEIGHBOR_NODE(i, a -> shift);
}
}
i -> tr_cap += bottleneck;
if (!i->tr_cap) {
/* add i to the adoption list */
i -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = i;
np -> next = orphan_first;
orphan_first = np;
}
flow += bottleneck;
}
/***********************************************************************/
inline void Graph::process_source_orphan(node *i)
{
node *j;
arc_forward *a0_for, *a0_for_first, *a0_for_last;
arc_reverse *a0_rev, *a0_rev_first, *a0_rev_last;
arc_forward *a0_min = NULL, *a;
nodeptr *np;
int d, d_min = INFINITE_D;
/* trying to find a new parent */
a0_for_first = i -> first_out;
if (IS_ODD(a0_for_first)) {
a0_for_first = (arc_forward *) (((char *)a0_for_first) + 1);
a0_for_last = (arc_forward *) ((a0_for_first ++) -> shift);
} else a0_for_last = (i + 1) -> first_out;
a0_rev_first = i -> first_in;
if (IS_ODD(a0_rev_first)) {
a0_rev_first = (arc_reverse *) (((char *)a0_rev_first) + 1);
a0_rev_last = (arc_reverse *) ((a0_rev_first ++) -> sister);
} else a0_rev_last = (i + 1) -> first_in;
for (a0_for=a0_for_first; a0_for<a0_for_last; a0_for++)
if (a0_for->r_rev_cap) {
j = NEIGHBOR_NODE(i, a0_for -> shift);
if (!j->is_sink && (a=j->parent)) {
/* checking the origin of j */
d = 0;
while ( 1 ) {
if (j->TS == TIME) {
d += j -> DIST;
break;
}
a = j -> parent;
d ++;
if (a==TERMINAL) {
j -> TS = TIME;
j -> DIST = 1;
break;
}
if (a==ORPHAN) {
d = INFINITE_D;
break;
}
if (IS_ODD(a))
j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
else
j = NEIGHBOR_NODE(j, a -> shift);
}
if (d<INFINITE_D) { /* j originates from the source - done */
if (d<d_min) {
a0_min = a0_for;
d_min = d;
}
/* set marks along the path */
for (j=NEIGHBOR_NODE(i, a0_for->shift); j->TS!=TIME; ) {
j -> TS = TIME;
j -> DIST = d --;
a = j->parent;
if (IS_ODD(a))
j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
else
j = NEIGHBOR_NODE(j, a -> shift);
}
}
}
}
for (a0_rev=a0_rev_first; a0_rev<a0_rev_last; a0_rev++) {
a0_for = a0_rev -> sister;
if (a0_for->r_cap) {
j = NEIGHBOR_NODE_REV(i, a0_for -> shift);
if (!j->is_sink && (a=j->parent)) {
/* checking the origin of j */
d = 0;
while ( 1 ) {
if (j->TS == TIME) {
d += j -> DIST;
break;
}
a = j -> parent;
d ++;
if (a==TERMINAL) {
j -> TS = TIME;
j -> DIST = 1;
break;
}
if (a==ORPHAN) {
d = INFINITE_D;
break;
}
if (IS_ODD(a))
j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
else
j = NEIGHBOR_NODE(j, a -> shift);
}
if (d<INFINITE_D) { /* j originates from the source - done */
if (d<d_min) {
a0_min = MAKE_ODD(a0_for);
d_min = d;
}
/* set marks along the path */
for (j=NEIGHBOR_NODE_REV(i,a0_for->shift); j->TS!=TIME; ) {
j -> TS = TIME;
j -> DIST = d --;
a = j->parent;
if (IS_ODD(a))
j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
else
j = NEIGHBOR_NODE(j, a -> shift);
}
}
}
}
}
if ( (i->parent = a0_min) ) {
i -> TS = TIME;
i -> DIST = d_min + 1;
} else {
/* no parent is found */
i -> TS = 0;
/* process neighbors */
for (a0_for=a0_for_first; a0_for<a0_for_last; a0_for++) {
j = NEIGHBOR_NODE(i, a0_for -> shift);
if (!j->is_sink && (a=j->parent)) {
if (a0_for->r_rev_cap) set_active(j);
if (a!=TERMINAL && a!=ORPHAN && IS_ODD(a)
&& NEIGHBOR_NODE_REV(j, MAKE_EVEN(a)->shift)==i) {
/* add j to the adoption list */
j -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = j;
if (orphan_last) orphan_last -> next = np;
else orphan_first = np;
orphan_last = np;
np -> next = NULL;
}
}
}
for (a0_rev=a0_rev_first; a0_rev<a0_rev_last; a0_rev++) {
a0_for = a0_rev -> sister;
j = NEIGHBOR_NODE_REV(i, a0_for -> shift);
if (!j->is_sink && (a=j->parent)) {
if (a0_for->r_cap) set_active(j);
if (a!=TERMINAL && a!=ORPHAN && !IS_ODD(a) && NEIGHBOR_NODE(j, a->shift)==i) {
/* add j to the adoption list */
j -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = j;
if (orphan_last) orphan_last -> next = np;
else orphan_first = np;
orphan_last = np;
np -> next = NULL;
}
}
}
}
}
inline void Graph::process_sink_orphan(node *i)
{
node *j;
arc_forward *a0_for, *a0_for_first, *a0_for_last;
arc_reverse *a0_rev, *a0_rev_first, *a0_rev_last;
arc_forward *a0_min = NULL, *a;
nodeptr *np;
int d, d_min = INFINITE_D;
/* trying to find a new parent */
a0_for_first = i -> first_out;
if (IS_ODD(a0_for_first)) {
a0_for_first = (arc_forward *) (((char *)a0_for_first) + 1);
a0_for_last = (arc_forward *) ((a0_for_first ++) -> shift);
} else a0_for_last = (i + 1) -> first_out;
a0_rev_first = i -> first_in;
if (IS_ODD(a0_rev_first)) {
a0_rev_first = (arc_reverse *) (((char *)a0_rev_first) + 1);
a0_rev_last = (arc_reverse *) ((a0_rev_first ++) -> sister);
} else a0_rev_last = (i + 1) -> first_in;
for (a0_for=a0_for_first; a0_for<a0_for_last; a0_for++)
if (a0_for->r_cap) {
j = NEIGHBOR_NODE(i, a0_for -> shift);
if (j->is_sink && (a=j->parent)) {
/* checking the origin of j */
d = 0;
while ( 1 ) {
if (j->TS == TIME) {
d += j -> DIST;
break;
}
a = j -> parent;
d ++;
if (a==TERMINAL) {
j -> TS = TIME;
j -> DIST = 1;
break;
}
if (a==ORPHAN) {
d = INFINITE_D;
break;
}
if (IS_ODD(a))
j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
else
j = NEIGHBOR_NODE(j, a -> shift);
}
if (d<INFINITE_D) { /* j originates from the sink - done */
if (d<d_min) {
a0_min = a0_for;
d_min = d;
}
/* set marks along the path */
for (j=NEIGHBOR_NODE(i, a0_for->shift); j->TS!=TIME; ) {
j -> TS = TIME;
j -> DIST = d --;
a = j->parent;
if (IS_ODD(a))
j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
else
j = NEIGHBOR_NODE(j, a -> shift);
}
}
}
}
for (a0_rev=a0_rev_first; a0_rev<a0_rev_last; a0_rev++) {
a0_for = a0_rev -> sister;
if (a0_for->r_rev_cap) {
j = NEIGHBOR_NODE_REV(i, a0_for -> shift);
if (j->is_sink && (a=j->parent)) {
/* checking the origin of j */
d = 0;
while ( 1 ) {
if (j->TS == TIME) {
d += j -> DIST;
break;
}
a = j -> parent;
d ++;
if (a==TERMINAL) {
j -> TS = TIME;
j -> DIST = 1;
break;
}
if (a==ORPHAN) {
d = INFINITE_D;
break;
}
if (IS_ODD(a))
j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
else
j = NEIGHBOR_NODE(j, a -> shift);
}
if (d<INFINITE_D) { /* j originates from the sink - done */
if (d<d_min) {
a0_min = MAKE_ODD(a0_for);
d_min = d;
}
/* set marks along the path */
for (j=NEIGHBOR_NODE_REV(i,a0_for->shift); j->TS!=TIME; ) {
j -> TS = TIME;
j -> DIST = d --;
a = j->parent;
if (IS_ODD(a))
j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
else
j = NEIGHBOR_NODE(j, a -> shift);
}
}
}
}
}
if ( (i->parent = a0_min) ) {
i -> TS = TIME;
i -> DIST = d_min + 1;
} else {
/* no parent is found */
i -> TS = 0;
/* process neighbors */
for (a0_for=a0_for_first; a0_for<a0_for_last; a0_for++) {
j = NEIGHBOR_NODE(i, a0_for -> shift);
if (j->is_sink && (a=j->parent)) {
if (a0_for->r_cap) set_active(j);
if (a!=TERMINAL && a!=ORPHAN && IS_ODD(a)
&& NEIGHBOR_NODE_REV(j, MAKE_EVEN(a)->shift)==i) {
/* add j to the adoption list */
j -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = j;
if (orphan_last) orphan_last -> next = np;
else orphan_first = np;
orphan_last = np;
np -> next = NULL;
}
}
}
for (a0_rev=a0_rev_first; a0_rev<a0_rev_last; a0_rev++) {
a0_for = a0_rev -> sister;
j = NEIGHBOR_NODE_REV(i, a0_for -> shift);
if (j->is_sink && (a=j->parent)) {
if (a0_for->r_rev_cap) set_active(j);
if (a!=TERMINAL && a!=ORPHAN && !IS_ODD(a) && NEIGHBOR_NODE(j, a->shift)==i) {
/* add j to the adoption list */
j -> parent = ORPHAN;
np = nodeptr_block -> New();
np -> ptr = j;
if (orphan_last) orphan_last -> next = np;
else orphan_first = np;
orphan_last = np;
np -> next = NULL;
}
}
}
}
}
/***********************************************************************/
inline Graph::flowtype Graph::maxflow()
{
node *i, *j, *current_node = NULL, *s_start, *t_start=NULL;
captype *cap_middle=NULL, *rev_cap_middle=NULL;
arc_forward *a_for, *a_for_first, *a_for_last;
arc_reverse *a_rev, *a_rev_first, *a_rev_last;
nodeptr *np, *np_next;
prepare_graph();
maxflow_init();
nodeptr_block = new DBlock<nodeptr>(NODEPTR_BLOCK_SIZE, error_function);
while ( 1 ) {
if ( (i=current_node) ) {
i -> next = NULL; /* remove active flag */
if (!i->parent) i = NULL;
}
if (!i) {
if (!(i = next_active())) break;
}
/* growth */
s_start = NULL;
a_for_first = i -> first_out;
if (IS_ODD(a_for_first)) {
a_for_first = (arc_forward *) (((char *)a_for_first) + 1);
a_for_last = (arc_forward *) ((a_for_first ++) -> shift);
} else a_for_last = (i + 1) -> first_out;
a_rev_first = i -> first_in;
if (IS_ODD(a_rev_first)) {
a_rev_first = (arc_reverse *) (((char *)a_rev_first) + 1);
a_rev_last = (arc_reverse *) ((a_rev_first ++) -> sister);
} else a_rev_last = (i + 1) -> first_in;
if (!i->is_sink) {
/* grow source tree */
for (a_for=a_for_first; a_for<a_for_last; a_for++)
if (a_for->r_cap) {
j = NEIGHBOR_NODE(i, a_for -> shift);
if (!j->parent) {
j -> is_sink = 0;
j -> parent = MAKE_ODD(a_for);
j -> TS = i -> TS;
j -> DIST = i -> DIST + 1;
set_active(j);
} else if (j->is_sink) {
s_start = i;
t_start = j;
cap_middle = & ( a_for -> r_cap );
rev_cap_middle = & ( a_for -> r_rev_cap );
break;
} else if (j->TS <= i->TS &&
j->DIST > i->DIST) {
/* heuristic - trying to make the distance from j to the source shorter */
j -> parent = MAKE_ODD(a_for);
j -> TS = i -> TS;
j -> DIST = i -> DIST + 1;
}
}
if (!s_start)
for (a_rev=a_rev_first; a_rev<a_rev_last; a_rev++) {
a_for = a_rev -> sister;
if (a_for->r_rev_cap) {
j = NEIGHBOR_NODE_REV(i, a_for -> shift);
if (!j->parent) {
j -> is_sink = 0;
j -> parent = a_for;
j -> TS = i -> TS;
j -> DIST = i -> DIST + 1;
set_active(j);
} else if (j->is_sink) {
s_start = i;
t_start = j;
cap_middle = & ( a_for -> r_rev_cap );
rev_cap_middle = & ( a_for -> r_cap );
break;
} else if (j->TS <= i->TS &&
j->DIST > i->DIST) {
/* heuristic - trying to make the distance from j to the source shorter */
j -> parent = a_for;
j -> TS = i -> TS;
j -> DIST = i -> DIST + 1;
}
}
}
} else {
/* grow sink tree */
for (a_for=a_for_first; a_for<a_for_last; a_for++)
if (a_for->r_rev_cap) {
j = NEIGHBOR_NODE(i, a_for -> shift);
if (!j->parent) {
j -> is_sink = 1;
j -> parent = MAKE_ODD(a_for);
j -> TS = i -> TS;
j -> DIST = i -> DIST + 1;
set_active(j);
} else if (!j->is_sink) {
s_start = j;
t_start = i;
cap_middle = & ( a_for -> r_rev_cap );
rev_cap_middle = & ( a_for -> r_cap );
break;
} else if (j->TS <= i->TS &&
j->DIST > i->DIST) {
/* heuristic - trying to make the distance from j to the sink shorter */
j -> parent = MAKE_ODD(a_for);
j -> TS = i -> TS;
j -> DIST = i -> DIST + 1;
}
}
for (a_rev=a_rev_first; a_rev<a_rev_last; a_rev++) {
a_for = a_rev -> sister;
if (a_for->r_cap) {
j = NEIGHBOR_NODE_REV(i, a_for -> shift);
if (!j->parent) {
j -> is_sink = 1;
j -> parent = a_for;
j -> TS = i -> TS;
j -> DIST = i -> DIST + 1;
set_active(j);
} else if (!j->is_sink) {
s_start = j;
t_start = i;
cap_middle = & ( a_for -> r_cap );
rev_cap_middle = & ( a_for -> r_rev_cap );
break;
} else if (j->TS <= i->TS &&
j->DIST > i->DIST) {
/* heuristic - trying to make the distance from j to the sink shorter */
j -> parent = a_for;
j -> TS = i -> TS;
j -> DIST = i -> DIST + 1;
}
}
}
}
TIME ++;
if (s_start) {
i -> next = i; /* set active flag */
current_node = i;
/* augmentation */
augment(s_start, t_start, cap_middle, rev_cap_middle);
/* augmentation end */
/* adoption */
while ( (np=orphan_first) ) {
np_next = np -> next;
np -> next = NULL;
while ( (np=orphan_first) ) {
orphan_first = np -> next;
i = np -> ptr;
nodeptr_block -> Delete(np);
if (!orphan_first) orphan_last = NULL;
if (i->is_sink) process_sink_orphan(i);
else process_source_orphan(i);
}
orphan_first = np_next;
}
/* adoption end */
} else current_node = NULL;
}
delete nodeptr_block;
return flow;
}
/***********************************************************************/
inline Graph::termtype Graph::what_segment(node_id i)
{
if (((node*)i)->parent && !((node*)i)->is_sink) return SOURCE;
return SINK;
}
#endif
|