This file is indexed.

/usr/include/vigra/graph_algorithms.hxx is in libvigraimpex-dev 1.10.0+git20160211.167be93+dfsg-2+b5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
/************************************************************************/
/*                                                                      */
/*     Copyright 2014 by Thorsten Beier and Ullrich Koethe              */
/*                                                                      */
/*    This file is part of the VIGRA computer vision library.           */
/*    The VIGRA Website is                                              */
/*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
/*    Please direct questions, bug reports, and contributions to        */
/*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
/*        vigra@informatik.uni-hamburg.de                               */
/*                                                                      */
/*    Permission is hereby granted, free of charge, to any person       */
/*    obtaining a copy of this software and associated documentation    */
/*    files (the "Software"), to deal in the Software without           */
/*    restriction, including without limitation the rights to use,      */
/*    copy, modify, merge, publish, distribute, sublicense, and/or      */
/*    sell copies of the Software, and to permit persons to whom the    */
/*    Software is furnished to do so, subject to the following          */
/*    conditions:                                                       */
/*                                                                      */
/*    The above copyright notice and this permission notice shall be    */
/*    included in all copies or substantial portions of the             */
/*    Software.                                                         */
/*                                                                      */
/*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
/*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
/*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
/*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
/*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
/*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
/*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
/*    OTHER DEALINGS IN THE SOFTWARE.                                   */
/*                                                                      */
/************************************************************************/

/**
 * This header provides definitions of graph-related algorithms
 */

#ifndef VIGRA_GRAPH_ALGORITHMS_HXX
#define VIGRA_GRAPH_ALGORITHMS_HXX

/*std*/
#include <algorithm>
#include <vector>
#include <functional>
#include <set>
#include <iomanip>

/*vigra*/
#include "graphs.hxx"
#include "graph_generalization.hxx"
#include "multi_gridgraph.hxx"
#include "priority_queue.hxx"
#include "union_find.hxx"
#include "adjacency_list_graph.hxx"
#include "graph_maps.hxx"

#include "timing.hxx"
//#include "openmp_helper.hxx"


#include "functorexpression.hxx"
#include "array_vector.hxx"

namespace vigra{

/** \addtogroup GraphDataStructures
*/
//@{

    namespace detail_graph_algorithms{
        template <class GRAPH_MAP,class COMPERATOR>
        struct GraphItemCompare
        {
            
            GraphItemCompare(const GRAPH_MAP & map,const COMPERATOR & comperator)
            : map_(map),
              comperator_(comperator){

            }

            template<class KEY>
            bool operator()(const KEY & a, const KEY & b) const{
                return comperator_(map_[a],map_[b]);
            }

            const GRAPH_MAP & map_;
            const COMPERATOR & comperator_;
        };
    } // namespace detail_graph_algorithms

    /// \brief get a vector of Edge descriptors
    ///
    /// Sort the Edge descriptors given weights 
    /// and a comperator
    template<class GRAPH,class WEIGHTS,class COMPERATOR>
    void edgeSort(
        const GRAPH   & g,
        const WEIGHTS & weights,
        const COMPERATOR  & comperator,
        std::vector<typename GRAPH::Edge> & sortedEdges
    ){
        sortedEdges.resize(g.edgeNum());
        size_t c=0;
        for(typename GRAPH::EdgeIt e(g);e!=lemon::INVALID;++e){
            sortedEdges[c]=*e;
            ++c;
        }
        detail_graph_algorithms::GraphItemCompare<WEIGHTS,COMPERATOR> edgeComperator(weights,comperator);
        std::sort(sortedEdges.begin(),sortedEdges.end(),edgeComperator);
    }


    /// \brief copy a lemon node map
    template<class G,class A,class B>
    void copyNodeMap(const G & g,const A & a ,B & b){
        typename  G::NodeIt iter(g);
        while(iter!=lemon::INVALID){
            b[*iter]=a[*iter];
            ++iter;
        }

    }
    /// \brief copy a lemon edge map
    template<class G,class A,class B>
    void copyEdgeMap(const G & g,const A & a ,B & b){
        typename  G::EdgeIt iter(g);
        while(iter!=lemon::INVALID){
            b[*iter]=a[*iter];
            ++iter;
        }
    }
    /// \brief fill a lemon node map 
    template<class G,class A,class T>
    void fillNodeMap(const G & g, A & a, const T & value){
        typename  G::NodeIt iter(g);
        while(iter!=lemon::INVALID){
            a[*iter]=value;
            ++iter;
        }
    }
    /// \brief fill a lemon edge map
    template<class G,class A,class T>
    void fillEdgeMap(const G & g,A & a ,const T & value){
        typename  G::EdgeIt iter(g);
        while(iter!=lemon::INVALID){
            a[*iter]=value;
            ++iter;
        }
    }

    /// \brief make a region adjacency graph from a graph and labels w.r.t. that graph
    ///
    /// \param graphIn  : input graph
    /// \param labels   : labels w.r.t. graphIn
    /// \param[out] rag  : region adjacency graph 
    /// \param[out] affiliatedEdges : a vector of edges of graphIn for each edge in rag
    /// \param      ignoreLabel : optional label to ignore (default: -1 means no label will be ignored)
    ///
    template<
        class GRAPH_IN,
        class GRAPH_IN_NODE_LABEL_MAP
    >
    void makeRegionAdjacencyGraph(
        GRAPH_IN                   graphIn,
        GRAPH_IN_NODE_LABEL_MAP    labels,
        AdjacencyListGraph & rag,
        typename AdjacencyListGraph:: template EdgeMap< std::vector<typename GRAPH_IN::Edge> > & affiliatedEdges,
        const Int64   ignoreLabel=-1
    ){
        rag=AdjacencyListGraph();
        typedef typename GraphMapTypeTraits<GRAPH_IN_NODE_LABEL_MAP>::Value LabelType;
        typedef GRAPH_IN GraphIn;
        typedef AdjacencyListGraph GraphOut;

        typedef typename GraphIn::Edge   EdgeGraphIn;
        typedef typename GraphIn::NodeIt NodeItGraphIn;
        typedef typename GraphIn::EdgeIt EdgeItGraphIn;
        typedef typename GraphOut::Edge   EdgeGraphOut; 


        for(NodeItGraphIn iter(graphIn);iter!=lemon::INVALID;++iter){
            const LabelType l=labels[*iter];
            if(ignoreLabel==-1 || static_cast<Int64>(l)!=ignoreLabel)
                rag.addNode(l);
        }
    
        for(EdgeItGraphIn e(graphIn);e!=lemon::INVALID;++e){
            const EdgeGraphIn edge(*e);
            const LabelType lu = labels[graphIn.u(edge)];
            const LabelType lv = labels[graphIn.v(edge)];
            if(  lu!=lv && ( ignoreLabel==-1 || (static_cast<Int64>(lu)!=ignoreLabel  && static_cast<Int64>(lv)!=ignoreLabel) )  ){
                // if there is an edge between lu and lv no new edge will be added
                rag.addEdge( rag.nodeFromId(lu),rag.nodeFromId(lv));
            }
        }
        
        //SET UP HYPEREDGES
        affiliatedEdges.assign(rag);
        for(EdgeItGraphIn e(graphIn);e!=lemon::INVALID;++e){
            const EdgeGraphIn edge(*e);
            const LabelType lu = labels[graphIn.u(edge)];
            const LabelType lv = labels[graphIn.v(edge)];
            //std::cout<<"edge between ?? "<<lu<<" "<<lv<<"\n";
            if(  lu!=lv && ( ignoreLabel==-1 || (static_cast<Int64>(lu)!=ignoreLabel  && static_cast<Int64>(lv)!=ignoreLabel) )  ){
                //std::cout<<"find edge between "<<lu<<" "<<lv<<"\n";
                EdgeGraphOut ragEdge= rag.findEdge(rag.nodeFromId(lu),rag.nodeFromId(lv));
                //std::cout<<"invalid?"<<bool(ragEdge==lemon::INVALID)<<" id "<<rag.id(ragEdge)<<"\n";
                affiliatedEdges[ragEdge].push_back(edge);
                //std::cout<<"write done\n";
            }
        }
    }

    template<unsigned int DIM, class DTAG, class AFF_EDGES>
    size_t affiliatedEdgesSerializationSize(
        const GridGraph<DIM,DTAG> & gridGraph,
        const AdjacencyListGraph & rag,
        const AFF_EDGES & affEdges
    ){
        size_t size = 0;

        typedef typename  AdjacencyListGraph::EdgeIt EdgeIt;
        typedef typename  AdjacencyListGraph::Edge Edge;

        for(EdgeIt iter(rag); iter!=lemon::INVALID; ++iter){
            const Edge e(*iter);
            size+=1;
            size+=affEdges[e].size()*(DIM+1);
        }
        return size;
    }

    template<class OUT_ITER, unsigned int DIM, class DTAG, class AFF_EDGES>
    void serializeAffiliatedEdges(
        const GridGraph<DIM,DTAG> & gridGraph,
        const AdjacencyListGraph & rag,
        const AFF_EDGES & affEdges,
        OUT_ITER outIter
    ){
      
        typedef typename  AdjacencyListGraph::EdgeIt EdgeIt;
        typedef typename  AdjacencyListGraph::Edge Edge;
        typedef typename  GridGraph<DIM,DTAG>::Edge GEdge;

        for(EdgeIt iter(rag); iter!=lemon::INVALID; ++iter){

            const Edge edge = *iter;
            const size_t numAffEdge = affEdges[edge].size();
            *outIter = numAffEdge; ++outIter;

            for(size_t i=0; i<numAffEdge; ++i){
                const GEdge gEdge = affEdges[edge][i];
                for(size_t d=0; d<DIM+1; ++d){
                    *outIter = gEdge[d]; ++outIter;
                }
            }
        }
    }

    template<class IN_ITER, unsigned int DIM, class DTAG, class AFF_EDGES>
    void deserializeAffiliatedEdges(
        const GridGraph<DIM,DTAG> & gridGraph,
        const AdjacencyListGraph & rag,
        AFF_EDGES & affEdges,
        IN_ITER begin,
        IN_ITER end
    ){
      
        typedef typename  AdjacencyListGraph::EdgeIt EdgeIt;
        typedef typename  AdjacencyListGraph::Edge Edge;
        typedef typename  GridGraph<DIM,DTAG>::Edge GEdge;

        affEdges.assign(rag);

        for(EdgeIt iter(rag); iter!=lemon::INVALID; ++iter){

            const Edge edge = *iter;
            const size_t numAffEdge = *begin; ++begin;

            for(size_t i=0; i<numAffEdge; ++i){
                GEdge gEdge;
                for(size_t d=0; d<DIM+1; ++d){
                    gEdge[d]=*begin; ++begin;
                }
                affEdges[edge].push_back(gEdge);
            }
        }
    }




    /// \brief shortest path computer
    template<class GRAPH,class WEIGHT_TYPE>
    class ShortestPathDijkstra{
    public:
        typedef GRAPH Graph;

        typedef typename Graph::Node Node;
        typedef typename Graph::NodeIt NodeIt;
        typedef typename Graph::Edge Edge;
        typedef typename Graph::OutArcIt OutArcIt;

        typedef WEIGHT_TYPE WeightType;
        typedef ChangeablePriorityQueue<WeightType>           PqType;
        typedef typename Graph:: template NodeMap<Node>       PredecessorsMap;
        typedef typename Graph:: template NodeMap<WeightType> DistanceMap;
        typedef ArrayVector<Node>                             DiscoveryOrder;
            
        /// \ brief constructor from graph
        ShortestPathDijkstra(const Graph & g)
        :   graph_(g),
            pq_(g.maxNodeId()+1),
            predMap_(g),
            distMap_(g)
        {
        }

        /// \brief run shortest path given edge weights
        ///
        /// \param weights : edge weights encoding the distance between adjacent nodes (must be non-negative) 
        /// \param source  : source node where shortest path should start
        /// \param target  : target node where shortest path should stop. If target is not given
        ///                  or <tt>INVALID</tt>, the shortest path from source to all reachable nodes is computed
        /// \param maxDistance  : path search is terminated when the path length exceeds <tt>maxDistance</tt>
        ///
        /// When a valid \a target is unreachable from \a source (either because the graph is disconnected 
        /// or \a maxDistance is exceeded), it is set to <tt>lemon::INVALID</tt>. In contrast, if \a target
        /// was <tt>lemon::INVALID</tt> at the beginning, it will always be set to the last node 
        /// visited in the search.
        template<class WEIGHTS>
        void run(const WEIGHTS & weights, const Node & source,
                 const Node & target = lemon::INVALID, 
                 WeightType maxDistance=NumericTraits<WeightType>::max())
        {
            this->initializeMaps(source);
            runImpl(weights, target, maxDistance);
        }

        /// \brief run shortest path in a region of interest of a \ref GridGraph.
        ///
        /// \param start : first point in the desired ROI.
        /// \param stop  : beyond the last point in the desired ROI (i.e. exclusive)
        /// \param weights : edge weights encoding the distance between adjacent nodes (must be non-negative) 
        /// \param source  : source node where shortest path should start
        /// \param target  : target node where shortest path should stop. If target is not given
        ///                  or <tt>INVALID</tt>, the shortest path from source to all reachable nodes is computed
        /// \param maxDistance  : path search is terminated when the path length exceeds <tt>maxDistance</tt>
        ///
        /// This version of <tt>run()</tt> restricts the path search to the ROI <tt>[start, stop)</tt> and only
        /// works for instances of \ref GridGraph. Otherwise, it is identical to the standard <tt>run()</tt> 
        /// function.
        template<class WEIGHTS>
        void run(Node const & start, Node const & stop,
                 const WEIGHTS & weights, const Node & source,
                 const Node & target = lemon::INVALID, 
                 WeightType maxDistance=NumericTraits<WeightType>::max())
        {
            vigra_precondition(allLessEqual(start, source) && allLess(source, stop),
                "ShortestPathDijkstra::run(): source is not within ROI");
            vigra_precondition(target == lemon::INVALID ||
                               (allLessEqual(start, target) && allLess(target, stop)),
                "ShortestPathDijkstra::run(): target is not within ROI");
            this->initializeMaps(source, start, stop);
            runImpl(weights, target, maxDistance);
        }

        /// \brief run shortest path again with given edge weights
        ///
        /// This only differs from standard <tt>run()</tt> by initialization: Instead of resetting 
        /// the entire graph, this only resets the nodes that have been visited in the 
        /// previous run, i.e. the contents of the array <tt>discoveryOrder()</tt>.
        /// This will be much faster if only a small fraction of the nodes has to be reset.
        template<class WEIGHTS>
        void reRun(const WEIGHTS & weights, const Node & source,
                   const Node & target = lemon::INVALID, 
                   WeightType maxDistance=NumericTraits<WeightType>::max())
        {
            this->reInitializeMaps(source);
            runImpl(weights, target, maxDistance);
        }

        /// \brief run shortest path with given edge weights from multiple sources.
        ///
        /// This is otherwise identical to standard <tt>run()</tt>, except that 
        /// <tt>source()</tt> returns <tt>lemon::INVALID</tt> after path search finishes.
        template<class WEIGHTS, class ITER>
        void 
        runMultiSource(const WEIGHTS & weights, ITER source_begin, ITER source_end,
                 const Node & target = lemon::INVALID, 
                 WeightType maxDistance=NumericTraits<WeightType>::max())
        {
            this->initializeMapsMultiSource(source_begin, source_end);
            runImpl(weights, target, maxDistance);
        }


        /// \brief run shortest path with given edge weights from multiple sources.
        ///
        /// This is otherwise identical to standard <tt>run()</tt>, except that 
        /// <tt>source()</tt> returns <tt>lemon::INVALID</tt> after path search finishes.
        template<class EFGE_WEIGHTS,class NODE_WEIGHTS, class ITER>
        void 
        runMultiSource(
            const EFGE_WEIGHTS & edgeWeights, 
            const NODE_WEIGHTS & nodeWeights,
            ITER source_begin, 
            ITER source_end,
            const Node & target = lemon::INVALID, 
            WeightType maxDistance = NumericTraits<WeightType>::max())
        {
            this->initializeMapsMultiSource(source_begin, source_end);
            runImplWithNodeWeights(edgeWeights, nodeWeights, target, maxDistance);
        }

        /// \brief get the graph
        const Graph & graph()const{
            return graph_;
        }
        /// \brief get the source node
        const Node & source()const{
            return source_;
        }
        /// \brief get the target node
        const Node & target()const{
            return target_;
        }

        /// \brief check if explicit target is given
        bool hasTarget()const{
            return target_!=lemon::INVALID;
        }

        /// \brief get an array with all visited nodes, sorted by distance from source
        const DiscoveryOrder & discoveryOrder() const{
            return discoveryOrder_;
        }

        /// \brief get the predecessors node map (after a call of run)
        const PredecessorsMap & predecessors()const{
            return predMap_;
        }
        
        /// \brief get the distances node map (after a call of run)
        const DistanceMap & distances()const{
            return distMap_;
        }
        
        /// \brief get the distance to a rarget node (after a call of run)
        WeightType distance(const Node & target)const{
            return distMap_[target];
        }


    private:

        template<class WEIGHTS>
        void runImpl(const WEIGHTS & weights,
                     const Node & target = lemon::INVALID, 
                     WeightType maxDistance=NumericTraits<WeightType>::max())
        {
            ZeroNodeMap<Graph, WEIGHT_TYPE> zeroNodeMap;
            this->runImplWithNodeWeights(weights,zeroNodeMap, target, maxDistance);
        }


        template<class EDGE_WEIGHTS, class NODE_WEIGHTS>
        void runImplWithNodeWeights(
            const EDGE_WEIGHTS & edgeWeights,
            const NODE_WEIGHTS & nodeWeights,
            const Node & target = lemon::INVALID, 
            WeightType maxDistance=NumericTraits<WeightType>::max())
        {
            target_ = lemon::INVALID;
            while(!pq_.empty() ){ //&& !finished){
                const Node topNode(graph_.nodeFromId(pq_.top()));
                if(distMap_[topNode] > maxDistance)
                    break; // distance threshold exceeded
                pq_.pop();
                discoveryOrder_.push_back(topNode);
                if(topNode == target)
                    break;
                // loop over all neigbours
                for(OutArcIt outArcIt(graph_,topNode);outArcIt!=lemon::INVALID;++outArcIt){
                    const Node otherNode = graph_.target(*outArcIt);
                    const size_t otherNodeId = graph_.id(otherNode);
                    const WeightType otherNodeWeight = nodeWeights[otherNode];
                    if(pq_.contains(otherNodeId)){
                        const Edge edge(*outArcIt);
                        const WeightType currentDist     = distMap_[otherNode];
                        const WeightType alternativeDist = distMap_[topNode]+edgeWeights[edge]+otherNodeWeight;
                        if(alternativeDist<currentDist){
                            pq_.push(otherNodeId,alternativeDist);
                            distMap_[otherNode]=alternativeDist;
                            predMap_[otherNode]=topNode;
                        }
                    }
                    else if(predMap_[otherNode]==lemon::INVALID){
                        const Edge edge(*outArcIt);
                        const WeightType initialDist = distMap_[topNode]+edgeWeights[edge]+otherNodeWeight;
                        if(initialDist<=maxDistance)
                        {
                            pq_.push(otherNodeId,initialDist);
                            distMap_[otherNode]=initialDist;
                            predMap_[otherNode]=topNode;
                        }
                    }
                }
            }
            while(!pq_.empty() ){
                const Node topNode(graph_.nodeFromId(pq_.top()));
                predMap_[topNode]=lemon::INVALID;
                pq_.pop();
            }
            if(target == lemon::INVALID || discoveryOrder_.back() == target)
                target_ = discoveryOrder_.back(); // Means that target was reached. If, to the contrary, target 
                                                  // was unreachable within maxDistance, target_ remains INVALID.
        }

        void initializeMaps(Node const & source){
            for(NodeIt n(graph_); n!=lemon::INVALID; ++n){
                const Node node(*n);
                predMap_[node]=lemon::INVALID;
            }
            distMap_[source]=static_cast<WeightType>(0.0);
            predMap_[source]=source;
            discoveryOrder_.clear();
            pq_.push(graph_.id(source),0.0);
            source_=source;
        }

        void initializeMaps(Node const & source,
                            Node const & start, Node const & stop)
        {
            Node left_border  = min(start, Node(1)),
                 right_border = min(predMap_.shape()-stop, Node(1)),
                 DONT_TOUCH   = Node(lemon::INVALID) - Node(1);
            
            initMultiArrayBorder(predMap_.subarray(start-left_border, stop+right_border),
                                 left_border, right_border, DONT_TOUCH);
            predMap_.subarray(start, stop) = lemon::INVALID;
            predMap_[source]=source;
            
            distMap_[source]=static_cast<WeightType>(0.0);
            discoveryOrder_.clear();
            pq_.push(graph_.id(source),0.0);
            source_=source;
        }

        template <class ITER>
        void initializeMapsMultiSource(ITER source, ITER source_end){
            for(NodeIt n(graph_); n!=lemon::INVALID; ++n){
                const Node node(*n);
                predMap_[node]=lemon::INVALID;
            }
            discoveryOrder_.clear();
            for( ; source != source_end; ++source)
            {
                distMap_[*source]=static_cast<WeightType>(0.0);
                predMap_[*source]=*source;
                pq_.push(graph_.id(*source),0.0);
            }
            source_=lemon::INVALID;
        }

        void reInitializeMaps(Node const & source){
            for(unsigned int n=0; n<discoveryOrder_.size(); ++n){
                predMap_[discoveryOrder_[n]]=lemon::INVALID;
            }
            distMap_[source]=static_cast<WeightType>(0.0);
            predMap_[source]=source;
            discoveryOrder_.clear();
            pq_.push(graph_.id(source),0.0);
            source_=source;
        }

        const Graph  & graph_;
        PqType  pq_;
        PredecessorsMap predMap_;
        DistanceMap     distMap_;
        DiscoveryOrder  discoveryOrder_;

        Node source_;
        Node target_;
    };

    /// \brief get the length in node units of a path
    template<class NODE,class PREDECESSORS>
    size_t pathLength(
        const NODE source,
        const NODE target,
        const PREDECESSORS & predecessors
    ){
        if(predecessors[target]==lemon::INVALID)
            return 0;
        else{
            NODE currentNode = target;
            size_t length=1;
            while(currentNode!=source){
                currentNode=predecessors[currentNode];
                length+=1;
            }
            return length;
        }
    }

    /// \brief Astar Shortest path search
    template<class GRAPH,class WEIGHTS,class PREDECESSORS,class DISTANCE,class HEURSTIC>
    void shortestPathAStar(
        const GRAPH         &           graph,
        const typename GRAPH::Node &    source,
        const typename GRAPH::Node &    target,
        const WEIGHTS       &           weights,
        PREDECESSORS        &           predecessors,
        DISTANCE            &           distance,
        const HEURSTIC      &           heuristic
    ){

        typedef GRAPH                       Graph;
        typedef typename Graph::Edge Edge;
        typedef typename Graph::Node Node;
        typedef typename Graph::NodeIt NodeIt;
        typedef typename Graph::OutArcIt OutArcIt;
        typedef typename DISTANCE::value_type    DistanceType;

        typename  GRAPH:: template NodeMap<bool> closedSet(graph);
        vigra::ChangeablePriorityQueue<typename WEIGHTS::value_type> estimatedDistanceOpenSet(graph.maxNodeId()+1);
        // initialize
        for(NodeIt n(graph);n!=lemon::INVALID;++n){
            const Node node(*n);
            closedSet[node]=false;
            distance[node]=std::numeric_limits<DistanceType>::infinity();
            predecessors[node]=lemon::INVALID;
        }
        // distance and estimated distance for start node
        distance[source]=static_cast<DistanceType>(0.0);
        estimatedDistanceOpenSet.push(graph.id(source),heuristic(source,target));

        // while any nodes left in openSet
        while(!estimatedDistanceOpenSet.empty()){

            // get the node with the lpwest estimated distance in the open set
            const Node current = graph.nodeFromId(estimatedDistanceOpenSet.top());

            // reached target?
            if(current==target)
                break;

            // remove current from openSet
            // add current to closedSet
            estimatedDistanceOpenSet.pop();
            closedSet[current]=true;

            // iterate over neigbours of current
            for(OutArcIt outArcIt(graph,current);outArcIt!=lemon::INVALID;++outArcIt){

                // get neigbour node and id
                const Node neighbour = graph.target(*outArcIt);
                const size_t neighbourId = graph.id(neighbour);

                // if neighbour is not yet in closedSet
                if(!closedSet[neighbour]){

                    // get edge between current and neigbour
                    const Edge edge(*outArcIt);

                    // get tentative score
                    const DistanceType tenativeScore = distance[current] + weights[edge];

                    // neighbour NOT in openSet OR tentative score better than the current distance
                    if(!estimatedDistanceOpenSet.contains(neighbourId) || tenativeScore < distance[neighbour] ){
                        // set predecessors and distance
                        predecessors[neighbour]=current;
                        distance[neighbour]=tenativeScore;

                        // update the estimated cost from neighbour to target
                        // ( and neigbour will be (re)-added to openSet)
                        estimatedDistanceOpenSet.push(neighbourId,distance[neighbour]+heuristic(neighbour,target));
                    }
                }
            }
        }
    }
    

    template<
    class GRAPH, 
    class EDGE_WEIGHTS, 
    class NODE_WEIGHTS,
    class SEED_NODE_MAP,
    class WEIGHT_TYPE
    >
    void shortestPathSegmentation(
        const GRAPH & graph,
        const EDGE_WEIGHTS & edgeWeights,
        const NODE_WEIGHTS & nodeWeights,
        SEED_NODE_MAP & seeds
    ){

        typedef GRAPH Graph;
        typedef typename Graph::Node Node;
        typedef typename Graph::NodeIt NodeIt;
        typedef WEIGHT_TYPE WeightType;

        // find seeds
        std::vector<Node> seededNodes;
        for(NodeIt n(graph);n!=lemon::INVALID;++n){
            const Node node(*n);
            // not a seed
            if(seeds[node]!=0){
                seededNodes.push_back(node);
            }
        }

        // do shortest path
        typedef ShortestPathDijkstra<Graph, WeightType> Sp;
        typedef typename Sp::PredecessorsMap PredecessorsMap;
        Sp sp(graph);
        sp.runMultiSource(edgeWeights, nodeWeights, seededNodes.begin(), seededNodes.end());
        const PredecessorsMap & predMap = sp.predecessors();
        // do the labeling
        for(NodeIt n(graph);n!=lemon::INVALID;++n){
            Node node(*n);
            if(seeds[node]==0){
                int label = 0 ;
                Node pred=predMap[node];
                while(seeds[pred]==0){
                    pred=predMap[pred];
                }
                seeds[node]=seeds[pred];
            }
        }
    }

    namespace detail_watersheds_segmentation{

    struct RawPriorityFunctor{
        template<class L, class T>
        T operator()(const L label,const T  priority)const{
            return priority;
        }
    };



    template<class PRIORITY_TYPE,class LABEL_TYPE>
    struct CarvingFunctor{
        CarvingFunctor(const LABEL_TYPE backgroundLabel,
                       const PRIORITY_TYPE & factor,
                       const PRIORITY_TYPE & noPriorBelow
        )
        :   backgroundLabel_(backgroundLabel),
            factor_(factor),
            noPriorBelow_(noPriorBelow){
        }
        PRIORITY_TYPE operator()(const LABEL_TYPE label,const PRIORITY_TYPE  priority)const{
            if(priority>=noPriorBelow_)
                return (label==backgroundLabel_ ? priority*factor_ : priority);
            else{
                return priority;
            }
        }
        LABEL_TYPE     backgroundLabel_;
        PRIORITY_TYPE  factor_;
        PRIORITY_TYPE  noPriorBelow_;
    };


    template<
        class GRAPH,
        class EDGE_WEIGHTS,
        class SEEDS,
        class PRIORITY_MANIP_FUNCTOR,
        class LABELS
    >
    void edgeWeightedWatershedsSegmentationImpl(
        const GRAPH & g,
        const EDGE_WEIGHTS      & edgeWeights,
        const SEEDS             & seeds,
        PRIORITY_MANIP_FUNCTOR  & priorManipFunctor,
        LABELS                  & labels
    ){  
        typedef GRAPH Graph;
        typedef typename Graph::Edge Edge;
        typedef typename Graph::Node Node;
        typedef typename Graph::NodeIt NodeIt;
        typedef typename Graph::OutArcIt OutArcIt;

        typedef typename EDGE_WEIGHTS::Value WeightType;
        typedef typename LABELS::Value  LabelType;
        //typedef typename Graph:: template EdgeMap<bool>    EdgeBoolMap;
        typedef PriorityQueue<Edge,WeightType,true> PQ;

        PQ pq;
        //EdgeBoolMap inPQ(g);
        copyNodeMap(g,seeds,labels);
        //fillEdgeMap(g,inPQ,false);


        // put edges from nodes with seed on pq
        for(NodeIt n(g);n!=lemon::INVALID;++n){
            const Node node(*n);
            if(labels[node]!=static_cast<LabelType>(0)){
                for(OutArcIt a(g,node);a!=lemon::INVALID;++a){
                    const Edge edge(*a);
                    const Node neigbour=g.target(*a);
                    //std::cout<<"n- node "<<g.id(neigbour)<<"\n";
                    if(labels[neigbour]==static_cast<LabelType>(0)){
                        const WeightType priority = priorManipFunctor(labels[node],edgeWeights[edge]);
                        pq.push(edge,priority);
                        //inPQ[edge]=true;
                    }
                }
            }
        }


        while(!pq.empty()){

            const Edge edge = pq.top();
            pq.pop();

            const Node u = g.u(edge);
            const Node v = g.v(edge);
            const LabelType lU = labels[u];
            const LabelType lV = labels[v];


            if(lU==0 && lV==0){
                throw std::runtime_error("both have no labels");
            }
            else if(lU!=0 && lV!=0){
                // nothing to do
            }
            else{

                const Node unlabeledNode = lU==0 ? u : v;
                const LabelType label = lU==0 ? lV : lU;

                // assign label to unlabeled node
                labels[unlabeledNode] = label;

                // iterate over the nodes edges
                for(OutArcIt a(g,unlabeledNode);a!=lemon::INVALID;++a){
                    const Edge otherEdge(*a);
                    const Node targetNode=g.target(*a);
                    if(labels[targetNode] == 0){
                    //if(inPQ[otherEdge] == false && labels[targetNode] == 0){
                        const WeightType priority = priorManipFunctor(label,edgeWeights[otherEdge]);
                        pq.push(otherEdge,priority);
                       // inPQ[otherEdge]=true;
                    }
                }
            }

        }

    }

    } // end namespace detail_watersheds_segmentation


    /// \brief edge weighted watersheds Segmentataion
    /// 
    /// \param g: input graph
    /// \param edgeWeights : edge weights / edge indicator
    /// \param seeds : seed must be non empty!
    /// \param[out] labels : resulting  nodeLabeling (not necessarily dense)
    template<class GRAPH,class EDGE_WEIGHTS,class SEEDS,class LABELS>
    void edgeWeightedWatershedsSegmentation(
        const GRAPH & g,
        const EDGE_WEIGHTS & edgeWeights,
        const SEEDS        & seeds,
        LABELS             & labels
    ){  
        detail_watersheds_segmentation::RawPriorityFunctor fPriority;
        detail_watersheds_segmentation::edgeWeightedWatershedsSegmentationImpl(g,edgeWeights,seeds,fPriority,labels);
    }   
    

    /// \brief edge weighted watersheds Segmentataion
    /// 
    /// \param g: input graph
    /// \param edgeWeights : edge weights / edge indicator
    /// \param seeds : seed must be non empty!
    /// \param backgroundLabel : which label is background
    /// \param backgroundBias  : bias for background
    /// \param noPriorBelow  : don't bias the background if edge indicator is below this value
    /// \param[out] labels : resulting  nodeLabeling (not necessarily dense)
    template<class GRAPH,class EDGE_WEIGHTS,class SEEDS,class LABELS>
    void carvingSegmentation(
        const GRAPH                         & g,
        const EDGE_WEIGHTS                  & edgeWeights,
        const SEEDS                         & seeds,
        const typename LABELS::Value        backgroundLabel,
        const typename EDGE_WEIGHTS::Value  backgroundBias,
        const typename EDGE_WEIGHTS::Value  noPriorBelow,
        LABELS                      & labels
    ){
        typedef typename EDGE_WEIGHTS::Value WeightType;
        typedef typename LABELS::Value       LabelType;
        detail_watersheds_segmentation::CarvingFunctor<WeightType,LabelType> fPriority(backgroundLabel,backgroundBias, noPriorBelow);
        detail_watersheds_segmentation::edgeWeightedWatershedsSegmentationImpl(g,edgeWeights,seeds,fPriority,labels);
    }

    /// \brief edge weighted watersheds Segmentataion
    /// 
    /// \param graph: input graph
    /// \param edgeWeights : edge weights / edge indicator
    /// \param nodeSizes : size of each node
    /// \param k : free parameter of felzenszwalb algorithm
    /// \param[out] nodeLabeling :  nodeLabeling (not necessarily dense)
    /// \param nodeNumStopCond      : optional stopping condition
    template< class GRAPH , class EDGE_WEIGHTS, class NODE_SIZE,class NODE_LABEL_MAP>
    void felzenszwalbSegmentation(
        const GRAPH &         graph,
        const EDGE_WEIGHTS &  edgeWeights,
        const NODE_SIZE    &  nodeSizes,
        float           k,
        NODE_LABEL_MAP     &  nodeLabeling,
        const int             nodeNumStopCond = -1
    ){
        typedef GRAPH Graph;
        typedef typename Graph::Edge Edge;
        typedef typename Graph::Node Node;

        typedef typename EDGE_WEIGHTS::Value WeightType;
        typedef typename EDGE_WEIGHTS::Value NodeSizeType;
        typedef typename Graph:: template NodeMap<WeightType>   NodeIntDiffMap;
        typedef typename Graph:: template NodeMap<NodeSizeType> NodeSizeAccMap;

        // initalize node size map  and internal diff map
        NodeIntDiffMap internalDiff(graph);
        NodeSizeAccMap nodeSizeAcc(graph);  
        copyNodeMap(graph,nodeSizes,nodeSizeAcc);
        fillNodeMap(graph,internalDiff,static_cast<WeightType>(0.0));



        // initlaize internal node diff map

        // sort the edges by their weights
        std::vector<Edge> sortedEdges;
        std::less<WeightType> comperator;
        edgeSort(graph,edgeWeights,comperator,sortedEdges);

        // make the ufd
        UnionFindArray<UInt64> ufdArray(graph.maxNodeId()+1);


        size_t nodeNum = graph.nodeNum();   


        while(true){
            // iterate over edges is the sorted order
            for(size_t i=0;i<sortedEdges.size();++i){
                const Edge e  = sortedEdges[i];
                const size_t rui = ufdArray.findIndex(graph.id(graph.u(e)));
                const size_t rvi = ufdArray.findIndex(graph.id(graph.v(e)));
                const Node   ru  = graph.nodeFromId(rui);
                const Node   rv  = graph.nodeFromId(rvi);
                if(rui!=rvi){

                    //check if to merge or not ?
                    const WeightType   w         = edgeWeights[e];
                    const NodeSizeType sizeRu    = nodeSizeAcc[ru];
                    const NodeSizeType sizeRv    = nodeSizeAcc[rv];
                    const WeightType tauRu       = static_cast<WeightType>(k)/static_cast<WeightType>(sizeRu);
                    const WeightType tauRv       = static_cast<WeightType>(k)/static_cast<WeightType>(sizeRv);
                    const WeightType minIntDiff  = std::min(internalDiff[ru]+tauRu,internalDiff[rv]+tauRv);
                    if(w<=minIntDiff){
                        // do merge
                        ufdArray.makeUnion(rui,rvi);
                        --nodeNum;
                        // update size and internal difference
                        const size_t newRepId = ufdArray.findIndex(rui);
                        const Node newRepNode = graph.nodeFromId(newRepId);
                        internalDiff[newRepNode]=w;
                        nodeSizeAcc[newRepNode] = sizeRu+sizeRv;
                    }
                }
                if(nodeNum==nodeNumStopCond){
                    break;
                }
            }
            if(nodeNumStopCond==-1){
                break;
            }
            else{
                if(nodeNum>nodeNumStopCond){
                    k *= 1.2f;
                }
                else{
                    break;
                }
            }
        }
        ufdArray.makeContiguous();
        for(typename  GRAPH::NodeIt n(graph);n!=lemon::INVALID;++n){
            const Node node(*n);
            nodeLabeling[node]=ufdArray.findLabel(graph.id(node));
        }
    } 




    namespace detail_graph_smoothing{

    template<
        class GRAPH, 
        class NODE_FEATURES_IN,
        class EDGE_WEIGHTS,
        class WEIGHTS_TO_SMOOTH_FACTOR,
        class NODE_FEATURES_OUT
    >
    void graphSmoothingImpl(
        const GRAPH & g,
        const NODE_FEATURES_IN   & nodeFeaturesIn,
        const EDGE_WEIGHTS       & edgeWeights,
        WEIGHTS_TO_SMOOTH_FACTOR & weightsToSmoothFactor,
        NODE_FEATURES_OUT        & nodeFeaturesOut

    ){
        typedef GRAPH Graph;
        typedef typename Graph::Edge Edge;
        typedef typename Graph::Node Node;
        typedef typename Graph::NodeIt NodeIt;
        typedef typename Graph::OutArcIt OutArcIt;

        typedef typename NODE_FEATURES_IN::Value          NodeFeatureInValue;
        typedef typename NODE_FEATURES_OUT::Reference     NodeFeatureOutRef;
        typedef typename EDGE_WEIGHTS::ConstReference SmoothFactorType;


        //fillNodeMap(g, nodeFeaturesOut, typename NODE_FEATURES_OUT::value_type(0.0));

        for(NodeIt n(g);n!=lemon::INVALID;++n){

            const Node node(*n);

            NodeFeatureInValue    featIn  = nodeFeaturesIn[node];
            NodeFeatureOutRef     featOut = nodeFeaturesOut[node];

            featOut=0;
            float weightSum = 0.0;
            size_t degree    = 0;
            for(OutArcIt a(g,node);a!=lemon::INVALID;++a){
                const Edge edge(*a);
                const Node neigbour(g.target(*a));
                SmoothFactorType smoothFactor= weightsToSmoothFactor(edgeWeights[edge]);

                NodeFeatureInValue neighbourFeat = nodeFeaturesIn[neigbour];
                neighbourFeat*=smoothFactor;
                if(degree==0)
                    featOut = neighbourFeat;
                else
                    featOut += neighbourFeat;
                weightSum+=smoothFactor;
                ++degree;
            }
            // fixme..set me to right type 
            featIn*=static_cast<float>(degree);
            weightSum+=static_cast<float>(degree);
            featOut+=featIn;
            featOut/=weightSum;
        }
    }

    template<class T>
    struct ExpSmoothFactor{
        ExpSmoothFactor(const T lambda,const T edgeThreshold,const T scale)
        :   lambda_(lambda),
            edgeThreshold_(edgeThreshold),
            scale_(scale){
        }
        T operator()(const T weight){
            return weight> edgeThreshold_ ? 0 :  std::exp(-1.0*lambda_*weight)*scale_;
        }
        T lambda_;
        T edgeThreshold_;
        T scale_;
    };

    } // namespace detail_graph_smoothing


    /// \brief smooth node features of a graph
    ///
    /// \param g               : input graph
    /// \param nodeFeaturesIn  : input node features which should be smoothed       
    /// \param edgeIndicator   : edge indicator to indicate over which edges one should smooth        
    /// \param lambda          : scale edge indicator by lambda before taking negative exponent
    /// \param edgeThreshold   : edge threshold
    /// \param scale           : how much smoothing should be applied
    /// \param[out] nodeFeaturesOut : smoothed node features
    template<class GRAPH, class NODE_FEATURES_IN,class EDGE_INDICATOR,class NODE_FEATURES_OUT>
    void graphSmoothing(
        const GRAPH & g,
        const NODE_FEATURES_IN  & nodeFeaturesIn,
        const EDGE_INDICATOR    & edgeIndicator,
        const float lambda,
        const float edgeThreshold,
        const float scale,
        NODE_FEATURES_OUT       & nodeFeaturesOut
    ){
        detail_graph_smoothing::ExpSmoothFactor<float> functor(lambda,edgeThreshold,scale);
        detail_graph_smoothing::graphSmoothingImpl(g,nodeFeaturesIn,edgeIndicator,functor,nodeFeaturesOut);
    }

    /// \brief smooth node features of a graph
    ///
    /// \param g               : input graph
    /// \param nodeFeaturesIn  : input node features which should be smoothed       
    /// \param edgeIndicator   : edge indicator to indicate over which edges one should smooth        
    /// \param lambda          : scale edge indicator by lambda before taking negative exponent
    /// \param edgeThreshold   : edge threshold
    /// \param scale           : how much smoothing should be applied
    /// \param iterations      : how often should this algorithm be called recursively
    /// \param[out] nodeFeaturesBuffer : preallocated(!) buffer to store node features temp.
    /// \param[out] nodeFeaturesOut : smoothed node features
    template<class GRAPH, class NODE_FEATURES_IN,class EDGE_INDICATOR,class NODE_FEATURES_OUT>
    void recursiveGraphSmoothing(
        const GRAPH & g,
        const NODE_FEATURES_IN   & nodeFeaturesIn,
        const EDGE_INDICATOR     & edgeIndicator,
        const float lambda,
        const float edgeThreshold,
        const float scale,
        size_t                    iterations,
        NODE_FEATURES_OUT       & nodeFeaturesBuffer,
        NODE_FEATURES_OUT       & nodeFeaturesOut
    ){

        iterations = std::max(size_t(1),iterations);
        // initial run
        graphSmoothing(g,nodeFeaturesIn,edgeIndicator,lambda,edgeThreshold,scale,nodeFeaturesOut);
        iterations -=1;

        bool outAsIn=true;
        for(size_t i=0;i<iterations;++i){
            if(outAsIn){
                graphSmoothing(g,nodeFeaturesOut,edgeIndicator,lambda,edgeThreshold,scale,nodeFeaturesBuffer);
                outAsIn=false;
            }
            else{
                graphSmoothing(g,nodeFeaturesBuffer,edgeIndicator,lambda,edgeThreshold,scale,nodeFeaturesOut);
                outAsIn=true;
            }
        }
        if(!outAsIn){
            copyNodeMap(g,nodeFeaturesBuffer,nodeFeaturesOut);
        }
    }


    template<class GRAPH, class NODE_MAP, class EDGE_MAP>
    void nodeGtToEdgeGt(
        const GRAPH & g,
        const NODE_MAP & nodeGt,
        const Int64 ignoreLabel,
        EDGE_MAP & edgeGt
    ){
        typedef typename  GRAPH::Node Node;
        typedef typename  GRAPH::EdgeIt EdgeIt;
        typedef typename  GRAPH::Edge Edge;

        for(EdgeIt edgeIt(g); edgeIt!=lemon::INVALID; ++edgeIt){
            const Edge edge(*edgeIt);
            const Node u = g.u(edge);
            const Node v = g.v(edge);

            const Int64 lU = static_cast<Int64>(nodeGt[u]);
            const Int64 lV = static_cast<Int64>(nodeGt[v]);

            if(ignoreLabel==-1 || (lU!=ignoreLabel || lV!=ignoreLabel)){
                edgeGt[edge] = lU == lV ? 0 : 1;
            }
            else{
                edgeGt[edge] = 2;
            }
        }
    }






    /// project ground truth from a base graph, to a region adjacency graph.
    ///     
    ///
    ///
    ///
    template<class RAG, class BASE_GRAPH, class BASE_GRAPH_RAG_LABELS,  
             class BASE_GRAPH_GT,  class RAG_GT, class RAG_GT_QT>
    void projectGroundTruth(
        const RAG                   & rag,
        const BASE_GRAPH            & baseGraph,
        const BASE_GRAPH_RAG_LABELS & baseGraphRagLabels,
        const BASE_GRAPH_GT         & baseGraphGt,
        RAG_GT                      & ragGt,
        RAG_GT_QT                   & ragGtQt
    ){
        typedef typename BASE_GRAPH::Node BaseGraphNode;
        typedef typename BASE_GRAPH::NodeIt BaseGraphNodeIt;
        typedef typename RAG::NodeIt RagNodeIt;
        typedef typename RAG::Node RagNode;
        typedef typename BASE_GRAPH_RAG_LABELS::Value BaseRagLabelType;
        typedef typename BASE_GRAPH_GT::Value GtLabelType;
        typedef typename RAG_GT::Value RagGtLabelType;

        // overlap map
        typedef std::map<GtLabelType,UInt32> MapType;
        typedef typename MapType::const_iterator MapIter;
        typedef typename  RAG:: template NodeMap<MapType> Overlap;
        Overlap overlap(rag);
        
        size_t i=0;
        //::cout<<"\n"; 
        for(BaseGraphNodeIt baseNodeIter(baseGraph); baseNodeIter!=lemon::INVALID; ++baseNodeIter , ++i ){

            //if (i%2000 == 0){
            //    std::cout<<"\r"<<std::setw(10)<< float(i)/float(baseGraph.nodeNum())<<std::flush;
            //}

            const BaseGraphNode baseNode = *baseNodeIter;

            // get gt label
            const GtLabelType gtLabel = baseGraphGt[baseNode];

            // get the label which generated rag 
            // (node mapping from  bg-node to rag-node-id)
            const BaseRagLabelType bgRagLabel = baseGraphRagLabels[baseNode];
            const RagNode  ragNode = rag.nodeFromId(bgRagLabel);

            // fill overlap 
            overlap[ragNode][gtLabel]+=1;
        }
        //std::cout<<"\n"; 
        // select label with max overlap
        for(RagNodeIt ragNodeIter(rag); ragNodeIter!=lemon::INVALID; ++ragNodeIter ){
            const RagNode  ragNode = *ragNodeIter;
            const MapType olMap = overlap[ragNode];
            UInt32 olSize=0;
            RagGtLabelType bestLabel = 0;
            for(MapIter  olIter = olMap.begin(); olIter!=olMap.end(); ++olIter ){
                if(olIter->second > olSize){
                    olSize = olIter->second;
                    bestLabel = olIter->first;
                }
            }
            ragGt[ragNode]=bestLabel;
        }
    }



    /// \brief Find indices of points on the edges
    ///
    /// \param rag : Region adjacency graph of the labels array
    /// \param graph : Graph of labels array
    /// \param affiliatedEdges : The affiliated edges of the region adjacency graph
    /// \param labelsArray : The label image
    /// \param node : The node (of the region adjacency graph), whose edges shall be found
    template<class RAGGRAPH, class GRAPH, class RAGEDGES, unsigned int N, class T>
    MultiArray<2, MultiArrayIndex> ragFindEdges(
            const RAGGRAPH & rag,
            const GRAPH & graph,
            const RAGEDGES & affiliatedEdges,
            MultiArrayView<N, T> labelsArray,
            const typename RAGGRAPH::Node & node
    ){
        typedef typename GRAPH::Node Node;
        typedef typename GRAPH::Edge Edge;
        typedef typename RAGGRAPH::OutArcIt RagOutArcIt;
        typedef typename RAGGRAPH::Edge RagEdge;
        typedef typename GraphDescriptorToMultiArrayIndex<GRAPH>::IntrinsicNodeMapShape NodeCoordinate;

        T nodeLabel = rag.id(node);

        // Find edges and write them into a set.
        std::set< NodeCoordinate > edgeCoordinates;
        for (RagOutArcIt iter(rag, node); iter != lemon::INVALID; ++iter)
        {
            const RagEdge ragEdge(*iter);
            const std::vector<Edge> & affEdges = affiliatedEdges[ragEdge];
            for (int i=0; i<affEdges.size(); ++i)
            {
                Node u = graph.u(affEdges[i]);
                Node v = graph.v(affEdges[i]);
                T uLabel = labelsArray[u];
                T vLabel = labelsArray[v];

                NodeCoordinate coords;
                if (uLabel == nodeLabel)
                {
                    coords = GraphDescriptorToMultiArrayIndex<GRAPH>::intrinsicNodeCoordinate(graph, u);
                }
                else if (vLabel == nodeLabel)
                {
                    coords = GraphDescriptorToMultiArrayIndex<GRAPH>::intrinsicNodeCoordinate(graph, v);
                }
                else
                {
                    vigra_precondition(false, "You should not come to this part of the code.");
                }
                edgeCoordinates.insert(coords);
            }
        }

        // Fill the return array.
        MultiArray<2, MultiArrayIndex> edgePoints(Shape2(edgeCoordinates.size(), N));
        edgePoints.init(0);
        int next = 0;
        typedef typename std::set< NodeCoordinate >::iterator setIter;
        for (setIter iter = edgeCoordinates.begin(); iter!=edgeCoordinates.end(); ++iter)
        {
            NodeCoordinate coords = *iter;
            for (int k=0; k<coords.size(); ++k)
            {
                edgePoints(next, k) = coords[k];
            }
            next++;
        }
        return edgePoints;
    }
    /// \brief create edge weights from node weights
    ///
    /// \param g : input graph
    /// \param nodeWeights : node property map holding node weights
    /// \param[out] edgeWeights : resulting edge weights
    /// \param euclidean : if 'true', multiply the computed weights with the Euclidean
    ///                    distance between the edge's end nodes (default: 'false')
    /// \param func : binary function that computes the edge weight from the 
    ///               weights of the edge's end nodes (default: take the average)
    template<unsigned int N, class DirectedTag,
             class NODEMAP, class EDGEMAP, class FUNCTOR>
    void 
    edgeWeightsFromNodeWeights(
            const GridGraph<N, DirectedTag> & g,
            const NODEMAP  & nodeWeights,
            EDGEMAP & edgeWeights,
            bool euclidean,
            FUNCTOR const & func)
    {
        typedef GridGraph<N, DirectedTag> Graph;
        typedef typename Graph::Edge Edge;
        typedef typename Graph::EdgeIt EdgeIt;
        typedef typename MultiArrayShape<N>::type CoordType;

        vigra_precondition(nodeWeights.shape() == g.shape(), 
             "edgeWeightsFromNodeWeights(): shape mismatch between graph and nodeWeights.");
        
        for (EdgeIt iter(g); iter!=lemon::INVALID; ++iter)
        {
            const Edge edge(*iter);
            const CoordType uCoord(g.u(edge));
            const CoordType vCoord(g.v(edge));
            if (euclidean)
            {
                edgeWeights[edge] = norm(uCoord-vCoord) * func(nodeWeights[uCoord], nodeWeights[vCoord]);
            }
            else
            {
                edgeWeights[edge] = func(nodeWeights[uCoord], nodeWeights[vCoord]);
            }
        }
    }

    template<unsigned int N, class DirectedTag,
             class NODEMAP, class EDGEMAP>
    inline void 
    edgeWeightsFromNodeWeights(
            const GridGraph<N, DirectedTag> & g,
            const NODEMAP  & nodeWeights,
            EDGEMAP & edgeWeights,
            bool euclidean=false)
    {
        using namespace vigra::functor;
        edgeWeightsFromNodeWeights(g, nodeWeights, edgeWeights, euclidean, Param(0.5)*(Arg1()+Arg2()));
    }
    
    
    /// \brief create edge weights from an interpolated image
    ///
    /// \param g : input graph
    /// \param interpolatedImage : interpolated image
    /// \param[out] edgeWeights : edge weights
    /// \param euclidean : if 'true', multiply the weights with the Euclidean
    ///                    distance between the edge's end nodes (default: 'false')
    /// 
    /// For each edge, the function reads the weight from <tt>interpolatedImage[u+v]</tt>,
    /// where <tt>u</tt> and <tt>v</tt> are the coordinates of the edge's end points.
    template<unsigned int N, class DirectedTag,
            class T, class EDGEMAP>
    void 
    edgeWeightsFromInterpolatedImage(
            const GridGraph<N, DirectedTag> & g,
            const MultiArrayView<N, T>  & interpolatedImage,
            EDGEMAP & edgeWeights,
            bool euclidean = false)
    {
        typedef GridGraph<N, DirectedTag> Graph;
        typedef typename Graph::Edge Edge;
        typedef typename Graph::EdgeIt EdgeIt;
        typedef typename MultiArrayShape<N>::type CoordType;

        vigra_precondition(interpolatedImage.shape() == 2*g.shape()-CoordType(1), 
             "edgeWeightsFromInterpolatedImage(): interpolated shape must be shape*2-1");
        
        for (EdgeIt iter(g); iter!=lemon::INVALID; ++iter)
        {
            const Edge edge(*iter);
            const CoordType uCoord(g.u(edge));
            const CoordType vCoord(g.v(edge));
            if (euclidean)
            {
                edgeWeights[edge] = norm(uCoord-vCoord) * interpolatedImage[uCoord+vCoord];
            }
            else
            {
                edgeWeights[edge] = interpolatedImage[uCoord+vCoord];
            }
        }
    }

    template<class GRAPH>
    struct ThreeCycle{

        typedef typename GRAPH::Node Node;

        ThreeCycle(const Node & a, const Node & b, const Node c){
            nodes_[0] = a;
            nodes_[1] = b;
            nodes_[2] = c;
            std::sort(nodes_, nodes_+3);
        }
        bool operator < (const ThreeCycle & other)const{
            if(nodes_[0] < other.nodes_[0]){
                return true;
            }
            else if(nodes_[0] == other.nodes_[0]){
                if(nodes_[1] < other.nodes_[1]){
                    return true;
                }
                else if(nodes_[1] == other.nodes_[1]){
                    if(nodes_[2] < other.nodes_[2]){
                        return true;
                    }
                    else{
                        return false;
                    }
                }
                else{
                    return false;
                }
            }
            else{
                return false;
            }
        }

        Node nodes_[3];


    };


    template<class GRAPH>
    void find3Cycles(
        const GRAPH & g, 
        MultiArray<1, TinyVector<Int32, 3> > & cyclesArray 
    ){
        typedef typename GRAPH::Node Node;
        typedef typename GRAPH::Edge Edge;
        typedef typename GRAPH::EdgeIt EdgeIt;
        typedef typename GRAPH::OutArcIt OutArcIt;

        typedef ThreeCycle<GRAPH> Cycle;

        std::set< Cycle > cycles;
        typedef typename std::set<Cycle>::const_iterator SetIter;
        for (EdgeIt iter(g); iter!=lemon::INVALID; ++iter){
            const Edge edge(*iter);
            const Node u = g.u(edge);
            const Node v = g.v(edge);

            // find a node n which is connected to u and v
            for(OutArcIt outArcIt(g,u); outArcIt!=lemon::INVALID;++outArcIt){
                const Node w = g.target(*outArcIt);
                if(w != v){
                    const Edge e = g.findEdge(w,v);
                    if(e != lemon::INVALID){
                        // found cycle
                        cycles.insert(Cycle(u, v, w));
                    }
                }
            }
        }
        cyclesArray.reshape(TinyVector<UInt32,1>(cycles.size()));
        UInt32 i=0;
        for(SetIter iter=cycles.begin(); iter!=cycles.end(); ++iter){

            const Cycle & c = *iter;
            for(size_t j=0;j<3; ++j){
                cyclesArray(i)[j] = g.id(c.nodes_[j]);
            }
            ++i;
        }
    }

    template<class GRAPH>
    void find3CyclesEdges(
        const GRAPH & g, 
        MultiArray<1, TinyVector<Int32, 3> > & cyclesArray 
    ){
        typedef typename GRAPH::Node Node;
        typedef typename GRAPH::Edge Edge;
        typedef typename GRAPH::EdgeIt EdgeIt;
        typedef typename GRAPH::OutArcIt OutArcIt;

        typedef ThreeCycle<GRAPH> Cycle;

        std::set< Cycle > cycles;
        typedef typename std::set<Cycle>::const_iterator SetIter;
        for (EdgeIt iter(g); iter!=lemon::INVALID; ++iter){
            const Edge edge(*iter);
            const Node u = g.u(edge);
            const Node v = g.v(edge);

            // find a node n which is connected to u and v
            for(OutArcIt outArcIt(g,u); outArcIt!=lemon::INVALID;++outArcIt){
                const Node w = g.target(*outArcIt);
                if(w != v){
                    const Edge e = g.findEdge(w,v);
                    if(e != lemon::INVALID){
                        // found cycle
                        cycles.insert(Cycle(u, v, w));
                    }
                }
            }
        }
        cyclesArray.reshape(TinyVector<UInt32,1>(cycles.size()));
        UInt32 i=0;
        for(SetIter iter=cycles.begin(); iter!=cycles.end(); ++iter){

            const Cycle & c = *iter;
            const Node u = c.nodes_[0];
            const Node v = c.nodes_[1];
            const Node w = c.nodes_[2];

            cyclesArray(i)[0] = g.id(g.findEdge(u, v));
            cyclesArray(i)[1] = g.id(g.findEdge(u, w));
            cyclesArray(i)[2] = g.id(g.findEdge(v, w));
            ++i;
        }
    }

//@}

} // namespace vigra

#endif // VIGRA_GRAPH_ALGORITHMS_HXX