File: flow.c

package info (click to toggle)
r-cran-igraph 0.7.1-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 14,280 kB
  • sloc: ansic: 150,105; cpp: 19,404; fortran: 3,777; yacc: 1,164; tcl: 931; lex: 484; makefile: 13; sh: 9
file content (2476 lines) | stat: -rw-r--r-- 85,926 bytes parent folder | download | duplicates (4)
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
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2006-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA
   
   This program is free software; 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 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include "igraph_flow.h"
#include "igraph_error.h"
#include "igraph_memory.h"
#include "igraph_constants.h"
#include "igraph_interface.h"
#include "igraph_adjlist.h"
#include "igraph_conversion.h"
#include "igraph_constructors.h"
#include "igraph_progress.h"
#include "igraph_structural.h"
#include "igraph_components.h"
#include "igraph_types_internal.h"
#include "config.h"
#include "igraph_math.h"
#include "igraph_dqueue.h"
#include "igraph_visitor.h"
#include "igraph_interrupt_internal.h"
#include "igraph_topology.h"

#include <limits.h>
#include <stdio.h>

/*
 * Some general remarks about the functions in this file.
 *
 * The following measures can be calculated:
 * ( 1) s-t maximum flow value, directed graph
 * ( 2) s-t maximum flow value, undirected graph
 * ( 3) s-t maximum flow, directed graph
 * ( 4) s-t maximum flow, undirected graph
 * ( 5) s-t minimum cut value, directed graph
 * ( 6) s-t minimum cut value, undirected graph
 * ( 7) minimum cut value, directed graph
 * ( 8) minimum cut value, undirected graph
 * ( 9) s-t minimum cut, directed graph
 * (10) s-t minimum cut, undirected graph
 * (11) minimum cut, directed graph
 * (12) minimum cut, undirected graph
 * (13) s-t edge connectivity, directed graph
 * (14) s-t edge connectivity, undirected graph
 * (15) edge connectivity, directed graph
 * (16) edge connectivity, undirected graph
 * (17) s-t vertex connectivity, directed graph
 * (18) s-t vertex connectivity, undirected graph
 * (19) vertex connectivity, directed graph
 * (20) vertex connectivity, undirected graph
 * (21) s-t number of edge disjoint paths, directed graph
 * (22) s-t number of edge disjoint paths, undirected graph
 * (23) s-t number of vertex disjoint paths, directed graph
 * (24) s-t number of vertex disjoint paths, undirected graph
 * (25) graph adhesion, directed graph
 * (26) graph adhesion, undirected graph
 * (27) graph cohesion, directed graph
 * (28) graph cohesion, undirected graph
 * 
 * This is how they are calculated:
 * ( 1) igraph_maxflow_value, calls igraph_maxflow.
 * ( 2) igraph_maxflow_value, calls igraph_maxflow, this calls
 *      igraph_i_maxflow_undirected. This transforms the graph into a
 *      directed graph, including two mutual edges instead of every
 *      undirected edge, then igraph_maxflow is called again with the
 *      directed graph.
 * ( 3) igraph_maxflow, does the push-relabel algorithm, optionally
 *      calculates the cut, the partitions and the flow itself.
 * ( 4) igraph_maxflow calls igraph_i_maxflow_undirected, this converts 
 *      the undirected graph into a directed one, adding two mutual edges
 *      for each undirected edge, then igraph_maxflow is called again, 
 *      with the directed graph. After igraph_maxflow returns, we need 
 *      to edit the flow (and the cut) to make it sense for the
 *      original graph.
 * ( 5) igraph_st_mincut_value, we just call igraph_maxflow_value
 * ( 6) igraph_st_mincut_value, we just call igraph_maxflow_value
 * ( 7) igraph_mincut_value, we call igraph_maxflow_value (|V|-1)*2
 *      times, from vertex 0 to all other vertices and from all other
 *      vertices to vertex 0
 * ( 8) We call igraph_i_mincut_value_undirected, that calls 
 *      igraph_i_mincut_undirected with partition=partition2=cut=NULL
 *      The Stoer-Wagner algorithm is used.
 * ( 9) igraph_st_mincut, just calls igraph_maxflow.
 * (10) igraph_st_mincut, just calls igraph_maxflow.
 * (11) igraph_mincut, calls igraph_i_mincut_directed, which runs 
 *      the maximum flow algorithm 2(|V|-1) times, from vertex zero to
 *      and from all other vertices and stores the smallest cut.
 * (12) igraph_mincut, igraph_i_mincut_undirected is called, 
 *      this is the Stoer-Wagner algorithm
 * (13) We just call igraph_maxflow_value, back to (1)
 * (14) We just call igraph_maxflow_value, back to (2)
 * (15) We just call igraph_mincut_value (possibly after some basic
 *      checks). Back to (7)
 * (16) We just call igraph_mincut_value (possibly after some basic
 *      checks). Back to (8).
 * (17) We call igraph_i_st_vertex_connectivity_directed.
 *      That creates a new graph with 2*|V| vertices and smartly chosen
 *      edges, so that the s-t edge connectivity of this graph is the
 *      same as the s-t vertex connectivity of the original graph.
 *      So finally it calls igraph_maxflow_value, go to (1)
 * (18) We call igraph_i_st_vertex_connectivity_undirected.
 *      We convert the graph to a directed one,
 *      IGRAPH_TO_DIRECTED_MUTUAL method. Then we call 
 *      igraph_i_st_vertex_connectivity_directed, see (17).
 * (19) We call igraph_i_vertex_connectivity_directed.
 *      That calls igraph_st_vertex_connectivity for all pairs of
 *      vertices. Back to (17).
 * (20) We call igraph_i_vertex_connectivity_undirected.
 *      That converts the graph into a directed one
 *      (IGRAPH_TO_DIRECTED_MUTUAL) and calls the directed version,
 *      igraph_i_vertex_connectivity_directed, see (19).
 * (21) igraph_edge_disjoint_paths, we just call igraph_maxflow_value, (1).
 * (22) igraph_edge_disjoint_paths, we just call igraph_maxflow_value, (2).
 * (23) igraph_vertex_disjoint_paths, if there is a connection between
 *      the two vertices, then we remove that (or all of them if there
 *      are many), as this could mess up vertex connectivity
 *      calculation. The we call
 *      igraph_i_st_vertex_connectivity_directed, see (19).
 * (24) igraph_vertex_disjoint_paths, if there is a connection between
 *      the two vertices, then we remove that (or all of them if there
 *      are many), as this could mess up vertex connectivity
 *      calculation. The we call
 *      igraph_i_st_vertex_connectivity_undirected, see (20).
 * (25) We just call igraph_edge_connectivity, see (15). 
 * (26) We just call igraph_edge_connectivity, see (16).
 * (27) We just call igraph_vertex_connectivity, see (19).
 * (28) We just call igraph_vertex_connectivity, see (20).
 */

/*
 * This is an internal function that calculates the maximum flow value
 * on undirected graphs, either for an s-t vertex pair or for the
 * graph (i.e. all vertex pairs). 
 * 
 * It does it by converting the undirected graph to a corresponding
 * directed graph, including reciprocal directed edges instead of each
 * undirected edge.
 */

int igraph_i_maxflow_undirected(const igraph_t *graph, 
				igraph_real_t *value,
				igraph_vector_t *flow,
				igraph_vector_t *cut,
				igraph_vector_t *partition,
				igraph_vector_t *partition2,
				igraph_integer_t source, 
				igraph_integer_t target,
				const igraph_vector_t *capacity,
				igraph_maxflow_stats_t *stats) {
  igraph_integer_t no_of_edges=(igraph_integer_t) igraph_ecount(graph);
  igraph_integer_t no_of_nodes=(igraph_integer_t) igraph_vcount(graph);
  igraph_vector_t edges;
  igraph_vector_t newcapacity;
  igraph_t newgraph;
  long int i;
  
  /* We need to convert this to directed by hand, since we need to be
     sure that the edge ids will be handled properly to build the new
     capacity vector. */

  IGRAPH_VECTOR_INIT_FINALLY(&edges, 0);
  IGRAPH_VECTOR_INIT_FINALLY(&newcapacity, no_of_edges*2);
  IGRAPH_CHECK(igraph_vector_reserve(&edges, no_of_edges*4));
  IGRAPH_CHECK(igraph_get_edgelist(graph, &edges, 0));
  IGRAPH_CHECK(igraph_vector_resize(&edges, no_of_edges*4));
  for (i=0; i<no_of_edges; i++) {
    VECTOR(edges)[no_of_edges*2+i*2] = VECTOR(edges)[i*2+1];
    VECTOR(edges)[no_of_edges*2+i*2+1] = VECTOR(edges)[i*2];
    VECTOR(newcapacity)[i] = VECTOR(newcapacity)[no_of_edges+i] = 
      capacity ? VECTOR(*capacity)[i] : 1.0;
  }
  
  IGRAPH_CHECK(igraph_create(&newgraph, &edges, no_of_nodes, IGRAPH_DIRECTED));
  IGRAPH_FINALLY(igraph_destroy, &newgraph);
  
  IGRAPH_CHECK(igraph_maxflow(&newgraph, value, flow, cut, partition,
			      partition2, source, target, &newcapacity, stats));

  if (cut) {
    long int i, cs=igraph_vector_size(cut);
    for (i=0; i<cs; i++) {
      if (VECTOR(*cut)[i] >= no_of_edges) {
	VECTOR(*cut)[i] -= no_of_edges;
      }
    }
  }
  
  /* The flow has one non-zero value for each real-nonreal edge pair,
     by definition, we convert it to a positive-negative vector. If
     for an edge the flow is negative that means that it is going
     from the bigger vertex id to the smaller one. For positive
     values the direction is the opposite. */
  if (flow) {
    long int i;
    for (i=0; i<no_of_edges; i++) {
      VECTOR(*flow)[i] -= VECTOR(*flow)[i+no_of_edges];
    }
    IGRAPH_CHECK(igraph_vector_resize(flow, no_of_edges));
  }
  
  igraph_destroy(&newgraph);
  igraph_vector_destroy(&edges);
  igraph_vector_destroy(&newcapacity);
  IGRAPH_FINALLY_CLEAN(3);
  
  return 0;
}

#define FIRST(i)       (VECTOR(*first)[(i)])
#define LAST(i)        (VECTOR(*first)[(i)+1])
#define CURRENT(i)     (VECTOR(*current)[(i)])
#define RESCAP(i)      (VECTOR(*rescap)[(i)])
#define REV(i)         (VECTOR(*rev)[(i)])
#define HEAD(i)        (VECTOR(*to)[(i)])
#define EXCESS(i)      (VECTOR(*excess)[(i)])
#define DIST(i)        (VECTOR(*distance)[(i)])
#define DISCHARGE(v)   (igraph_i_mf_discharge((v), &current, &first, &rescap, \
					      &to, &distance, &excess,	      \
					      no_of_nodes, source, target,    \
					      &buckets, &ibuckets,	      \
					      &rev, stats, &npushsince,       \
					      &nrelabelsince))
#define PUSH(v,e,n)    (igraph_i_mf_push((v), (e), (n), current, rescap,      \
					 excess, target, source, buckets,     \
					 ibuckets, distance, rev, stats,      \
					 npushsince))
#define RELABEL(v)     (igraph_i_mf_relabel((v), no_of_nodes, distance,	      \
					    first, rescap, to, current,       \
					    stats, nrelabelsince))
#define GAP(b)         (igraph_i_mf_gap((b), stats, buckets, ibuckets,        \
					no_of_nodes, distance))
#define BFS()          (igraph_i_mf_bfs(&bfsq, source, target, no_of_nodes,   \
					&buckets, &ibuckets, &distance,       \
					&first,	&current, &to, &excess,       \
					&rescap, &rev))

void igraph_i_mf_gap(long int b, igraph_maxflow_stats_t *stats,
		     igraph_buckets_t *buckets, igraph_dbuckets_t *ibuckets,
		     long int no_of_nodes,
		     igraph_vector_long_t *distance) {

  long int bo;
  (stats->nogap)++;
  for (bo=b+1; bo <= no_of_nodes; bo++) {
    while (!igraph_dbuckets_empty_bucket(ibuckets, bo)) {
      long int n=igraph_dbuckets_pop(ibuckets, bo);
      (stats->nogapnodes)++;
      DIST(n)=no_of_nodes;
    }
  }
}

void igraph_i_mf_relabel(long int v, long int no_of_nodes,
			 igraph_vector_long_t *distance,
			 igraph_vector_long_t *first,
			 igraph_vector_t *rescap, igraph_vector_long_t *to,
			 igraph_vector_long_t *current,
			 igraph_maxflow_stats_t *stats, int *nrelabelsince) {

    long int min=no_of_nodes;
    long int k, l, min_edge=0;
    (stats->norelabel)++; (*nrelabelsince)++;
    DIST(v)=no_of_nodes;
    for (k=FIRST(v), l=LAST(v); k<l; k++) {
      if (RESCAP(k) > 0 && DIST(HEAD(k)) < min) {
	min=DIST(HEAD(k));
	min_edge=k;
      }
    }
    min++;
    if (min < no_of_nodes) {
      DIST(v) = min;
      CURRENT(v) = min_edge;
    }
}

void igraph_i_mf_push(long int v, long int e, long int n,
		      igraph_vector_long_t *current,
		      igraph_vector_t *rescap, igraph_vector_t *excess,
		      long int target, long int source,
		      igraph_buckets_t *buckets, igraph_dbuckets_t *ibuckets,
		      igraph_vector_long_t *distance,
		      igraph_vector_long_t *rev, igraph_maxflow_stats_t *stats,
		      int *npushsince) {
  igraph_real_t delta=
    RESCAP(e) < EXCESS(v) ? RESCAP(e) : EXCESS(v);
  (stats->nopush)++; (*npushsince)++;
  if (EXCESS(n) == 0 && n != target) {
    igraph_dbuckets_delete(ibuckets, DIST(n), n);
    igraph_buckets_add(buckets, (long int) DIST(n), n);
  }
  RESCAP(e) -= delta;
  RESCAP(REV(e)) += delta;
  EXCESS(n) += delta;
  EXCESS(v) -= delta;
}

void igraph_i_mf_discharge(long int v,
			   igraph_vector_long_t *current,
			   igraph_vector_long_t *first,
			   igraph_vector_t *rescap,
			   igraph_vector_long_t *to,
			   igraph_vector_long_t *distance,
			   igraph_vector_t *excess,
			   long int no_of_nodes, long int source,
			   long int target, igraph_buckets_t *buckets,
			   igraph_dbuckets_t *ibuckets,
			   igraph_vector_long_t *rev,
			   igraph_maxflow_stats_t *stats,
			   int *npushsince, int *nrelabelsince) {
  do {
    long int i;
    long int start=(long int) CURRENT(v);
    long int stop =(long int) LAST(v);
    for (i = start; i < stop; i++) {
      if (RESCAP(i) > 0) {
	long int nei=HEAD(i);
	if (DIST(v) == DIST(nei)+1) {
	  PUSH((v), i, nei);
	  if (EXCESS(v) == 0) { break; }
	}
      }
    }
    if (i == stop) {
      long int origdist=DIST(v);
      RELABEL(v);
      if (igraph_buckets_empty_bucket(buckets, origdist) &&
	  igraph_dbuckets_empty_bucket(ibuckets, origdist)) {
	GAP(origdist);
      }
      if (DIST(v) == no_of_nodes) { break; }
    } else {
      CURRENT(v) = i;
      igraph_dbuckets_add(ibuckets, DIST(v), v);
      break;
    }
  } while (1);
}

void igraph_i_mf_bfs(igraph_dqueue_long_t *bfsq,
		     long int source, long int target,
		     long int no_of_nodes, igraph_buckets_t *buckets,
		     igraph_dbuckets_t *ibuckets,
		     igraph_vector_long_t *distance,
		     igraph_vector_long_t *first, igraph_vector_long_t *current,
		     igraph_vector_long_t *to, igraph_vector_t *excess,
		     igraph_vector_t *rescap, igraph_vector_long_t *rev) {

  long int k, l;

  igraph_buckets_clear(buckets);
  igraph_dbuckets_clear(ibuckets);
  igraph_vector_long_fill(distance, no_of_nodes);
  DIST(target) = 0;

  igraph_dqueue_long_push(bfsq, target);
  while (!igraph_dqueue_long_empty(bfsq)) {
    long int node=igraph_dqueue_long_pop(bfsq);
    long int ndist=DIST(node)+1;
    for (k=FIRST(node), l=LAST(node); k<l; k++) {
      if (RESCAP(REV(k)) > 0) {
	long int nei=HEAD(k);
	if (DIST(nei) == no_of_nodes) {
	  DIST(nei) = ndist;
	  CURRENT(nei) = FIRST(nei);
	  if (EXCESS(nei) > 0) {
	    igraph_buckets_add(buckets, ndist, nei);
	  } else {
	    igraph_dbuckets_add(ibuckets, ndist, nei);
	  }
	  igraph_dqueue_long_push(bfsq, nei);
	}
      }
    }
  }
}

/**
 * \function igraph_maxflow
 * Maximum network flow between a pair of vertices
 * 
 * </para><para>This function implements the Goldberg-Tarjan algorithm for
 * calculating value of the maximum flow in a directed or undirected
 * graph. The algorithm was given in Andrew V. Goldberg, Robert
 * E. Tarjan: A New Approach to the Maximum-Flow Problem, Journal of
 * the ACM, 35(4), 921-940, 1988. </para>
 * 
 * <para> The input of the function is a graph, a vector
 * of real numbers giving the capacity of the edges and two vertices
 * of the graph, the source and the target. A flow is a function 
 * assigning positive real numbers to the edges and satisfying two
 * requirements: (1) the flow value is less than the capacity of the
 * edge and (2) at each vertex except the source and the target, the
 * incoming flow (ie. the sum of the flow on the incoming edges) is
 * the same as the outgoing flow (ie. the sum of the flow on the
 * outgoing edges). The value of the flow is the incoming flow at the
 * target vertex. The maximum flow is the flow with the maximum
 * value.
 * 
 * \param graph The input graph, either directed or undirected.
 * \param value Pointer to a real number, the value of the maximum
 *        will be placed here, unless it is a null pointer.
 * \param flow If not a null pointer, then it must be a pointer to an
 *        initialized vector. The vector will be resized, and the flow
 *        on each edge will be placed in it, in the order of the edge
 *        ids. For undirected graphs this argument is bit trickier,
 *        since for these the flow direction is not predetermined by
 *        the edge direction. For these graphs the elements of the
 *        \p flow vector can be negative, this means that the flow
 *        goes from the bigger vertex id to the smaller one. Positive
 *        values mean that the flow goes from the smaller vertex id to
 *        the bigger one.
 * \param cut A null pointer or a pointer to an initialized vector. 
 *        If not a null pointer, then the minimum cut corresponding to
 *        the maximum flow is stored here, i.e. all edge ids that are
 *        part of the minimum cut are stored in the vector.
 * \param partition A null pointer or a pointer to an initialized
 *        vector. If not a null pointer, then the first partition of
 *        the minimum cut that corresponds to the maximum flow will be
 *        placed here. The first partition is always the one that
 *        contains the source vertex.
 * \param partition2 A null pointer or a pointer to an initialized
 *        vector. If not a null pointer, then the second partition of
 *        the minimum cut that corresponds to the maximum flow will be
 *        placed here. The second partition is always the one that
 *        contains the target vertex.
 * \param source The id of the source vertex.
 * \param target The id of the target vertex.
 * \param capacity Vector containing the capacity of the edges. If NULL, then
 *        every edge is considered to have capacity 1.0.
 * \param stats Counts of the number of different operations
 *        preformed by the algorithm are stored here.
 * \return Error code.
 * 
 * Time complexity: O(|V|^3). In practice it is much faster, but i
 * cannot prove a better lower bound for the data structure i've
 * used. In fact, this implementation runs much faster than the
 * \c hi_pr implementation discussed in
 * B. V. Cherkassky and A. V. Goldberg: On implementing the 
 * push-relabel method for the maximum flow problem, (Algorithmica, 
 * 19:390--410, 1997) on all the graph classes i've tried.
 * 
 * \sa \ref igraph_mincut_value(), \ref igraph_edge_connectivity(),
 * \ref igraph_vertex_connectivity() for 
 * properties based on the maximum flow.
 * 
 * \example examples/simple/flow.c
 * \example examples/simple/flow2.c
 */

int igraph_maxflow(const igraph_t *graph, igraph_real_t *value,
		   igraph_vector_t *flow, igraph_vector_t *cut,
		   igraph_vector_t *partition, igraph_vector_t *partition2,
		   igraph_integer_t source, igraph_integer_t target,
		   const igraph_vector_t *capacity,
		   igraph_maxflow_stats_t *stats) {

  igraph_integer_t no_of_nodes=(igraph_integer_t) igraph_vcount(graph);
  igraph_integer_t no_of_orig_edges=(igraph_integer_t) igraph_ecount(graph);
  igraph_integer_t no_of_edges=2*no_of_orig_edges;

  igraph_vector_t rescap, excess;
  igraph_vector_long_t from, to, rev, distance;
  igraph_vector_t edges, rank;
  igraph_vector_long_t current, first;
  igraph_buckets_t buckets;
  igraph_dbuckets_t ibuckets;

  igraph_dqueue_long_t bfsq;

  long int i, j, idx;
  int npushsince=0, nrelabelsince=0;

  igraph_maxflow_stats_t local_stats;   /* used if the user passed a null pointer for stats */

  if (stats == 0) {
    stats = &local_stats;
  }

  if (!igraph_is_directed(graph)) {
    IGRAPH_CHECK(igraph_i_maxflow_undirected(graph, value, flow, cut,
					     partition, partition2, source,
					     target, capacity, stats));
    return 0;
  }

  if (capacity && igraph_vector_size(capacity) != no_of_orig_edges) {
    IGRAPH_ERROR("Invalid capacity vector", IGRAPH_EINVAL);
  }
  if (source<0 || source>=no_of_nodes || target<0 || target>=no_of_nodes) {
    IGRAPH_ERROR("Invalid source or target vertex", IGRAPH_EINVAL);
  }

  stats->nopush = stats->norelabel = stats->nogap = stats->nogapnodes =
    stats->nobfs = 0;

  /* 
   * The data structure:
   * - First of all, we consider every edge twice, first the edge
   *   itself, but also its opposite.
   * - (from, to) contain all edges (original + opposite), ordered by 
   *   the id of the source vertex. During the algorithm we just need
   *   'to', so from is destroyed soon. We only need it in the
   *   beginning, to create the 'first' pointers.
   * - 'first' is a pointer vector for 'to', first[i] points to the
   *   first neighbor of vertex i and first[i+1]-1 is the last
   *   neighbor of vertex i. (Unless vertex i is isolate, in which
   *   case first[i]==first[i+1]).
   * - 'rev' contains a mapping from an edge to its opposite pair
   * - 'rescap' contains the residual capacities of the edges, this is
   *   initially equal to the capacity of the edges for the original
   *   edges and it is zero for the opposite edges.
   * - 'excess' contains the excess flow for the vertices. I.e. the flow
   *   that is coming in, but it is not going out.
   * - 'current' stores the next neighboring vertex to check, for every
   *   vertex, when excess flow is being pushed to neighbors.
   * - 'distance' stores the distance of the vertices from the source.
   * - 'rank' and 'edges' are only needed temporarily, for ordering and
   *   storing the edges.
   * - we use an igraph_buckets_t data structure ('buckets') to find
   *   the vertices with the highest 'distance' values quickly.
   *   This always contains the vertices that have a positive excess
   *   flow.
   */
#undef FIRST
#undef LAST
#undef CURRENT
#undef RESCAP
#undef REV
#undef HEAD
#undef EXCESS
#undef DIST
#define FIRST(i)       (VECTOR(first)[(i)])
#define LAST(i)        (VECTOR(first)[(i)+1])
#define CURRENT(i)     (VECTOR(current)[(i)])
#define RESCAP(i)      (VECTOR(rescap)[(i)])
#define REV(i)         (VECTOR(rev)[(i)])
#define HEAD(i)        (VECTOR(to)[(i)])
#define EXCESS(i)      (VECTOR(excess)[(i)])
#define DIST(i)        (VECTOR(distance)[(i)])

  igraph_dqueue_long_init(&bfsq,             no_of_nodes);
  IGRAPH_FINALLY(igraph_dqueue_long_destroy, &bfsq);
  IGRAPH_VECTOR_LONG_INIT_FINALLY(&to,       no_of_edges);
  IGRAPH_VECTOR_LONG_INIT_FINALLY(&rev,      no_of_edges);
  IGRAPH_VECTOR_INIT_FINALLY(&rescap,        no_of_edges);
  IGRAPH_VECTOR_INIT_FINALLY(&excess,        no_of_nodes);
  IGRAPH_VECTOR_LONG_INIT_FINALLY(&distance, no_of_nodes);
  IGRAPH_VECTOR_LONG_INIT_FINALLY(&first,    no_of_nodes+1);

  IGRAPH_VECTOR_INIT_FINALLY(&rank,          no_of_edges);
  IGRAPH_VECTOR_LONG_INIT_FINALLY(&from,     no_of_edges);
  IGRAPH_VECTOR_INIT_FINALLY(&edges,         no_of_edges);
  
  /* Create the basic data structure */
  IGRAPH_CHECK(igraph_get_edgelist(graph, &edges, 0));
  IGRAPH_CHECK(igraph_vector_rank(&edges, &rank, no_of_nodes));
  
  for (i=0; i<no_of_edges; i+=2) {
    long int pos=(long int) VECTOR(rank)[i];
    long int pos2=(long int) VECTOR(rank)[i+1];
    VECTOR(from)[pos] = VECTOR(edges)[i];
    VECTOR(to)[pos]   = VECTOR(edges)[i+1];
    VECTOR(from)[pos2] = VECTOR(edges)[i+1];
    VECTOR(to)[pos2]   = VECTOR(edges)[i];
    VECTOR(rev)[pos] = pos2;
    VECTOR(rev)[pos2] = pos;
    VECTOR(rescap)[pos] = capacity ? VECTOR(*capacity)[i/2] : 1.0;
    VECTOR(rescap)[pos2] = 0.0;
  }  
 
  /* The first pointers. This is a but trickier, than one would
     think, because of the possible isolate vertices. */
  
  idx=-1;
  for (i=0; i<=VECTOR(from)[0]; i++) {
    idx++; VECTOR(first)[idx]=0;
  }
  for (i=1; i<no_of_edges; i++) {
    long int n=(long int) (VECTOR(from)[i] - 
			   VECTOR(from)[ (long int) VECTOR(first)[idx] ]);
    for (j=0; j<n; j++) {
      idx++; VECTOR(first)[idx]=i;
    }
  }
  idx++;
  while (idx < no_of_nodes+1) {
    VECTOR(first)[idx++] = no_of_edges;
  }

  igraph_vector_long_destroy(&from);
  igraph_vector_destroy(&edges);
  IGRAPH_FINALLY_CLEAN(2);

  if (!flow) {
    igraph_vector_destroy(&rank);
    IGRAPH_FINALLY_CLEAN(1);
  }

  /* And the current pointers, initially the same as the first */
  IGRAPH_VECTOR_LONG_INIT_FINALLY(&current, no_of_nodes);
  for (i=0; i<no_of_nodes; i++) {
    VECTOR(current)[i] = VECTOR(first)[i];
  }

  /* OK, the graph is set up, initialization */

  IGRAPH_CHECK(igraph_buckets_init(&buckets, no_of_nodes+1, no_of_nodes));
  IGRAPH_FINALLY(igraph_buckets_destroy, &buckets);
  IGRAPH_CHECK(igraph_dbuckets_init(&ibuckets, no_of_nodes+1, no_of_nodes));
  IGRAPH_FINALLY(igraph_dbuckets_destroy, &ibuckets);

  /* Send as much flow as possible from the source to its neighbors */
  for (i=FIRST(source), j=LAST(source); i<j; i++) {
    if (HEAD(i) != source) {
      igraph_real_t delta=RESCAP(i);
      RESCAP(i) = 0;
      RESCAP(REV(i)) += delta;
      EXCESS(HEAD(i)) += delta;
    }
  }

  BFS();
  (stats->nobfs)++;

  while (!igraph_buckets_empty(&buckets)) {
    long int vertex=igraph_buckets_popmax(&buckets);
    DISCHARGE(vertex);
    if (npushsince > no_of_nodes / 2 && nrelabelsince > no_of_nodes) {
      (stats->nobfs)++;
      BFS();
      npushsince = nrelabelsince = 0;
    }
  }

  /* Store the result */
  if (value) {
    *value=EXCESS(target);
  }  

  /* If we also need the minimum cut */
  if (cut || partition || partition2) {
    /* We need to find all vertices from which the target is reachable 
       in the residual graph. We do a breadth-first search, going
       backwards. */
    igraph_dqueue_t Q;
    igraph_vector_bool_t added;
    long int marked=0;

    IGRAPH_CHECK(igraph_vector_bool_init(&added, no_of_nodes));
    IGRAPH_FINALLY(igraph_vector_bool_destroy, &added);

    IGRAPH_CHECK(igraph_dqueue_init(&Q, 100));
    IGRAPH_FINALLY(igraph_dqueue_destroy, &Q);

    igraph_dqueue_push(&Q, target);
    VECTOR(added)[(long int)target]=1;
    marked++;
    while (!igraph_dqueue_empty(&Q)) {
      long int actnode=(long int) igraph_dqueue_pop(&Q);
      for (i=FIRST(actnode), j=LAST(actnode); i<j; i++) {
	long int nei=HEAD(i);
	if (!VECTOR(added)[nei] && RESCAP(REV(i)) > 0.0) {
	  VECTOR(added)[nei]=1;
	  marked++;
	  IGRAPH_CHECK(igraph_dqueue_push(&Q, nei));
	}
      }
    }    
    igraph_dqueue_destroy(&Q);
    IGRAPH_FINALLY_CLEAN(1);

    /* Now we marked each vertex that is on one side of the cut,
       check the crossing edges */

    if (cut) {
      igraph_vector_clear(cut);
      for (i=0; i<no_of_orig_edges; i++) {
	long int f=IGRAPH_FROM(graph, i);
	long int t=IGRAPH_TO(graph, i);
	if (!VECTOR(added)[f] && VECTOR(added)[t]) {
	  IGRAPH_CHECK(igraph_vector_push_back(cut, i));
	}
      }
    }

    if (partition2) {
      long int x=0;
      IGRAPH_CHECK(igraph_vector_resize(partition2, marked));
      for (i=0; i<no_of_nodes; i++) {
	if (VECTOR(added)[i]) {
	  VECTOR(*partition2)[x++]=i;
	}
      }
    }

    if (partition) {
      long int x=0;
      IGRAPH_CHECK(igraph_vector_resize(partition,
					no_of_nodes-marked));
      for (i=0; i<no_of_nodes; i++) {
	if (!VECTOR(added)[i]) {
	  VECTOR(*partition)[x++]=i;
	}
      }
    }
    
    igraph_vector_bool_destroy(&added);
    IGRAPH_FINALLY_CLEAN(1);
  }

  if (flow) {
    /* Initialize the backward distances, with a breadth-first search 
       from the source */ 
    igraph_dqueue_t Q;
    igraph_vector_int_t added;
    long int j, k, l;
    igraph_t flow_graph;
    igraph_vector_t flow_edges;
    igraph_bool_t dag;

    IGRAPH_CHECK(igraph_vector_int_init(&added, no_of_nodes));
    IGRAPH_FINALLY(igraph_vector_int_destroy, &added);
    IGRAPH_CHECK(igraph_dqueue_init(&Q, 100));
    IGRAPH_FINALLY(igraph_dqueue_destroy, &added);
    
    igraph_dqueue_push(&Q, source);
    igraph_dqueue_push(&Q, 0);
    VECTOR(added)[(long int)source]=1;
    while (!igraph_dqueue_empty(&Q)) {
      long int actnode=(long int) igraph_dqueue_pop(&Q);
      long int actdist=(long int) igraph_dqueue_pop(&Q);
      DIST(actnode)=actdist;
	  
      for (i=FIRST(actnode), j=LAST(actnode); i<j; i++) {
	long int nei=HEAD(i);
	if (!VECTOR(added)[nei] && RESCAP(REV(i)) > 0.0) {
	  VECTOR(added)[nei]=1;
	  IGRAPH_CHECK(igraph_dqueue_push(&Q, nei));
	  IGRAPH_CHECK(igraph_dqueue_push(&Q, actdist+1));
	}
      }
    } /* !igraph_dqueue_empty(&Q) */
	  
    igraph_vector_int_destroy(&added);
    igraph_dqueue_destroy(&Q);
    IGRAPH_FINALLY_CLEAN(2);

    /* Reinitialize the buckets */
    igraph_buckets_clear(&buckets);
    for (i=0; i<no_of_nodes; i++) {
      if (EXCESS(i) > 0.0 && i != source && i != target) {
	igraph_buckets_add(&buckets, (long int) DIST(i), i);
      }
    }

    /* Now we return the flow to the source */
    while (!igraph_buckets_empty(&buckets)) {
      long int vertex=igraph_buckets_popmax(&buckets);

      /* DISCHARGE(vertex) comes here */
      do {
	for (i=(long int) CURRENT(vertex), j=LAST(vertex); i<j; i++) {
	  if (RESCAP(i) > 0) {
	    long int nei=HEAD(i);
	    
	    if (DIST(vertex) == DIST(nei)+1) {
	      igraph_real_t delta=
		RESCAP(i) < EXCESS(vertex) ? RESCAP(i) : EXCESS(vertex);
	      RESCAP(i) -= delta;
	      RESCAP(REV(i)) += delta;
	      
	      if (nei != source && EXCESS(nei) == 0.0 &&
		  DIST(nei) != no_of_nodes) {
		igraph_buckets_add(&buckets, (long int) DIST(nei), nei);
	      }
	      
	      EXCESS(nei) += delta;
	      EXCESS(vertex) -= delta;
	      
	      if (EXCESS(vertex) == 0) break;
	      
	    }
	  }
	}
	
	if (i==j) {
	  
	  /* RELABEL(vertex) comes here */	
	  igraph_real_t min;
	  long int min_edge=0;
	  DIST(vertex)=min=no_of_nodes;
	  for (k=FIRST(vertex), l=LAST(vertex); k<l; k++) {
	    if (RESCAP(k) > 0) {
	      if (DIST(HEAD(k)) < min) {
		min=DIST(HEAD(k));
		min_edge=k;
	      }
	    }
	  }
	  
	  min++;
	  
	  if (min < no_of_nodes) {
	    DIST(vertex)=min;
	    CURRENT(vertex)=min_edge;
	    /* Vertex is still active */
	    igraph_buckets_add(&buckets, (long int) DIST(vertex), vertex);
	  }
	  
	  /* TODO: gap heuristics here ??? */

	} else {
	  CURRENT(vertex) = FIRST(vertex);
	}
	
	break;
	
      } while (1);
    }

    /* We need to eliminate flow cycles now. Before that we check that
       there is a cycle in the flow graph.

       First we do a couple of DFSes from the source vertex to the
       target and factor out the paths we find. If there is no more
       path to the target, then all remaining flow must be in flow
       cycles, so we don't need it at all.
       
       Some details. 'stack' contains the whole path of the DFS, both
       the vertices and the edges, they are alternating in the stack.
       'current' helps finding the next outgoing edge of a vertex
       quickly, the next edge of 'v' is FIRST(v)+CURRENT(v). If this
       is LAST(v), then there are no more edges to try.

       The 'added' vector contains 0 if the vertex was not visited
       before, 1 if it is currently in 'stack', and 2 if it is not in
       'stack', but it was visited before. */
    
    IGRAPH_VECTOR_INIT_FINALLY(&flow_edges, 0);
    for (i=0, j=0; i<no_of_edges; i+=2, j++) {
      long int pos=(long int) VECTOR(rank)[i];
      if ((capacity ? VECTOR(*capacity)[j] : 1.0) > RESCAP(pos)) {
	IGRAPH_CHECK(igraph_vector_push_back(&flow_edges, 
					     IGRAPH_FROM(graph, j)));
	IGRAPH_CHECK(igraph_vector_push_back(&flow_edges, 
					     IGRAPH_TO(graph, j)));
      }
    }
    IGRAPH_CHECK(igraph_create(&flow_graph, &flow_edges, no_of_nodes, 
			       IGRAPH_DIRECTED));
    igraph_vector_destroy(&flow_edges);
    IGRAPH_FINALLY_CLEAN(1);
    IGRAPH_FINALLY(igraph_destroy, &flow_graph);
    IGRAPH_CHECK(igraph_is_dag(&flow_graph, &dag));
    igraph_destroy(&flow_graph);
    IGRAPH_FINALLY_CLEAN(1);

    if (!dag) {
      igraph_vector_long_t stack;
      igraph_vector_t mycap;

      IGRAPH_CHECK(igraph_vector_long_init(&stack, 0));
      IGRAPH_FINALLY(igraph_vector_long_destroy, &stack);
      IGRAPH_CHECK(igraph_vector_int_init(&added, no_of_nodes));
      IGRAPH_FINALLY(igraph_vector_int_destroy, &added);
      IGRAPH_VECTOR_INIT_FINALLY(&mycap, no_of_edges);
      
#define MYCAP(i)      (VECTOR(mycap)[(i)])

      for (i=0; i<no_of_edges; i+=2) {
	long int pos=(long int) VECTOR(rank)[i];
	long int pos2=(long int) VECTOR(rank)[i+1];
	MYCAP(pos) = (capacity ? VECTOR(*capacity)[i/2] : 1.0) - RESCAP(pos);
	MYCAP(pos2) = 0.0;
      }
      
      do { 
	igraph_vector_long_null(&current);
	igraph_vector_long_clear(&stack);
	igraph_vector_int_null(&added);
	
	IGRAPH_CHECK(igraph_vector_long_push_back(&stack, -1));
	IGRAPH_CHECK(igraph_vector_long_push_back(&stack, source));
	VECTOR(added)[(long int)source]=1;
	while (!igraph_vector_long_empty(&stack) &&
	       igraph_vector_long_tail(&stack) != target) {
	  long int actnode=igraph_vector_long_tail(&stack);
	  long int edge=FIRST(actnode) + (long int) CURRENT(actnode);
	  long int nei;
	  while (edge < LAST(actnode) && MYCAP(edge)==0.0) { edge++; }
	  nei=edge < LAST(actnode) ? HEAD(edge) : -1;
	  
	  if (edge < LAST(actnode) && !VECTOR(added)[nei]) {
	    /* Go forward along next edge, if the vertex was not
	       visited before */
	    IGRAPH_CHECK(igraph_vector_long_push_back(&stack, edge));
	    IGRAPH_CHECK(igraph_vector_long_push_back(&stack, nei));
	    VECTOR(added)[nei]=1;
	    CURRENT(actnode) += 1;
	  } else if (edge < LAST(actnode) && VECTOR(added)[nei]==1) {
	    /* We found a flow cycle, factor it out. Go back in stack
	       until we find 'nei' again, determine the flow along the
	       cycle. */
	    igraph_real_t thisflow=MYCAP(edge);
	    long int idx;
	    for (idx=igraph_vector_long_size(&stack)-2; 
		 idx >= 0 && VECTOR(stack)[idx+1] != nei; idx-=2) {
	      long int e=VECTOR(stack)[idx];
	      igraph_real_t rcap= e >= 0 ? MYCAP(e) : MYCAP(edge);
	      if (rcap < thisflow) { thisflow=rcap; }
	    }
	    MYCAP(edge) -= thisflow; RESCAP(edge) += thisflow;
	    for (idx=igraph_vector_long_size(&stack)-2; 
		 idx >= 0 && VECTOR(stack)[idx+1] != nei; idx-=2) {
	      long int e=VECTOR(stack)[idx];
	      if (e >= 0) { MYCAP(e) -= thisflow; RESCAP(e) += thisflow; }
	    }
	    CURRENT(actnode) += 1;
	  } else if (edge < LAST(actnode)) { /* && VECTOR(added)[nei]==2 */
	    /* The next edge leads to a vertex that was visited before,
	       but it is currently not in 'stack' */
	    CURRENT(actnode) += 1;
	  } else {
	    /* Go backward, take out the node and the edge that leads to it */
	    igraph_vector_long_pop_back(&stack);
	    igraph_vector_long_pop_back(&stack);
	    VECTOR(added)[actnode]=2;
	  }
	}
      
	/* If non-empty, then it contains a path from source to target
	   in the residual graph. We factor out this path from the flow. */
	if (!igraph_vector_long_empty(&stack)) {
	  long int pl=igraph_vector_long_size(&stack);
	  igraph_real_t thisflow=EXCESS(target);
	  for (i=2; i<pl; i+=2) {
	    long int edge=VECTOR(stack)[i];
	    igraph_real_t rcap=MYCAP(edge);
	    if (rcap < thisflow) { thisflow=rcap; }
	  }
	  for (i=2; i<pl; i+=2) {
	    long int edge=VECTOR(stack)[i];
	    MYCAP(edge) -= thisflow;
	  }
	}
	
      } while (!igraph_vector_long_empty(&stack));
      
      igraph_vector_destroy(&mycap);
      igraph_vector_int_destroy(&added);
      igraph_vector_long_destroy(&stack);
      IGRAPH_FINALLY_CLEAN(3);
    }

    /* ----------------------------------------------------------- */

    IGRAPH_CHECK(igraph_vector_resize(flow, no_of_orig_edges));
    for (i=0, j=0; i<no_of_edges; i+=2, j++) {
      long int pos=(long int) VECTOR(rank)[i];
      VECTOR(*flow)[j] = (capacity ? VECTOR(*capacity)[j] : 1.0) - 
	RESCAP(pos);
    }
    
    igraph_vector_destroy(&rank);
    IGRAPH_FINALLY_CLEAN(1);
  }

  igraph_dbuckets_destroy(&ibuckets);
  igraph_buckets_destroy(&buckets);
  igraph_vector_long_destroy(&current);
  igraph_vector_long_destroy(&first);
  igraph_vector_long_destroy(&distance);
  igraph_vector_destroy(&excess);
  igraph_vector_destroy(&rescap);
  igraph_vector_long_destroy(&rev);
  igraph_vector_long_destroy(&to);
  igraph_dqueue_long_destroy(&bfsq);
  IGRAPH_FINALLY_CLEAN(10);

  return 0;
}

/**
 * \function igraph_maxflow_value
 * \brief Maximum flow in a network with the push/relabel algorithm
 * 
 * </para><para>This function implements the Goldberg-Tarjan algorithm for
 * calculating value of the maximum flow in a directed or undirected
 * graph. The algorithm was given in Andrew V. Goldberg, Robert
 * E. Tarjan: A New Approach to the Maximum-Flow Problem, Journal of
 * the ACM, 35(4), 921-940, 1988. </para>
 * 
 * <para> The input of the function is a graph, a vector
 * of real numbers giving the capacity of the edges and two vertices
 * of the graph, the source and the target. A flow is a function 
 * assigning positive real numbers to the edges and satisfying two
 * requirements: (1) the flow value is less than the capacity of the
 * edge and (2) at each vertex except the source and the target, the
 * incoming flow (ie. the sum of the flow on the incoming edges) is
 * the same as the outgoing flow (ie. the sum of the flow on the
 * outgoing edges). The value of the flow is the incoming flow at the
 * target vertex. The maximum flow is the flow with the maximum
 * value. </para>
 * 
 * <para> According to a theorem by Ford and Fulkerson 
 * (L. R. Ford Jr. and D. R. Fulkerson. Maximal flow through a
 * network. Canadian J. Math., 8:399-404, 1956.) the maximum flow
 * between two vertices is the same as the 
 * minimum cut between them (also called the minimum s-t cut). So \ref
 * igraph_st_mincut_value() gives the same result in all cases as \c
 * igraph_maxflow_value().</para>
 * 
 * <para> Note that the value of the maximum flow is the same as the
 * minimum cut in the graph.
 * \param graph The input graph, either directed or undirected.
 * \param value Pointer to a real number, the result will be placed here.
 * \param source The id of the source vertex.
 * \param target The id of the target vertex.
 * \param capacity Vector containing the capacity of the edges. If NULL, then
 *        every edge is considered to have capacity 1.0.
 * \param stats Counts of the number of different operations
 *        preformed by the algorithm are stored here.
 * \return Error code.
 * 
 * Time complexity: O(|V|^3).
 *
 * \sa \ref igraph_maxflow() to calculate the actual flow. 
 * \ref igraph_mincut_value(), \ref igraph_edge_connectivity(),
 * \ref igraph_vertex_connectivity() for 
 * properties based on the maximum flow.
 */

int igraph_maxflow_value(const igraph_t *graph, igraph_real_t *value,
			 igraph_integer_t source, igraph_integer_t target,
			 const igraph_vector_t *capacity,
			 igraph_maxflow_stats_t *stats) {

  return igraph_maxflow(graph, value, /*flow=*/ 0, /*cut=*/ 0, 
			/*partition=*/ 0, /*partition1=*/ 0,
			source, target, capacity, stats);
}

/**
 * \function igraph_st_mincut_value
 * \brief The minimum s-t cut in a graph
 * 
 * </para><para> The minimum s-t cut in a weighted (=valued) graph is the
 * total minimum edge weight needed to remove from the graph to
 * eliminate all paths from a given vertex (\c source) to
 * another vertex (\c target). Directed paths are considered in
 * directed graphs, and undirected paths in undirected graphs.  </para>
 * 
 * <para> The minimum s-t cut between two vertices is known to be same
 * as the maximum flow between these two vertices. So this function
 * calls \ref igraph_maxflow_value() to do the calculation.
 * \param graph The input graph.
 * \param value Pointer to a real variable, the result will be stored
 *        here. 
 * \param source The id of the source vertex.
 * \param target The id of the target vertex.
 * \param capacity Pointer to the capacity vector, it should contain
 *        non-negative numbers and its length should be the same the
 *        the number of edges in the graph. It can be a null pointer, then
 *        every edge has unit capacity.
 * \return Error code.
 * 
 * Time complexity: O(|V|^3), see also the discussion for \ref
 * igraph_maxflow_value(), |V| is the number of vertices. 
 */

int igraph_st_mincut_value(const igraph_t *graph, igraph_real_t *value,
			   igraph_integer_t source, igraph_integer_t target,
			   const igraph_vector_t *capacity) {
  
  if (source == target) {
    IGRAPH_ERROR("source and target vertices are the same", IGRAPH_EINVAL);
  }

  IGRAPH_CHECK(igraph_maxflow_value(graph, value, source, target, capacity, 0));

  return 0;
}			    

/** 
 * \function igraph_st_mincut
 * Minimum cut between a source and a target vertex
 * 
 * Finds the edge set that has the smallest total capacity among all
 * edge sets that disconnect the source and target vertices.
 * 
 * </para><para>The calculation is performed using maximum flow
 * techniques, by calling \ref igraph_maxflow().
 * \param graph The input graph.
 * \param value Pointer to a real variable, the value of the cut is
 *        stored here.
 * \param cut Pointer to a real vector, the edge ids that are included
 *        in the cut are stored here. This argument is ignored if it
 *        is a null pointer.
 * \param partition Pointer to a real vector, the vertex ids of the
 *        vertices in the first partition of the cut are stored
 *        here. The first partition is always the one that contains the
 *        source vertex. This argument is ignored if it is a null pointer.
 * \param partition2 Pointer to a real vector, the vertex ids of the
 *        vertices in the second partition of the cut are stored here.
 *        The second partition is always the one that contains the
 *        target vertex. This argument is ignored if it is a null pointer.
 * \param source Integer, the id of the source vertex.
 * \param target Integer, the id of the target vertex.
 * \param capacity Vector containing the capacity of the edges. If a
 *        null pointer, then every edge is considered to have capacity
 *        1.0.
 * \return Error code.
 * 
 * \sa \ref igraph_maxflow().
 * 
 * Time complexity: see \ref igraph_maxflow().
 */

int igraph_st_mincut(const igraph_t *graph, igraph_real_t *value,
		     igraph_vector_t *cut, igraph_vector_t *partition,
		     igraph_vector_t *partition2,
		     igraph_integer_t source, igraph_integer_t target,
		     const igraph_vector_t *capacity) {

  return igraph_maxflow(graph, value, /*flow=*/ 0, 
			cut, partition, partition2, 
			source, target, capacity, 0);
}

/* This is a flow-based version, but there is a better one
   for undirected graphs */

/* int igraph_i_mincut_value_undirected(const igraph_t *graph, */
/* 				     igraph_real_t *res, */
/* 				     const igraph_vector_t *capacity) { */
  
/*   long int no_of_edges=igraph_ecount(graph); */
/*   long int no_of_nodes=igraph_vcount(graph); */
/*   igraph_vector_t edges; */
/*   igraph_vector_t newcapacity; */
/*   igraph_t newgraph; */
/*   long int i; */
  
/*   /\* We need to convert this to directed by hand, since we need to be */
/*      sure that the edge ids will be handled properly to build the new */
/*      capacity vector. *\/ */

/*   IGRAPH_VECTOR_INIT_FINALLY(&edges, 0); */
/*   IGRAPH_VECTOR_INIT_FINALLY(&newcapacity, no_of_edges*2); */
/*   IGRAPH_CHECK(igraph_vector_reserve(&edges, no_of_edges*4)); */
/*   IGRAPH_CHECK(igraph_get_edgelist(graph, &edges, 0)); */
/*   IGRAPH_CHECK(igraph_vector_resize(&edges, no_of_edges*4)); */
/*   for (i=0; i<no_of_edges; i++) { */
/*     VECTOR(edges)[no_of_edges*2+i*2] = VECTOR(edges)[i*2+1]; */
/*     VECTOR(edges)[no_of_edges*2+i*2+1] = VECTOR(edges)[i*2]; */
/*     VECTOR(newcapacity)[i] = VECTOR(newcapacity)[no_of_edges+i] =  */
/*       capacity ? VECTOR(*capacity)[i] : 1.0 ; */
/*   } */
  
/*   IGRAPH_CHECK(igraph_create(&newgraph, &edges, no_of_nodes, IGRAPH_DIRECTED)); */
/*   IGRAPH_FINALLY(igraph_destroy, &newgraph); */
  
/*   IGRAPH_CHECK(igraph_mincut_value(&newgraph, res, &newcapacity)); */
  
/*   igraph_destroy(&newgraph); */
/*   igraph_vector_destroy(&edges); */
/*   igraph_vector_destroy(&newcapacity); */
/*   IGRAPH_FINALLY_CLEAN(3); */
  
/*   return 0; */
/* } */

/*
 * This is the Stoer-Wagner algorithm, it works for calculating the
 * minimum cut for undirected graphs, for the whole graph. 
 * I.e. this is basically the edge-connectivity of the graph. 
 * It can also calculate the cut itself, not just the cut value.
 */

int igraph_i_mincut_undirected(const igraph_t *graph, 
			       igraph_real_t *res,
			       igraph_vector_t *partition,
			       igraph_vector_t *partition2,
			       igraph_vector_t *cut,
			       const igraph_vector_t *capacity) {
  
  igraph_integer_t no_of_nodes=(igraph_integer_t) igraph_vcount(graph);
  igraph_integer_t no_of_edges=(igraph_integer_t) igraph_ecount(graph);  

  igraph_i_cutheap_t heap;
  igraph_real_t mincut=IGRAPH_INFINITY;	/* infinity */
  long int i;
  
  igraph_adjlist_t adjlist;
  igraph_inclist_t inclist;

  igraph_vector_t mergehist;
  igraph_bool_t calc_cut=partition || partition2 || cut;
  long int act_step=0, mincut_step=0;
  
  if (capacity && igraph_vector_size(capacity) != no_of_edges) {
    IGRAPH_ERROR("Invalid capacity vector size", IGRAPH_EINVAL);
  }

  /* Check if the graph is connected at all */
  {
    igraph_vector_t memb, csize;
    igraph_integer_t no;
    IGRAPH_VECTOR_INIT_FINALLY(&memb, 0);
    IGRAPH_VECTOR_INIT_FINALLY(&csize, 0);
    IGRAPH_CHECK(igraph_clusters(graph, &memb, &csize, &no,
				 /*mode=*/ IGRAPH_WEAK));
    if (no != 1) {
      if (res) { *res = 0;                 }
      if (cut) { igraph_vector_clear(cut); }
      if (partition) {
	int j=0;
	IGRAPH_CHECK(igraph_vector_resize(partition, 
					  (long int) VECTOR(csize)[0]));
	for (i=0; i<no_of_nodes; i++) {
	  if (VECTOR(memb)[i] == 0) { VECTOR(*partition)[j++] = i; }
	}
      }
      if (partition2) {
	int j=0;
	IGRAPH_CHECK(igraph_vector_resize(partition2, no_of_nodes - 
					  (long int) VECTOR(csize)[0]));
	for (i=0; i<no_of_nodes; i++) {
	  if (VECTOR(memb)[i] != 0) { VECTOR(*partition2)[j++] = i; }
	}
      }
    }
    igraph_vector_destroy(&csize);
    igraph_vector_destroy(&memb);
    IGRAPH_FINALLY_CLEAN(2);
    
    if (no != 1) { return 0; }
  }

  if (calc_cut) {
    IGRAPH_VECTOR_INIT_FINALLY(&mergehist, 0);
    IGRAPH_CHECK(igraph_vector_reserve(&mergehist, no_of_nodes*2));
  }

  IGRAPH_CHECK(igraph_i_cutheap_init(&heap, no_of_nodes));
  IGRAPH_FINALLY(igraph_i_cutheap_destroy, &heap);

  IGRAPH_CHECK(igraph_inclist_init(graph, &inclist, IGRAPH_OUT));
  IGRAPH_FINALLY(igraph_inclist_destroy, &inclist);

  IGRAPH_CHECK(igraph_adjlist_init(graph, &adjlist, IGRAPH_OUT));
  IGRAPH_FINALLY(igraph_adjlist_destroy, &adjlist);

  while (igraph_i_cutheap_size(&heap) >= 2) {

    long int last;
    igraph_real_t acut;
    long int a, n;

    igraph_vector_t *edges, *edges2;
    igraph_vector_int_t *neis, *neis2;
   
    do {
      a=igraph_i_cutheap_popmax(&heap);

      /* update the weights of the active vertices connected to a */
      edges=igraph_inclist_get(&inclist, a);
      neis=igraph_adjlist_get(&adjlist, a);
      n=igraph_vector_size(edges);
      for (i=0; i<n; i++) {
	igraph_integer_t edge=(igraph_integer_t) VECTOR(*edges)[i];
	igraph_integer_t to=(igraph_integer_t) VECTOR(*neis)[i];
	igraph_real_t weight=capacity ? VECTOR(*capacity)[(long int)edge] : 1.0;
	igraph_i_cutheap_update(&heap, to, weight);
      }
            
    } while (igraph_i_cutheap_active_size(&heap) > 1);

    /* Now, there is only one active vertex left, 
       calculate the cut of the phase */
    acut=igraph_i_cutheap_maxvalue(&heap);
    last=igraph_i_cutheap_popmax(&heap);

    if (acut < mincut) {
      mincut=acut;
      mincut_step=act_step;
    }    

    if (mincut == 0) {
      break;
    }

    /* And contract the last and the remaining vertex (a and last) */
    /* Before actually doing that, make some notes */
    act_step++;
    if (calc_cut) {
      IGRAPH_CHECK(igraph_vector_push_back(&mergehist, a));
      IGRAPH_CHECK(igraph_vector_push_back(&mergehist, last));
    }
    /* First remove the a--last edge if there is one, a is still the
       last deactivated vertex */
    edges=igraph_inclist_get(&inclist, a);
    neis=igraph_adjlist_get(&adjlist, a);
    n=igraph_vector_size(edges);
    for (i=0; i<n; ) {
      if (VECTOR(*neis)[i]==last) {
	VECTOR(*neis)[i] = VECTOR(*neis)[n-1];
	VECTOR(*edges)[i] = VECTOR(*edges)[n-1];
	igraph_vector_int_pop_back(neis);
	igraph_vector_pop_back(edges);
	n--;
      } else {
	i++;
      }
    }
    
    edges=igraph_inclist_get(&inclist, last);
    neis=igraph_adjlist_get(&adjlist, last);
    n=igraph_vector_size(edges);
    for (i=0; i<n; ) {
      if (VECTOR(*neis)[i] == a) {
	VECTOR(*neis)[i] = VECTOR(*neis)[n-1];
	VECTOR(*edges)[i] = VECTOR(*edges)[n-1];
	igraph_vector_int_pop_back(neis);
	igraph_vector_pop_back(edges);
	n--;
      } else {
	i++;
      }
    }

    /* Now rewrite the edge lists of last's neighbors */
    neis=igraph_adjlist_get(&adjlist, last);
    n=igraph_vector_int_size(neis);    
    for (i=0; i<n; i++) {     
      igraph_integer_t nei=(igraph_integer_t) VECTOR(*neis)[i];
      long int n2, j;
      neis2=igraph_adjlist_get(&adjlist, nei);
      n2=igraph_vector_int_size(neis2);
      for (j=0; j<n2; j++) {
	if (VECTOR(*neis2)[j] == last) {
	  VECTOR(*neis2)[j] = a;
	}
      }
    }
    
    /* And append the lists of last to the lists of a */
    edges=igraph_inclist_get(&inclist, a);
    neis=igraph_adjlist_get(&adjlist, a);
    edges2=igraph_inclist_get(&inclist, last);
    neis2=igraph_adjlist_get(&adjlist, last);
    IGRAPH_CHECK(igraph_vector_append(edges, edges2));
    IGRAPH_CHECK(igraph_vector_int_append(neis, neis2));
    igraph_vector_clear(edges2); /* TODO: free it */
    igraph_vector_int_clear(neis2);	 /* TODO: free it */

    /* Remove the deleted vertex from the heap entirely */
    igraph_i_cutheap_reset_undefine(&heap, last);    
  }

  *res=mincut;

  igraph_inclist_destroy(&inclist);
  igraph_adjlist_destroy(&adjlist);
  igraph_i_cutheap_destroy(&heap);
  IGRAPH_FINALLY_CLEAN(3);

  if (calc_cut) {
    long int bignode=(long int) VECTOR(mergehist)[2*mincut_step+1];
    long int i, idx;
    long int size=1;
    char *mark;
    mark=igraph_Calloc(no_of_nodes, char);
    if (!mark) { 
      IGRAPH_ERROR("Not enough memory for minimum cut", IGRAPH_ENOMEM);
    }
    IGRAPH_FINALLY(igraph_free, mark);
    
    /* first count the vertices in the partition */
    mark[bignode]=1;
    for (i=mincut_step-1; i>=0; i--) {
      if ( mark[ (long int) VECTOR(mergehist)[2*i] ] ) {
	size++;
	mark [ (long int) VECTOR(mergehist)[2*i+1] ]=1;
      }
    }
	
    /* now store them, if requested */
    if (partition) {
      IGRAPH_CHECK(igraph_vector_resize(partition, size));
      idx=0;
      VECTOR(*partition)[idx++]=bignode;
      for (i=mincut_step-1; i>=0; i--) {
	if (mark[ (long int) VECTOR(mergehist)[2*i] ]) {
	  VECTOR(*partition)[idx++] = VECTOR(mergehist)[2*i+1];
	}
      }
    }

    /* The other partition too? */
    if (partition2) {
      IGRAPH_CHECK(igraph_vector_resize(partition2, no_of_nodes-size));
      idx=0;
      for (i=0; i<no_of_nodes; i++) {
	if (!mark[i]) {
	  VECTOR(*partition2)[idx++]=i;
	}
      }
    }
    
    /* The edges in the cut are also requested? */
    /* We want as few memory allocated for 'cut' as possible,
       so we first collect the edges in mergehist, we don't 
       need that anymore. Then we copy it to 'cut';  */
    if (cut) {
      igraph_integer_t from, to;
      igraph_vector_clear(&mergehist);
      for (i=0; i<no_of_edges; i++) {
	igraph_edge(graph, (igraph_integer_t) i, &from, &to);
	if ((mark[(long int)from] && !mark[(long int)to]) ||
	    (mark[(long int)to] && !mark[(long int)from])) {
	  IGRAPH_CHECK(igraph_vector_push_back(&mergehist, i));
	}
      }
      igraph_vector_clear(cut);
      IGRAPH_CHECK(igraph_vector_append(cut, &mergehist));
    }

    igraph_free(mark);
    igraph_vector_destroy(&mergehist);
    IGRAPH_FINALLY_CLEAN(2);
  }
  
  return 0;
}

int igraph_i_mincut_directed(const igraph_t *graph,
			     igraph_real_t *value,
			     igraph_vector_t *partition,
			     igraph_vector_t *partition2,
			     igraph_vector_t *cut,
			     const igraph_vector_t *capacity) {
  long int i;
  long int no_of_nodes=igraph_vcount(graph);
  igraph_real_t flow;
  igraph_real_t minmaxflow=IGRAPH_INFINITY;
  igraph_vector_t mypartition, mypartition2, mycut;
  igraph_vector_t *ppartition=0, *ppartition2=0, *pcut=0;
  igraph_vector_t bestpartition, bestpartition2, bestcut;

  if (partition) {
    IGRAPH_VECTOR_INIT_FINALLY(&bestpartition, 0);
  }
  if (partition2) {
    IGRAPH_VECTOR_INIT_FINALLY(&bestpartition2, 0);
  }
  if (cut) {
    IGRAPH_VECTOR_INIT_FINALLY(&bestcut, 0);
  }
  
  if (partition) {
    IGRAPH_VECTOR_INIT_FINALLY(&mypartition, 0);
    ppartition=&mypartition;
  }
  if (partition2) {
    IGRAPH_VECTOR_INIT_FINALLY(&mypartition2, 0);
    ppartition2=&mypartition2;
  }
  if (cut) {
    IGRAPH_VECTOR_INIT_FINALLY(&mycut, 0);
    pcut=&mycut;
  }

  for (i=1; i<no_of_nodes; i++) {
    IGRAPH_CHECK(igraph_maxflow(graph, /*value=*/ &flow, /*flow=*/ 0, 
				pcut, ppartition, ppartition2, /*source=*/ 0,
				/*target=*/ (igraph_integer_t) i, capacity, 0));
    if (flow < minmaxflow) {
      minmaxflow = flow;
      if (cut) { 
	IGRAPH_CHECK(igraph_vector_update(&bestcut, &mycut)); 
      }
      if (partition) { 
	IGRAPH_CHECK(igraph_vector_update(&bestpartition, &mypartition)); 
      }
      if (partition2) { 
	IGRAPH_CHECK(igraph_vector_update(&bestpartition2, &mypartition2)); 
      }

      if (minmaxflow == 0) { break; }
    }
    IGRAPH_CHECK(igraph_maxflow(graph, /*value=*/ &flow, /*flow=*/ 0,
				pcut, ppartition, ppartition2, 
				/*source=*/ (igraph_integer_t) i,
				/*target=*/ 0, capacity, 0));
    if (flow < minmaxflow) {
      minmaxflow = flow;
      if (cut) { 
	IGRAPH_CHECK(igraph_vector_update(&bestcut, &mycut)); 
      }
      if (partition) { 
	IGRAPH_CHECK(igraph_vector_update(&bestpartition, &mypartition)); 
      }
      if (partition2) { 
	IGRAPH_CHECK(igraph_vector_update(&bestpartition2, &mypartition2)); 
      }

      if (minmaxflow == 0) { break; }
    }
  }
  
  if (value) {
    *value = minmaxflow;
  }

  if (cut) {
    igraph_vector_destroy(&mycut);
    IGRAPH_FINALLY_CLEAN(1);
  }
  if (partition) {
    igraph_vector_destroy(&mypartition);
    IGRAPH_FINALLY_CLEAN(1);
  }
  if (partition2) {
    igraph_vector_destroy(&mypartition2);
    IGRAPH_FINALLY_CLEAN(1);
  }
  if (cut) {
    IGRAPH_CHECK(igraph_vector_update(cut, &bestcut));
    igraph_vector_destroy(&bestcut);
    IGRAPH_FINALLY_CLEAN(1);
  }  
  if (partition2) {
    IGRAPH_CHECK(igraph_vector_update(partition2, &bestpartition2));
    igraph_vector_destroy(&bestpartition2);
    IGRAPH_FINALLY_CLEAN(1);
  }
  if (partition) {
    IGRAPH_CHECK(igraph_vector_update(partition, &bestpartition));
    igraph_vector_destroy(&bestpartition);
    IGRAPH_FINALLY_CLEAN(1);
  }
  
  return 0;
}

/** 
 * \function igraph_mincut
 * \brief Calculates the minimum cut in a graph.
 * 
 * This function calculates the minimum cut in a graph. 
 * The minimum cut is the minimum set of edges which needs to be
 * removed to disconnect the graph. The minimum is calculated using
 * the weights (\p capacity) of the edges, so the cut with the minimum
 * total capacity is calculated. 
 * 
 * </para><para> For directed graphs an implementation based on
 * calculating 2|V|-2 maximum flows is used. 
 * For undirected graphs we use the Stoer-Wagner
 * algorithm, as described in M. Stoer and F. Wagner: A simple min-cut
 * algorithm, Journal of the ACM, 44 585-591, 1997.
 * 
 * </para><para>
 * The first implementation of the actual cut calculation for
 * undirected graphs was made by Gregory Benison, thanks Greg.
 * \param graph The input graph.
 * \param value Pointer to a float, the value of the cut will be
 *    stored here.
 * \param partition Pointer to an initialized vector, the ids
 *    of the vertices in the first partition after separating the
 *    graph will be stored here. The vector will be resized as
 *    needed. This argument is ignored if it is a NULL pointer. 
 * \param partition2 Pointer to an initialized vector the ids
 *    of the vertices in the second partition will be stored here. 
 *    The vector will be resized as needed. This argument is ignored
 *    if it is a NULL pointer.
 * \param cut Pointer to an initialized vector, the ids of the edges
 *    in the cut will be stored here. This argument is ignored if it
 *    is a NULL pointer.
 * \param capacity A numeric vector giving the capacities of the
 *    edges. If a null pointer then all edges have unit capacity.
 * \return Error code.
 * 
 * \sa \ref igraph_mincut_value(), a simpler interface for calculating
 * the value of the cut only.
 *
 * Time complexity: for directed graphs it is O(|V|^4), but see the
 * remarks at \ref igraph_maxflow(). For undirected graphs it is
 * O(|V||E|+|V|^2 log|V|). |V| and |E| are the number of vertices and
 * edges respectively.
 * 
 * \example examples/simple/igraph_mincut.c
 */

int igraph_mincut(const igraph_t *graph,
		  igraph_real_t *value,
		  igraph_vector_t *partition,
		  igraph_vector_t *partition2,
		  igraph_vector_t *cut,
		  const igraph_vector_t *capacity) {
  
  if (igraph_is_directed(graph)) {
    if (partition || partition2 || cut) {
      igraph_i_mincut_directed(graph, value, partition, partition2, cut, 
			       capacity);
    } else {
      return igraph_mincut_value(graph, value, capacity);      
    }
  } else {
    IGRAPH_CHECK(igraph_i_mincut_undirected(graph, value, partition,
					    partition2, cut, capacity));
    return IGRAPH_SUCCESS;
  }
  
  return 0;
}
    

int igraph_i_mincut_value_undirected(const igraph_t *graph, 
				     igraph_real_t *res,
				     const igraph_vector_t *capacity) {
  return igraph_i_mincut_undirected(graph, res, 0, 0, 0, capacity);
}

/** 
 * \function igraph_mincut_value
 * \brief The minimum edge cut in a graph
 * 
 * </para><para> The minimum edge cut in a graph is the total minimum
 * weight of the edges needed to remove from the graph to make the
 * graph \em not strongly connected. (If the original graph is not
 * strongly connected then this is zero.) Note that in undirected
 * graphs strong connectedness is the same as weak connectedness. </para>
 * 
 * <para> The minimum cut can be calculated with maximum flow
 * techniques, although the current implementation does this only for
 * directed graphs and a separate non-flow based implementation is
 * used for undirected graphs. See Mechthild Stoer and Frank Wagner: A
 * simple min-cut algorithm, Journal of the ACM 44 585--591, 1997.
 * For directed graphs
 * the maximum flow is calculated between a fixed vertex and all the
 * other vertices in the graph and this is done in both
 * directions. Then the minimum is taken to get the minimum cut.
 * 
 * \param graph The input graph. 
 * \param res Pointer to a real variable, the result will be stored
 *    here.
 * \param capacity Pointer to the capacity vector, it should contain
 *    the same number of non-negative numbers as the number of edges in
 *    the graph. If a null pointer then all edges will have unit capacity.
 * \return Error code.
 *
 * \sa \ref igraph_mincut(), \ref igraph_maxflow_value(), \ref
 * igraph_st_mincut_value(). 
 * 
 * Time complexity: O(log(|V|)*|V|^2) for undirected graphs and 
 * O(|V|^4) for directed graphs, but see also the discussion at the
 * documentation of \ref igraph_maxflow_value().
 */

int igraph_mincut_value(const igraph_t *graph, igraph_real_t *res, 
			const igraph_vector_t *capacity) {

  long int no_of_nodes=igraph_vcount(graph);
  igraph_real_t minmaxflow, flow;
  long int i;

  minmaxflow=IGRAPH_INFINITY;

  if (!igraph_is_directed(graph)) {
    IGRAPH_CHECK(igraph_i_mincut_value_undirected(graph, res, capacity));
    return 0;
  }    

  for (i=1; i<no_of_nodes; i++) {
    IGRAPH_CHECK(igraph_maxflow_value(graph, &flow, 0, (igraph_integer_t) i,
				      capacity, 0));
    if (flow < minmaxflow) {
      minmaxflow = flow;
      if (flow==0) break;
    }
    IGRAPH_CHECK(igraph_maxflow_value(graph, &flow, (igraph_integer_t) i, 0,
				      capacity, 0));
    if (flow < minmaxflow) {
      minmaxflow = flow;
      if (flow==0) break;
    }
  }

  if (res) {
    *res=minmaxflow;
  }
  
  return 0;
}

int igraph_i_st_vertex_connectivity_directed(const igraph_t *graph,
					     igraph_integer_t *res,
					     igraph_integer_t source, 
					     igraph_integer_t target,
					     igraph_vconn_nei_t neighbors) {
  
  igraph_integer_t no_of_nodes=(igraph_integer_t) igraph_vcount(graph);
  igraph_integer_t no_of_edges=(igraph_integer_t) igraph_ecount(graph);
  igraph_vector_t edges;
  igraph_real_t real_res;
  igraph_t newgraph;
  long int i;
  igraph_bool_t conn1;
  
  if (source<0 || source>=no_of_nodes || target<0 || target>=no_of_nodes) {
    IGRAPH_ERROR("Invalid source or target vertex", IGRAPH_EINVAL);
  }

  switch (neighbors) {
  case IGRAPH_VCONN_NEI_ERROR:
    IGRAPH_CHECK(igraph_are_connected(graph, source, target, &conn1));
    if (conn1) {
      IGRAPH_ERROR("vertices connected", IGRAPH_EINVAL);
      return 0;
    }
    break;
  case IGRAPH_VCONN_NEI_NEGATIVE:
    IGRAPH_CHECK(igraph_are_connected(graph, source, target, &conn1));
    if (conn1) {
      *res=-1;
      return 0;
    }
    break;
  case IGRAPH_VCONN_NEI_NUMBER_OF_NODES:
    IGRAPH_CHECK(igraph_are_connected(graph, source, target, &conn1));
    if (conn1) {
      *res=no_of_nodes;
      return 0;
    }
    break;
  case IGRAPH_VCONN_NEI_IGNORE:
    break;
  default:
    IGRAPH_ERROR("Unknown `igraph_vconn_nei_t'", IGRAPH_EINVAL);
    break;
  }
  
  /* Create the new graph */
  
  IGRAPH_VECTOR_INIT_FINALLY(&edges, 0);
  IGRAPH_CHECK(igraph_vector_reserve(&edges, 2*(no_of_edges+no_of_nodes)));
  IGRAPH_CHECK(igraph_get_edgelist(graph, &edges, 0));
  IGRAPH_CHECK(igraph_vector_resize(&edges, 2*(no_of_edges+no_of_nodes)));
  
  for (i=0; i<2*no_of_edges; i+=2) {
    igraph_integer_t to=(igraph_integer_t) VECTOR(edges)[i+1];
    if (to != source && to != target) {
      VECTOR(edges)[i+1] = no_of_nodes + to;
      }
  }
  
  for (i=0; i<no_of_nodes; i++) {
    VECTOR(edges)[ 2*(no_of_edges+i)   ] = no_of_nodes+i;
    VECTOR(edges)[ 2*(no_of_edges+i)+1 ] = i;
  }
  
  IGRAPH_CHECK(igraph_create(&newgraph, &edges, 2*no_of_nodes, 
			     igraph_is_directed(graph)));
  
  igraph_vector_destroy(&edges);
  IGRAPH_FINALLY_CLEAN(1);
  IGRAPH_FINALLY(igraph_destroy, &newgraph);
  
  /* Do the maximum flow */
  
  no_of_nodes=igraph_vcount(&newgraph);
  no_of_edges=igraph_ecount(&newgraph);
  
  IGRAPH_CHECK(igraph_maxflow_value(&newgraph, &real_res, 
				    source, target, 0, 0));
  *res = (igraph_integer_t)real_res;

  igraph_destroy(&newgraph);
  IGRAPH_FINALLY_CLEAN(1);
  
  return 0;
}

int igraph_i_st_vertex_connectivity_undirected(const igraph_t *graph, 
					       igraph_integer_t *res,
					       igraph_integer_t source,
					       igraph_integer_t target,
					       igraph_vconn_nei_t neighbors){
  
  igraph_integer_t no_of_nodes=(igraph_integer_t) igraph_vcount(graph);
  igraph_t newgraph;
  igraph_bool_t conn;
  
  if (source<0 || source>=no_of_nodes || target<0 || target>=no_of_nodes) {
    IGRAPH_ERROR("Invalid source or target vertex", IGRAPH_EINVAL);
  }

  switch (neighbors) {
  case IGRAPH_VCONN_NEI_ERROR:
    IGRAPH_CHECK(igraph_are_connected(graph, source, target, &conn));
    if (conn) { 
      IGRAPH_ERROR("vertices connected", IGRAPH_EINVAL);      
      return 0;
    }
    break;
  case IGRAPH_VCONN_NEI_NEGATIVE:
    IGRAPH_CHECK(igraph_are_connected(graph, source, target, &conn));
    if (conn) {
      *res=-1;
      return 0;
    }
    break;
  case IGRAPH_VCONN_NEI_NUMBER_OF_NODES:
    IGRAPH_CHECK(igraph_are_connected(graph, source, target, &conn));
    if (conn) {
      *res=no_of_nodes;
      return 0;
    }
    break;
  case IGRAPH_VCONN_NEI_IGNORE:
    break;
  default:
    IGRAPH_ERROR("Unknown `igraph_vconn_nei_t'", IGRAPH_EINVAL);
    break;
  }

  IGRAPH_CHECK(igraph_copy(&newgraph, graph));
  IGRAPH_FINALLY(igraph_destroy, &newgraph);
  IGRAPH_CHECK(igraph_to_directed(&newgraph, IGRAPH_TO_DIRECTED_MUTUAL));

  IGRAPH_CHECK(igraph_i_st_vertex_connectivity_directed(&newgraph, res, 
							source, target, 
							IGRAPH_VCONN_NEI_IGNORE));
  
  igraph_destroy(&newgraph);
  IGRAPH_FINALLY_CLEAN(1);

  return 0;
}

/**
 * \function igraph_st_vertex_connectivity
 * \brief The vertex connectivity of a pair of vertices
 * 
 * </para><para>The vertex connectivity of two vertices (\c source and
 * \c target) is the minimum number of vertices that have to be
 * deleted to eliminate all paths from \c source to \c
 * target. Directed paths are considered in directed graphs.</para>
 * 
 * <para>The vertex connectivity of a pair is the same as the number
 * of different (ie. node-independent) paths from source to
 * target.</para> 
 *
 * <para>The current implementation uses maximum flow calculations to
 * obtain the result.
 * \param graph The input graph.
 * \param res Pointer to an integer, the result will be stored here.
 * \param source The id of the source vertex.
 * \param target The id of the target vertex.
 * \param neighbors A constant giving what to do if the two vertices
 *     are connected. Possible values: 
 *     \c IGRAPH_VCONN_NEI_ERROR, stop with an error message,
 *     \c IGRAPH_VCONN_NEGATIVE, return -1.
 *     \c IGRAPH_VCONN_NUMBER_OF_NODES, return the number of nodes.
 *     \c IGRAPH_VCONN_IGNORE, ignore the fact that the two vertices
 *        are connected and calculated the number of vertices needed
 *        to eliminate all paths except for the trivial (direct) paths
 *        between \c source and \c vertex. TOOD: what about neighbors?
 * \return Error code.
 * 
 * Time complexity: O(|V|^3), but see the discussion at \ref
 * igraph_maxflow_value(). 
 * 
 * \sa \ref igraph_vertex_connectivity(),
 * \ref igraph_edge_connectivity(),
 * \ref igraph_maxflow_value().
 */

int igraph_st_vertex_connectivity(const igraph_t *graph, 
				  igraph_integer_t *res,
				  igraph_integer_t source,
				  igraph_integer_t target,
				  igraph_vconn_nei_t neighbors) {

  if (source == target) { 
    IGRAPH_ERROR("source and target vertices are the same", IGRAPH_EINVAL);
  }
  
  if (igraph_is_directed(graph)) {
    IGRAPH_CHECK(igraph_i_st_vertex_connectivity_directed(graph, res,
							  source, target,
							  neighbors));
  } else {
    IGRAPH_CHECK(igraph_i_st_vertex_connectivity_undirected(graph, res,
							    source, target,
							    neighbors));
  }
  
  return 0;
}

int igraph_i_vertex_connectivity_directed(const igraph_t *graph, 
					igraph_integer_t *res) {

  igraph_integer_t no_of_nodes=(igraph_integer_t) igraph_vcount(graph);
  long int i, j;
  igraph_integer_t minconn=no_of_nodes-1, conn;

  for (i=0; i<no_of_nodes; i++) {
    for (j=0; j<no_of_nodes; j++) {
      if (i==j) { continue; }

      IGRAPH_ALLOW_INTERRUPTION();

      IGRAPH_CHECK(igraph_st_vertex_connectivity(graph, &conn, 
					 (igraph_integer_t) i, 
					 (igraph_integer_t) j, 
					 IGRAPH_VCONN_NEI_NUMBER_OF_NODES));
      if (conn < minconn) {
	minconn = conn;
	if (conn == 0) { break; }
      }
    }
    if (conn == 0) { break; }
  }

  if (res) {
    *res = minconn;
  }

  return 0;
}

int igraph_i_vertex_connectivity_undirected(const igraph_t *graph, 
					    igraph_integer_t *res) {
  igraph_t newgraph;

  IGRAPH_CHECK(igraph_copy(&newgraph, graph));
  IGRAPH_FINALLY(igraph_destroy, &newgraph);
  IGRAPH_CHECK(igraph_to_directed(&newgraph, IGRAPH_TO_DIRECTED_MUTUAL));
  
  IGRAPH_CHECK(igraph_i_vertex_connectivity_directed(&newgraph, res));
  
  igraph_destroy(&newgraph);
  IGRAPH_FINALLY_CLEAN(1);

  return 0;  
}

/* Use that vertex.connectivity(G) <= edge.connectivity(G) <= min(degree(G)) */
int igraph_i_connectivity_checks(const igraph_t *graph,
				 igraph_integer_t *res,
				 igraph_bool_t *found) {
  igraph_bool_t conn;
  *found=0;
  IGRAPH_CHECK(igraph_is_connected(graph, &conn, IGRAPH_STRONG));
  if (!conn) {
    *res=0;
    *found=1;
  } else {
    igraph_vector_t degree;
    IGRAPH_VECTOR_INIT_FINALLY(&degree, 0);
    if (!igraph_is_directed(graph)) {
      IGRAPH_CHECK(igraph_degree(graph, &degree, igraph_vss_all(),
				 IGRAPH_OUT, IGRAPH_LOOPS));
      if (igraph_vector_min(&degree)==1) {
	*res=1;
	*found=1;
      }
    } else {
      /* directed, check both in- & out-degree */
      IGRAPH_CHECK(igraph_degree(graph, &degree, igraph_vss_all(),
				 IGRAPH_OUT, IGRAPH_LOOPS));
      if (igraph_vector_min(&degree)==1) {
	*res=1;
	*found=1;
      } else {
	IGRAPH_CHECK(igraph_degree(graph, &degree, igraph_vss_all(),
				   IGRAPH_IN, IGRAPH_LOOPS));
	if (igraph_vector_min(&degree)==1) {
	  *res=1;
	  *found=1;
	}
      }
    }
    igraph_vector_destroy(&degree);
    IGRAPH_FINALLY_CLEAN(1);
  }
  return 0;
}

/**
 * \function igraph_vertex_connectivity
 * The vertex connectivity of a graph
 * 
 * </para><para> The vertex connectivity of a graph is the minimum
 * vertex connectivity along each pairs of vertices in the graph.
 * </para>
 * <para> The vertex connectivity of a graph is the same as group
 * cohesion as defined in Douglas R. White and Frank Harary: The
 * cohesiveness of blocks in social networks: node connectivity and
 * conditional density, Sociological Methodology 31:305--359, 2001.
 * \param graph The input graph.
 * \param res Pointer to an integer, the result will be stored here. 
 * \param checks Logical constant. Whether to check that the graph is
 *    connected and also the degree of the vertices. If the graph is
 *    not (strongly) connected then the connectivity is obviously zero. Otherwise
 *    if the minimum degree is one then the vertex connectivity is also
 *    one. It is a good idea to perform these checks, as they can be
 *    done quickly compared to the connectivity calculation itself. 
 *    They were suggested by Peter McMahan, thanks Peter.
 * \return Error code.
 * 
 * Time complexity: O(|V|^5).
 * 
 * \sa \ref igraph_st_vertex_connectivity(), \ref igraph_maxflow_value(),
 * and \ref igraph_edge_connectivity(). 
 */

int igraph_vertex_connectivity(const igraph_t *graph, igraph_integer_t *res, 
			       igraph_bool_t checks) {

  igraph_bool_t ret=0;

  if (checks) {
    IGRAPH_CHECK(igraph_i_connectivity_checks(graph, res, &ret));
  }
  
  /* Are we done yet? */
  if (!ret) {
    if (igraph_is_directed(graph)) {
      IGRAPH_CHECK(igraph_i_vertex_connectivity_directed(graph, res));
    } else {
      IGRAPH_CHECK(igraph_i_vertex_connectivity_undirected(graph, res));
    }
  }

  return 0;
}

/**
 * \function igraph_st_edge_connectivity
 * \brief Edge connectivity of a pair of vertices
 * 
 * </para><para> The edge connectivity of two vertices (\c source and
 * \c target) in a graph is the minimum number of edges that
 * have to be deleted from the graph to eliminate all paths from \c
 * source to \c target.</para>
 * 
 * <para>This function uses the maximum flow algorithm to calculate
 * the edge connectivity.
 * \param graph The input graph, it has to be directed.
 * \param res Pointer to an integer, the result will be stored here.
 * \param source The id of the source vertex.
 * \param target The id of the target vertex.
 * \return Error code.
 *
 * Time complexity: O(|V|^3). 
 * 
 * \sa \ref igraph_maxflow_value(), \ref igraph_edge_connectivity(),
 * \ref igraph_st_vertex_connectivity(), \ref
 * igraph_vertex_connectivity().
 */

int igraph_st_edge_connectivity(const igraph_t *graph, igraph_integer_t *res,
				igraph_integer_t source, 
				igraph_integer_t target) {
  igraph_real_t flow;

  if (source == target) {
    IGRAPH_ERROR("source and target vertices are the same", IGRAPH_EINVAL);
  }

  IGRAPH_CHECK(igraph_maxflow_value(graph, &flow, source, target, 0, 0));
  *res = (igraph_integer_t) flow;

  return 0;
}


/**
 * \function igraph_edge_connectivity
 * \brief The minimum edge connectivity in a graph.
 * 
 * </para><para> This is the minimum of the edge connectivity over all
 * pairs of vertices in the graph. </para>
 * 
 * <para>
 * The edge connectivity of a graph is the same as group adhesion as
 * defined in Douglas R. White and Frank Harary: The cohesiveness of
 * blocks in social networks: node connectivity and conditional
 * density, Sociological Methodology 31:305--359, 2001.
 * \param graph The input graph.
 * \param res Pointer to an integer, the result will be stored here.
 * \param checks Logical constant. Whether to check that the graph is
 *    connected and also the degree of the vertices. If the graph is
 *    not (strongly) connected then the connectivity is obviously zero. Otherwise
 *    if the minimum degree is one then the edge connectivity is also
 *    one. It is a good idea to perform these checks, as they can be
 *    done quickly compared to the connectivity calculation itself. 
 *    They were suggested by Peter McMahan, thanks Peter.
 * \return Error code.
 * 
 * Time complexity: O(log(|V|)*|V|^2) for undirected graphs and 
 * O(|V|^4) for directed graphs, but see also the discussion at the
 * documentation of \ref igraph_maxflow_value().
 * 
 * \sa \ref igraph_st_edge_connectivity(), \ref igraph_maxflow_value(), 
 * \ref igraph_vertex_connectivity().
 */

int igraph_edge_connectivity(const igraph_t *graph, igraph_integer_t *res,
			     igraph_bool_t checks) {
  igraph_bool_t ret=0;
  
  /* Use that vertex.connectivity(G) <= edge.connectivity(G) <= min(degree(G)) */
  if (checks) {
    IGRAPH_CHECK(igraph_i_connectivity_checks(graph, res, &ret));
  }  

  if (!ret) {
    igraph_real_t real_res;
    IGRAPH_CHECK(igraph_mincut_value(graph, &real_res, 0));
	*res = (igraph_integer_t)real_res;
  }

  return 0;
}

/**
 * \function igraph_edge_disjoint_paths
 * \brief The maximum number of edge-disjoint paths between two vertices. 
 * 
 * </para><para> A set of paths between two vertices is called
 * edge-disjoint if they do not share any edges. The maximum number of
 * edge-disjoint paths are calculated by this function using maximum
 * flow techniques. Directed paths are considered in directed
 * graphs. </para>
 * 
 * <para> Note that the number of disjoint paths is the same as the
 * edge connectivity of the two vertices using uniform edge weights.
 * \param graph The input graph, can be directed or undirected.
 * \param res Pointer to an integer variable, the result will be
 *        stored here. 
 * \param source The id of the source vertex.
 * \param target The id of the target vertex.
 * \return Error code.
 * 
 * Time complexity: O(|V|^3), but see the discussion at \ref
 * igraph_maxflow_value().
 * 
 * \sa \ref igraph_vertex_disjoint_paths(), \ref
 * igraph_st_edge_connectivity(), \ref igraph_maxflow_value().
 */

int igraph_edge_disjoint_paths(const igraph_t *graph, igraph_integer_t *res,
			       igraph_integer_t source, 
			       igraph_integer_t target) {

  igraph_real_t flow;

  if (source == target) {
    IGRAPH_ERROR("Not implemented for source=target", IGRAPH_UNIMPLEMENTED);
  }

  IGRAPH_CHECK(igraph_maxflow_value(graph, &flow, source, target, 0, 0));

  *res = (igraph_integer_t) flow;
  
  return 0;
}

/**
 * \function igraph_vertex_disjoint_paths
 * \brief Maximum number of vertex-disjoint paths between two vertices.
 * 
 * </para><para> A set of paths between two vertices is called
 * vertex-disjoint if they share no vertices. The calculation is
 * performed by using maximum flow techniques. </para>
 * 
 * <para> Note that the number of vertex-disjoint paths is the same as
 * the vertex connectivity of the two vertices in most cases (if the
 * two vertices are not connected by an edge).
 * \param graph The input graph.
 * \param res Pointer to an integer variable, the result will be
 *        stored here. 
 * \param source The id of the source vertex.
 * \param target The id of the target vertex.
 * \return Error code.
 * 
 * Time complexity: O(|V|^3).
 * 
 * \sa \ref igraph_edge_disjoint_paths(), \ref
 * igraph_vertex_connectivity(), \ref igraph_maxflow_value().
 */

int igraph_vertex_disjoint_paths(const igraph_t *graph, igraph_integer_t *res,
				 igraph_integer_t source,
				 igraph_integer_t target) {

  igraph_bool_t conn;

  if (source==target) {
    IGRAPH_ERROR("The source==target case is not implemented",
		 IGRAPH_UNIMPLEMENTED);
  }

  igraph_are_connected(graph, source, target, &conn);
  if (conn) { 
    /* We need to remove every (possibly directed) edge between source
       and target and calculate the disjoint paths on the new
       graph. Finally we add 1 for the removed connection(s).  */
    igraph_es_t es;
    igraph_vector_t v;
    igraph_t newgraph;
    IGRAPH_VECTOR_INIT_FINALLY(&v, 2);
    VECTOR(v)[0]=source;
    VECTOR(v)[1]=target;
    IGRAPH_CHECK(igraph_es_multipairs(&es, &v, IGRAPH_DIRECTED));
    IGRAPH_FINALLY(igraph_es_destroy, &es);
    
    IGRAPH_CHECK(igraph_copy(&newgraph, graph));    
    IGRAPH_FINALLY(igraph_destroy, &newgraph);
    IGRAPH_CHECK(igraph_delete_edges(&newgraph, es));

    if (igraph_is_directed(graph)) {
      IGRAPH_CHECK(igraph_i_st_vertex_connectivity_directed(&newgraph, res,
							    source, target,
							    IGRAPH_VCONN_NEI_IGNORE));
    } else {
      IGRAPH_CHECK(igraph_i_st_vertex_connectivity_undirected(&newgraph, res,
							      source, target, 
							      IGRAPH_VCONN_NEI_IGNORE));
    }

    if (res) {
      *res += 1;
    }
    
    IGRAPH_FINALLY_CLEAN(3);
    igraph_destroy(&newgraph);
    igraph_es_destroy(&es);
    igraph_vector_destroy(&v);
  }

  /* These do nothing if the two vertices are connected, 
     so it is safe to call them. */

  if (igraph_is_directed(graph)) {
    IGRAPH_CHECK(igraph_i_st_vertex_connectivity_directed(graph, res,
							  source, target,
							  IGRAPH_VCONN_NEI_IGNORE));
  } else {
    IGRAPH_CHECK(igraph_i_st_vertex_connectivity_undirected(graph, res,
							    source, target,
							    IGRAPH_VCONN_NEI_IGNORE));
  }    
  
  return 0;
}

/**
 * \function igraph_adhesion
 * \brief Graph adhesion, this is (almost) the same as edge connectivity.
 * 
 * </para><para> This quantity is defined by White and Harary in
 * The cohesiveness of blocks in social networks: node connectivity and
 * conditional density, (Sociological Methodology 31:305--359, 2001)
 * and basically it is the edge connectivity of the graph
 * with uniform edge weights.
 * \param graph The input graph, either directed or undirected.
 * \param res Pointer to an integer, the result will be stored here.
 * \param checks Logical constant. Whether to check that the graph is
 *    connected and also the degree of the vertices. If the graph is
 *    not (strongly) connected then the adhesion is obviously zero. Otherwise
 *    if the minimum degree is one then the adhesion is also
 *    one. It is a good idea to perform these checks, as they can be
 *    done quickly compared to the edge connectivity calculation itself. 
 *    They were suggested by Peter McMahan, thanks Peter.
* \return Error code.
 * 
 * Time complexity: O(log(|V|)*|V|^2) for undirected graphs and 
 * O(|V|^4) for directed graphs, but see also the discussion at the
 * documentation of \ref igraph_maxflow_value().
 *
 * \sa \ref igraph_cohesion(), \ref igraph_maxflow_value(), \ref
 * igraph_edge_connectivity(), \ref igraph_mincut_value().
 */

int igraph_adhesion(const igraph_t *graph, igraph_integer_t *res,
		    igraph_bool_t checks) {
  return igraph_edge_connectivity(graph, res, checks);
}

/**
 * \function igraph_cohesion
 * \brief Graph cohesion, this is the same as vertex connectivity. 
 * 
 * </para><para> This quantity was defined by White and Harary in <quote>The
 * cohesiveness of blocks in social networks: node connectivity and
 * conditional density</quote>, (Sociological Methodology 31:305--359, 2001)
 * and it is the same as the vertex connectivity of a 
 * graph. 
 * \param graph The input graph.
 * \param res Pointer to an integer variable, the result will be
 *        stored here.
 * \param checks Logical constant. Whether to check that the graph is
 *    connected and also the degree of the vertices. If the graph is
 *    not (strongly) connected then the cohesion is obviously zero. Otherwise
 *    if the minimum degree is one then the cohesion is also
 *    one. It is a good idea to perform these checks, as they can be
 *    done quickly compared to the vertex connectivity calculation itself. 
 *    They were suggested by Peter McMahan, thanks Peter.
 * \return Error code.
 * 
 * Time complexity: O(|V|^4), |V| is the number of vertices. In
 * practice it is more like O(|V|^2), see \ref igraph_maxflow_value().
 * 
 * \sa \ref igraph_vertex_connectivity(), \ref igraph_adhesion(), 
 * \ref igraph_maxflow_value().
 */

int igraph_cohesion(const igraph_t *graph, igraph_integer_t *res,
		    igraph_bool_t checks) {
  
  IGRAPH_CHECK(igraph_vertex_connectivity(graph, res, checks));
  return 0;
}

/**
 * \function igraph_gomory_hu_tree
 * \brief Gomory-Hu tree of a graph.
 *
 * </para><para>
 * The Gomory-Hu tree is a concise representation of the value of all the
 * maximum flows (or minimum cuts) in a graph. The vertices of the tree
 * correspond exactly to the vertices of the original graph in the same order.
 * Edges of the Gomory-Hu tree are annotated by flow values.  The value of
 * the maximum flow (or minimum cut) between an arbitrary (u,v) vertex
 * pair in the original graph is then given by the minimum flow value (i.e.
 * edge annotation) along the shortest path between u and v in the
 * Gomory-Hu tree.
 *
 * </para><para>This implementation uses Gusfield's algorithm to construct the
 * Gomory-Hu tree. See the following paper for more details:
 * 
 * </para><para>
 * Gusfield D: Very simple methods for all pairs network flow analysis. SIAM J
 * Comput 19(1):143-155, 1990.
 *
 * \param graph The input graph.
 * \param tree  Pointer to an uninitialized graph; the result will be
 *              stored here.
 * \param flows Pointer to an uninitialized vector; the flow values
 *              corresponding to each edge in the Gomory-Hu tree will
 *              be returned here. You may pass a NULL pointer here if you are
 *              not interested in the flow values.
 * \param capacity Vector containing the capacity of the edges. If NULL, then
 *        every edge is considered to have capacity 1.0.
 * \return Error code.
 *
 * Time complexity: O(|V|^4) since it performs a max-flow calculation
 * between vertex zero and every other vertex and max-flow is
 * O(|V|^3).
 *
 * \sa \ref igraph_maxflow()
 */
int igraph_gomory_hu_tree(const igraph_t *graph, igraph_t *tree,
			  igraph_vector_t *flows, const igraph_vector_t *capacity) {

  igraph_integer_t no_of_nodes = igraph_vcount(graph);
  igraph_integer_t source, target, mid, i, n;
  igraph_vector_t neighbors;
  igraph_vector_t flow_values;
  igraph_vector_t partition;
  igraph_vector_t partition2;
  igraph_real_t flow_value;

  if (igraph_is_directed(graph)) {
    IGRAPH_ERROR("Gomory-Hu tree can only be calculated for undirected graphs",
	IGRAPH_EINVAL);
  }
  
  /* Allocate memory */
  IGRAPH_VECTOR_INIT_FINALLY(&neighbors, no_of_nodes);
  IGRAPH_VECTOR_INIT_FINALLY(&flow_values, no_of_nodes);
  IGRAPH_VECTOR_INIT_FINALLY(&partition, 0);
  IGRAPH_VECTOR_INIT_FINALLY(&partition2, 0);

  /* Initialize the tree: every edge points to node 0 */
  /* Actually, this is done implicitly since both 'neighbors' and 'flow_values' are
   * initialized to zero already */

  /* For each source vertex except vertex zero... */
  for (source = 1; source < no_of_nodes; source++) {
    IGRAPH_ALLOW_INTERRUPTION();
    IGRAPH_PROGRESS("Gomory-Hu tree", (100.0 * (source - 1)) / (no_of_nodes-1), 0);

    /* Find its current neighbor in the tree */
    target = VECTOR(neighbors)[(long int)source];

    /* Find the maximum flow between source and target */
    IGRAPH_CHECK(igraph_maxflow(graph, &flow_value, 0, 0, &partition, &partition2,
	  source, target, capacity, 0));

    /* Store the maximum flow and determine which side each node is on */
    VECTOR(flow_values)[(long int)source] = flow_value;

    /* Update the tree */
    /* igraph_maxflow() guarantees that the source vertex will be in &partition
     * and not in &partition2 */
    n = igraph_vector_size(&partition);
    for (i = 0; i < n; i++) {
      mid = VECTOR(partition)[i];
      if (mid > source && VECTOR(neighbors)[(long int)mid] == target) {
	VECTOR(neighbors)[(long int)mid] = source;
      }
    }
  }

  IGRAPH_PROGRESS("Gomory-Hu tree", 100.0, 0);
  
  /* Re-use the 'partition' vector as an edge list now */
  IGRAPH_CHECK(igraph_vector_resize(&partition, 2*(no_of_nodes-1)));
  for (i = 1, mid = 0; i < no_of_nodes; i++, mid += 2) {
    VECTOR(partition)[(long int)mid]   = i;
    VECTOR(partition)[(long int)mid+1] = VECTOR(neighbors)[(long int)i];
  }

  /* Create the tree graph; we use igraph_subgraph_edges here to keep the
   * graph and vertex attributes */
  IGRAPH_CHECK(igraph_subgraph_edges(graph, tree, igraph_ess_none(), 0));
  IGRAPH_CHECK(igraph_add_edges(tree, &partition, 0));

  /* Free the allocated memory */
  igraph_vector_destroy(&partition2);
  igraph_vector_destroy(&partition);
  igraph_vector_destroy(&neighbors);
  IGRAPH_FINALLY_CLEAN(3);

  /* Return the flow values to the caller */
  if (flows != 0) {
    IGRAPH_CHECK(igraph_vector_update(flows, &flow_values));
    if (no_of_nodes > 0) {
      igraph_vector_remove(flows, 0);
    }
  }

  /* Free the remaining allocated memory */
  igraph_vector_destroy(&flow_values);
  IGRAPH_FINALLY_CLEAN(1);
  
  return IGRAPH_SUCCESS;
}