-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork_types.h
1930 lines (1349 loc) · 52.6 KB
/
Network_types.h
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
#ifndef NETWORK_TYPES_H
#define NETWORK_TYPES_H
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<fstream>
#include<string>
#include<cstdlib>
#include<cassert>
#include<algorithm>
#include <boost/random.hpp>
#include<boost/random/uniform_real.hpp>
#include<boost/lexical_cast.hpp>
#include<CGAL/Random.h>
#include <CGAL/QP_models.h>
#include <CGAL/QP_functions.h>
struct Node_type
{
std::string name, place;
double longitude, latitude;
int active_since, active_until, serial_number;
int mark;
struct Edge_type
{
int from,to,weight;
Edge_type():from(0),to(0),weight(0)
{}
};
std::vector<Edge_type> in_edges, out_edges;
Node_type(): longitude(-1), latitude(-1),
active_since(-1), active_until(-2), serial_number(-1), mark(-1)
{}
bool was_active( int t )
{ return ( t>= (active_since-2) && t<= active_until && active_since <= active_until ); }
bool was_active( int t1, int t2 )
{
return ( !( t2< (active_since-2) || t1> active_until )
&& t1<=t2 && active_since <= active_until ); }
bool is_marked()
{ return mark > -1; }
}; //Node_type
struct Is_smaller_node
{
bool operator()(const Node_type &n1, const Node_type& n2 )
{
if( n1.serial_number < n2.serial_number )
return true;
return false;
}
}; // Is_smaller_node
struct Is_smaller_edge
{
bool operator()(const Node_type::Edge_type &e1, const Node_type::Edge_type& e2 )
{
if( e1.from < e2.from )
return true;
if( e2.from < e1.from )
return false;
if( e1.to < e2.to )
return true;
return false;
}
}; // Is_smaller_edge
struct Is_heavier_edge
{
bool operator()(const Node_type::Edge_type &e1,
const Node_type::Edge_type & e2 )
{
if( e1.weight > e2.weight )
return true;
if( e1.weight < e2.weight )
return false;
// The rest of the code is for avoiding mixing
// edges that are distinct but have the same weight.
Is_smaller_edge ise;
return ise(e1, e2);
}
}; // Is_heavier_edge
struct Is_lighter_edge
{
bool operator()(const Node_type::Edge_type &e1,
const Node_type::Edge_type & e2 )
{
if( e1.weight < e2.weight )
return true;
if( e1.weight > e2.weight )
return false;
// The rest of the code is for avoiding mixing
// edges that are distinct but have the same weight.
Is_smaller_edge ise;
return ise(e1, e2);
}
}; // Is_heavier_edge
struct Is_heavier_cn
{
bool operator()(const std::pair<Node_type::Edge_type, double> &e1,
const std::pair<Node_type::Edge_type, double>& e2 )
{
if( e1.second > e2.second )
return true;
if( e1.second < e2.second )
return false;
// The rest of the code is for avoiding mixing
// pairs that are distinct but have the same
// double value.
Is_smaller_edge ise;
return ise(e1.first, e2.first);
}
}; // Is_heavier_cn
class Delinquent_network
{
public:
typedef Node_type Delinquent;
typedef Delinquent::Edge_type Connection;
typedef std::vector<int> Path;
public:
typedef std::vector<Connection>::iterator Connection_iterator;
public:
Delinquent_network():_number_of_connections(-1){}
Delinquent_network( char * filename )
{
std::ifstream in(filename);
// Reading first file with queries
if( !( in.is_open() && in.good() ) )
{
std::cout << " There was a problem with opening the file that stores the network ... aborting " <<std::endl;
std::exit(-1);
}
// Read the first set of lines, the ones that describe the delinquents
std::string line;
std::getline(in,line);
int llline=0;
while( in.good() && line[0]=='(' )
{
Delinquent dlq;
int cfirst=1, c=1;
// Get name
while(c < line.size() && line[c] != ',' && line[c] != ' ' )
c++;
dlq.name = line.substr(cfirst,c-cfirst);
while(c < line.size() && (line[c] == ',' || line[c] == ' ') )
c++;
cfirst = c;
// Get location of activity
while(c < line.size() && line[c] != ',' && line[c] != ' ' )
c++;
dlq.place = line.substr(cfirst,c-cfirst);
while(c < line.size() && (line[c] == ',' || line[c] == ' ') )
c++;
cfirst = c;
// Get period of recorded activity : From
while(c < line.size() && line[c] != ',' && line[c] != ' ' && line[c] != '-' )
c++;
bool single_date = (line[c] == ',');
dlq.active_since = boost::lexical_cast<int>(line.substr(cfirst,c-cfirst));
while(c < line.size() && (line[c] == ',' || line[c] == ' ' || line[c] == '-') )
c++;
cfirst = c;
if(!single_date)
{
// Get period of recorded activity : Until
while(c < line.size() && line[c] != ',' && line[c] != ' ' )
c++;
dlq.active_until = boost::lexical_cast<int>(line.substr(cfirst,c-cfirst));
while(c < line.size() && (line[c] == ',' || line[c] == ' ') )
c++;
cfirst = c;
}
else
dlq.active_until = dlq.active_since;
// Get serial number
while(c < line.size() && line[c] != ',' && line[c] != ' ' && line[c] != ')' )
c++;
dlq.serial_number = boost::lexical_cast<int>(line.substr(cfirst,c-cfirst));
_nodes.push_back(dlq);
line.clear();
std::getline(in,line);
llline++;
} // while( in.good() && line[0]=='(' )
std::sort(_nodes.begin(), _nodes.end(), Is_smaller_node());
for(int jj=0; jj<_nodes.size(); jj++)
_serials_to_nodes[_nodes[jj].serial_number] = jj;
std::map< Connection, int, Is_smaller_edge > _traded_items;
bool keep_reading = true;
do
{
assert(line[0] == '{');
int cfirst=1, c=1, from=-1, to=-1;
Connection cn;
Path p;
while( c < line.size() )
{
while( c < line.size() && line[c] != ',' && line[c] != ' ' && line[c] != '}' )
c++;
int serial = boost::lexical_cast<int>(line.substr(cfirst,c-cfirst));
p.push_back(serial);
if( from == -1 )
from = _serials_to_nodes[serial];
else
{
if( to == -1 )
to = _serials_to_nodes[serial];
else
{
from = to;
to = _serials_to_nodes[serial];
}
cn.from = from;
cn.to = to;
if( _traded_items.find(cn) != _traded_items.end() )
_traded_items[cn]++;
else
_traded_items[cn]=1;
} // else of if( from == -1 )
while( c < line.size() && ( line[c] == ',' || line[c] == ' ' || line[c] == '}') )
c++;
cfirst=c;
} // while( c < line.size() )
_paths.push_back(p);
line.clear();
std::getline(in,line);
++llline;
} while( in.good() );
// Flush all edges that were counted in the _traded_items map.
_number_of_connections = 0;
for( std::map< Connection, int, Is_smaller_edge >::iterator tr_it = _traded_items.begin();
tr_it != _traded_items.end(); tr_it++ )
{
int from_index = (*tr_it).first.from,
to_index = (*tr_it).first.to;
Connection c;
c.from = from_index;
c.to = to_index;
c.weight = (*tr_it).second;
_nodes[from_index].out_edges.push_back(c);
_nodes[to_index].in_edges.push_back(c);
_number_of_connections++;
}
} // Delinquent_network( char * filename )
int number_of_delinquents()
{ return _nodes.size(); }
int number_of_connections()
{
if( _number_of_connections != -1 )
return _number_of_connections;
_number_of_connections = 0;
for(int i=0; i<_nodes.size(); i++ )
_number_of_connections += _nodes[i].in_edges.size();
return _number_of_connections;
}
Delinquent& delinquent(int i)
{ return _nodes[i]; }
Delinquent& delinquent_from_serial(int sr)
{ return _nodes[_serials_to_nodes[sr]]; }
Connection_iterator in_edges_begin( int i )
{ return _nodes[i].in_edges.begin(); }
Connection_iterator in_edges_end( int i )
{ return _nodes[i].in_edges.end(); }
Connection_iterator out_edges_begin( int i )
{ return _nodes[i].out_edges.begin(); }
Connection_iterator out_edges_end( int i )
{ return _nodes[i].out_edges.end(); }
int in_degree( int i )
{ return _nodes[i].in_edges.size(); }
int out_degree( int i )
{ return _nodes[i].out_edges.size(); }
int degree( int i )
{ return in_degree(i)+out_degree(i); }
void insert_node( Delinquent &d )
{ _nodes.push_back(d); }
void insert_path( Path &p )
{ _paths.push_back(p); }
void insert_serial_to_node( int serial, int index)
{ _serials_to_nodes[serial] = index; }
int serial_to_node_index( int serial)
{ return _serials_to_nodes[serial]; }
bool has_edge( int sr1, int sr2 )
{
int ind1 = serial_to_node_index(sr1),
ind2 = serial_to_node_index(sr2);
for( int i=0; i< _nodes[ind1].out_edges.size(); i++ )
if( _nodes[ind1].out_edges[i].to == ind2 )
return true;
return false;
}
bool has_path( std::vector<int>& path )
{
if(path.size()==0)
return false;
if(path.size()==1)
return (serial_to_node_index(path[0]) > 0);
for( int i=0; i<path.size()-1; i++ )
if( !has_edge(path[i],path[i+1]) )
return false;
return true;
}
void print_path( std::vector<Delinquent> &path )
{
if(path.size() == 0)
{
std::cout << std::endl;
return;
}
for( int i=0; i<path.size(); i++ )
{
std::cout << " " << path[i].name;
if(i !=path.size()-1 )
std::cout << " -->" ;
else
std::cout << std::endl;
}
}
void print_path( std::vector<int> &path )
{
if(path.size() == 0)
{
std::cout << std::endl;
return;
}
for( int i=0; i<path.size(); i++ )
{
std::cout << " " << _nodes[serial_to_node_index(path[i])].name;
if(i !=path.size()-1 )
std::cout << " -->" ;
else
std::cout << std::endl;
}
}
// Returns the serial numbers of all the neighbours
// of the node that has INDEX ind.
template < class OutputIterator >
void neighbours( int ind, OutputIterator ot )
{
std::set<int> nachbar;
for( int i=0; i< _nodes[ind].in_edges.size(); i++ )
{
Delinquent from_node = _nodes[_nodes[ind].in_edges[i].from];
nachbar.insert(from_node.serial_number);
}
for( int i=0; i< _nodes[ind].out_edges.size(); i++ )
{
Delinquent to_node = _nodes[_nodes[ind].out_edges[i].to];
nachbar.insert(to_node.serial_number);
}
for( std::set<int>::iterator it = nachbar.begin(); it != nachbar.end(); it++ )
*ot++ = *it;
}
// This differs from the degree() function
// since it counts each distinct incident
// node only once, and not once per edge.
int number_of_neighbours( int i )
{
std::vector<int> nachbar;
neighbours(i,std::back_inserter(nachbar));
return nachbar.size();
}
// Compute the clustering coefficient of the node that has index ind.
double clustering_coefficient( int ind )
{
std::vector<int> nachbar;
neighbours(ind,std::back_inserter(nachbar));
if(nachbar.size() == 0 || nachbar.size() == 1)
return double(0.0);
std::set<int> neighs;
for(int j=0; j<nachbar.size(); j++)
neighs.insert(nachbar[j]);
int total = 0;
for(int j=0; j<nachbar.size(); j++)
{
std::vector<int> nachb;
neighbours(nachbar[j],std::back_inserter(nachb));
for( int k=0; k<nachb.size() ; k++ )
if( neighs.find(nachb[k]) != neighs.end() && nachb[k] != ind )
total++;
}
return double(2.0)*double(total)/(double(nachbar.size())*double(nachbar.size()-1));
}
// Returns the average of the clustering coefficients of all nodes
double average_clustering_coefficient()
{
double avg(0.0);
for( int i=0; i<_nodes.size(); i++ )
avg += clustering_coefficient(i);
return avg/double(_nodes.size());
}
bool is_scale_free()
{
int degree_cut_off = 25;
std::vector<double> degrees;
degrees.assign(_nodes.size(),double(0.0));
for( int i=0; i<_nodes.size(); i++ )
degrees[number_of_neighbours(i)] += double(1.0);
for( int i=1; i< degree_cut_off; i++ )
if( degrees[i] != 0 )
{
double p = degrees[i] /double(_nodes.size());
// std::cout << " Probability of degree " << i <<" : " << p << std::endl;
// std::cout << "Upper bound: " << (double(1.0)/double(i*i)) << std::endl;
// std::cout << "Lower bound: " << (double(1.0)/double(i*i*i)) << std::endl;
if( p >= double(6.0)*(double(1.0)/double(i*i)) ||
p < (double(1.0)/double(i*i*i))/double(6.0) )
return false;
}
return true;
}
// Returns the serial numbers of all the neighbours
// of the node that has SERIAL NUMBER sn.
template < class OutputIterator >
void neighbours_serials( int sn, OutputIterator ot )
{
int index = _serials_to_nodes[sn];
return neighbours(index,ot);
}
// Returns the length (number of edges) of
// the shortest directed path that starts
// from the node with index `index1` and
// ends at the node with index `index2`.
int compute_distance( int index1, int index2 )
{
if( index1 < 0 || index2 < 0 || index1 >= _nodes.size() || index2 >= _nodes.size() )
return -1;
unmark_network();
backward_BFS_labelling(index2);
return _nodes[index1].mark;
}
// Erases all edges incident to the node that has index `index`
void disconnect_node( int index )
{
for(int i=0; i<_nodes[index].in_edges.size(); i++)
{
int from_index = _nodes[index].in_edges[i].from;
std::vector<Connection>::iterator it = _nodes[from_index].out_edges.begin();
while( it != _nodes[from_index].out_edges.end() && (*it).to != index )
it++;
assert(it!=_nodes[from_index].out_edges.end());
_nodes[from_index].out_edges.erase(it);
}
for(int i=0; i<_nodes[index].out_edges.size(); i++)
{
int to_index = _nodes[index].out_edges[i].to;
std::vector<Connection>::iterator it = _nodes[to_index].in_edges.begin();
while( it != _nodes[to_index].in_edges.end() && (*it).from != index )
it++;
assert(it!=_nodes[to_index].in_edges.end());
_nodes[to_index].in_edges.erase(it);
}
_nodes[index].in_edges.clear();
_nodes[index].out_edges.clear();
}
// Returns the simple directed path P from node `index1` to node `index2`
// for which each path edge e=(v1,v2) has the largest possible weight
// among all the edges adjacent to v1 such that there exists a path P'
// from v2 to node `index2`, and P' is simple and does not include
// any vertices of the subpath P-P'.
template< class OutputIterator >
void construct_greedy_capacity_path( int index1, int index2, OutputIterator ot )
{
Delinquent_network new_nw;
// Create a copy of the network
for( std::vector<Delinquent>::iterator rdit = _nodes.begin(); rdit != _nodes.end(); rdit++ )
new_nw.insert_node( *rdit );
for( std::vector<Path>::iterator rpit = _paths.begin(); rpit != _paths.end(); rpit++ )
new_nw.insert_path( *rpit );
for( std::map<int,int>::iterator it_stn = _serials_to_nodes.begin();
it_stn != _serials_to_nodes.end(); it_stn++ )
new_nw.insert_serial_to_node( it_stn->first, it_stn->second );
int curr_index = index1;
std::vector<int> path;
path.push_back(curr_index);
while( curr_index != index2 )
{
std::vector<Connection> out_cn;
for( std::vector<Connection>::iterator it = new_nw.out_edges_begin(curr_index);
it != new_nw.out_edges_end(curr_index); it++ )
out_cn.push_back(*it);
new_nw.disconnect_node(curr_index);
new_nw.unmark_network();
new_nw.backward_BFS_labelling(index2);
int max_weight=-1, next_index=-1;
for( int i=0; i< out_cn.size() ; i++ )
if( new_nw.delinquent(out_cn[i].to).is_marked() && out_cn[i].weight > max_weight )
{
next_index = out_cn[i].to;
max_weight = out_cn[i].weight;
}
if( next_index == -1 )
return;
path.push_back(next_index);
curr_index = next_index;
}
for( int i=0; i<path.size(); i++ )
*ot++ = _nodes[path[i]];
}
// Same as above function, except now there is an aspect of
// randomisation when selecting an edge that expands the path.
template< class OutputIterator >
void construct_greedy_capacity_path_randomized
( int index1, int index2, OutputIterator ot, int seed = 1, double power = 1.0 )
{
CGAL::Random random(seed);
Delinquent_network new_nw;
// Create a copy of the network
for( std::vector<Delinquent>::iterator rdit = _nodes.begin(); rdit != _nodes.end(); rdit++ )
new_nw.insert_node( *rdit );
for( std::vector<Path>::iterator rpit = _paths.begin(); rpit != _paths.end(); rpit++ )
new_nw.insert_path( *rpit );
for( std::map<int,int>::iterator it_stn = _serials_to_nodes.begin();
it_stn != _serials_to_nodes.end(); it_stn++ )
new_nw.insert_serial_to_node( it_stn->first, it_stn->second );
int curr_index = index1;
std::vector<int> path;
path.push_back(curr_index);
while( curr_index != index2 )
{
std::vector<Connection> out_cn;
for( std::vector<Connection>::iterator it = new_nw.out_edges_begin(curr_index);
it != new_nw.out_edges_end(curr_index); it++ )
out_cn.push_back(*it);
new_nw.disconnect_node(curr_index);
new_nw.unmark_network();
new_nw.backward_BFS_labelling(index2);
int next_index=-1;
std::vector<Connection> marked_cn;
double total_weight = 0.0, scaled_weight;
for( int i=0; i< out_cn.size() ; i++ )
if( new_nw.delinquent(out_cn[i].to).is_marked() )
{
scaled_weight = pow(out_cn[i].weight,power);
marked_cn.push_back(out_cn[i]);
marked_cn.back().weight = scaled_weight;
total_weight += scaled_weight;
}
if(marked_cn.empty())
return;
double picked_weight = random.get_double(0,total_weight),
weight_sum=0.0;
int j=0;
while( picked_weight > weight_sum + marked_cn[j].weight )
{
weight_sum += marked_cn[j].weight;
j++;
}
next_index = marked_cn[j].to;
path.push_back(next_index);
curr_index = next_index;
}
for( int i=0; i<path.size(); i++ )
*ot++ = _nodes[path[i]];
}
// Constructs a path on the graph between two nodes
// with indices `index1` and `index2` using the
// maximum weight arboresence of the network
// that is rooted at the node with index `index2`.
// The function implements Prim's algorithm for
// constructing the arboresence.
template< class OutputIterator >
void construct_path_via_maximum_arboresence
( int index1, int index2, OutputIterator ot )
{
if( index1 == index2 )
{
*ot++ = _nodes[index2];
return;
}
std::priority_queue<Connection, std::vector<Connection>, Is_lighter_edge> wavefront_edges;
std::set<Connection, Is_smaller_edge> selected_edges;
unmark_network();
_nodes[index2].mark = 1;
for(int i=0; i< _nodes[index2].in_edges.size(); i++ )
{
int neighbour = _nodes[index2].in_edges[i].from;
if( _nodes[neighbour].is_marked() == false )
wavefront_edges.push(_nodes[index2].in_edges[i]);
}
// Execute Pim's algorithm and mark the edges of the arboresence on the current network.
int label = 2;
while( !wavefront_edges.empty() )
{
while( !wavefront_edges.empty() &&
_nodes[wavefront_edges.top().from].is_marked() )
wavefront_edges.pop();
if( !wavefront_edges.empty() )
{
int curr_index = wavefront_edges.top().from;
selected_edges.insert(wavefront_edges.top());
wavefront_edges.pop();
_nodes[curr_index].mark = label++;
for(int i=0; i< _nodes[curr_index].in_edges.size(); i++ )
{
int neighbour = _nodes[curr_index].in_edges[i].from;
if( _nodes[neighbour].is_marked() == false )
wavefront_edges.push(_nodes[curr_index].in_edges[i]);
} // for(int i=0; ... )
} // if( !wavefront_edges.empty() )
} // while( !wavefront_edges.empty() )
if( _nodes[index1].is_marked() == false )
return;
int t_index = index1;
*ot++ = _nodes[t_index];
do
{
int max_weight=-1;
for( int i=0; i<_nodes[t_index].out_edges.size(); i++ )
{
if( selected_edges.find(_nodes[t_index].out_edges[i]) != selected_edges.end() )
{
t_index = _nodes[t_index].out_edges[i].to;
break;
}
}
*ot++ = _nodes[t_index];
}while(t_index != index2);
} // construct_path_via_maximum_arboresence(...)
// Construct the maximum weight path of legth `d`
// between nodes with indices `index1` and `index2`
template< class OutputIterator >
void construct_maximum_weight_path_of_length
( int index1, int index2, int d, OutputIterator ot )
{
unmark_network();
// Make an index number for each edge,
// to use it for indicating the corresponding
// variable in the IP program.
std::map<Connection,int,Is_smaller_edge> edges_to_vars;
std::map<int,Connection> vars_to_edges;
int variable_index = 0;
for( int i=0; i<_nodes.size(); i++ )
for( int j=0; j<_nodes[i].in_edges.size(); j++ )
{
edges_to_vars[_nodes[i].in_edges[j]] = variable_index;
vars_to_edges[variable_index] = _nodes[i].in_edges[j];
variable_index++;
}
CGAL::Quadratic_program<int> ip(CGAL::EQUAL, true, 0, false, 0);
CGAL::Quadratic_program_solution<double> solution;
int equation_index = 0;
// Restriction #1: for every node, except the ones with indices
// `index1` and `index2`, the number of selected in-edges equals
// the number of the selected out-edges.
for( int i=0; i<_nodes.size(); i++ )
if( i != index1 && i != index2 )
{
for( int j=0; j<_nodes[i].in_edges.size(); j++ )
ip.set_a(edges_to_vars[_nodes[i].in_edges[j]],equation_index,1);
for( int j=0; j<_nodes[i].out_edges.size(); j++ )
ip.set_a(edges_to_vars[_nodes[i].out_edges[j]],equation_index,-1);
ip.set_b(equation_index, 0);
ip.set_r(equation_index, CGAL::EQUAL);
equation_index++;
}
// Restriction #2: Every node, except the ones with indices
// `index1` and `index2`, has at most one selected in-edge,
// and at most one selected out-edge.
for( int i=0; i<_nodes.size(); i++ )
if( i != index1 && i != index2 )
{
for( int j=0; j<_nodes[i].in_edges.size(); j++ )
ip.set_a(edges_to_vars[_nodes[i].in_edges[j]],equation_index,1);
ip.set_b(equation_index,1);
ip.set_r(equation_index, CGAL::SMALLER);
equation_index++;
for( int j=0; j<_nodes[i].out_edges.size(); j++ )
ip.set_a(edges_to_vars[_nodes[i].out_edges[j]],equation_index,1);
ip.set_b(equation_index,1);
ip.set_r(equation_index, CGAL::SMALLER);
equation_index++;
}
// Restriction #3: Node with `index1` has exactly one selected out-edge,
// and node with `index2` has exactly one selected in-edge.
for( int j=0; j<_nodes[index1].out_edges.size(); j++ )
ip.set_a(edges_to_vars[_nodes[index1].out_edges[j]],equation_index,1);
ip.set_b(equation_index,1);
ip.set_r(equation_index, CGAL::EQUAL);
equation_index++;
for( int j=0; j<_nodes[index2].in_edges.size(); j++ )
ip.set_a(edges_to_vars[_nodes[index2].in_edges[j]],equation_index,1);
ip.set_b(equation_index,1);
ip.set_r(equation_index, CGAL::EQUAL);
equation_index++;
// Restriction #4 The number of path edges should be equal to d.
if( d >= 0 )
{
for( int i=0; i<_nodes.size(); i++ )
for( int j=0; j<_nodes[i].in_edges.size(); j++ )
ip.set_a(edges_to_vars[_nodes[i].in_edges[j]],equation_index,1);
ip.set_b(equation_index,d);
ip.set_r(equation_index, CGAL::EQUAL);
}
// Value restriction: Every variable should have value at most one.
for( int i=0; i<_nodes.size(); i++ )
for( int j=0; j<_nodes[i].in_edges.size(); j++ )
ip.set_u(edges_to_vars[_nodes[i].in_edges[j]], true, 1);
// Definition of objective function.
for( int i=0; i<_nodes.size(); i++ )
for( int j=0; j<_nodes[i].in_edges.size(); j++ )
ip.set_c(edges_to_vars[_nodes[i].in_edges[j]], -_nodes[i].in_edges[j].weight);
solution = CGAL::solve_linear_program(ip, double());