-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct_community_hierarchy.cpp
2054 lines (1434 loc) · 56.3 KB
/
construct_community_hierarchy.cpp
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
#include<Rcpp.h>
#include<vector>
#include<set>
#include<string>
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<list>
#include<map>
#include<queue>
#include<cstdlib>
#include<cctype>
///////////////////////////////////////////////////////////////////////////
//////////////// Function objects on Multipartite Networks ////////////////
///////////////////////////////////////////////////////////////////////////
namespace MultipartiteNetworkFunctors{
class Edge_list_inner_product
{
public:
template<class NODE_TYPE>
double operator()( NODE_TYPE& n1, NODE_TYPE& n2 ) const
{
typedef NODE_TYPE Node;
typedef typename Node::Neighbour_element Neighbour_element;
typedef typename Node::Neighbour_iterator Neighbour_iterator;
int s1 = n1.number_of_neighbours(),
s2 = n2.number_of_neighbours();
double product(0.0);
Neighbour_iterator nit_1 = n1.neighbours_begin(),
nit_2 = n2.neighbours_begin();
while( nit_1 !=n1.neighbours_end() && nit_2 != n2.neighbours_end() )
{
Neighbour_element nlm1 = *nit_1, nlm2 = *nit_2;
if(nlm1 == nlm2)
{
product += (nlm1.weight())*(nlm2.weight());
nit_1++;
nit_2++;
}
else if( nlm1 < nlm2 )
nit_1++;
else
nit_2++;
}
// Check also if the two nodes are neighbours
Neighbour_element slm(n1.group(),n1.index(),double(0.0),0);
Neighbour_iterator cit = n2.find_neighbour(slm);
if( cit != n2.neighbours_end() )
{
product += n1.compute_predicate_value_1()*(cit->weight());
product += n2.compute_predicate_value_1()*(cit->weight());
}
return product;
} // double operator()( Node& n1, Node& n2 ) const
}; // class Edge_list_inner_product
class Tanimoto_coefficient
{
public:
template<class NODE_TYPE>
double operator()( NODE_TYPE& n1, NODE_TYPE& n2 ) const
{
double inner_product = Edge_list_inner_product()(n1,n2);
return inner_product/( n1.compute_predicate_value_2()
+ n2.compute_predicate_value_2() -inner_product );
} // double operator()( const Node& n1, const Node& n2 ) const
}; // Tanimoto_coefficient
class Minimum_weight_sum
{
public:
template<class NODE_TYPE>
double operator()( NODE_TYPE& n1, NODE_TYPE& n2 ) const
{ return std::min(n1.compute_predicate_value_1(), n2.compute_predicate_value_1());}
}; // Minimum_weight_sum
} // MultipartiteNetworkFunctors
///////////////////////////////////////////////////////////////////////////
//////////////////// Multipartite Network Edge Traits /////////////////////
///////////////////////////////////////////////////////////////////////////
namespace MultipartiteNetwork{
template < class NELM >
struct Is_greater_element
{
typedef NELM Neigh_element;
bool operator()( const Neigh_element& ne1, const Neigh_element& ne2 ) const
{
if(ne1.group() < ne2.group())
return true;
if(ne1.group() > ne2.group())
return false;
if(ne1.index() < ne2.index())
return true;
return false;
}
}; // Is_greater_element
class Edge_traits_base
{
public:
class Neighbour_element_base
{
public:
Neighbour_element_base(int group, int ind, int edge_ind ):
_group(group),_index(ind),_edge_index(edge_ind){}
int index(void) const
{ return _index; }
int group(void) const
{ return _group; }
int edge_index(void) const
{ return _edge_index; }
bool operator==(const Neighbour_element_base &nlm) const
{
if(nlm.group() != this->group() || nlm.index() != this->index())
return false;
return true;
}
bool operator<(const Neighbour_element_base &nlm) const
{ return Is_greater_element<Neighbour_element_base>()(*this,nlm); }
bool operator>(const Neighbour_element_base &nlm) const
{ return Is_greater_element<Neighbour_element_base>()(nlm,*this); }
private:
int _group, _index, _edge_index;
}; // class Neighbour_element_base
class Primary_edge_base
{
public:
Primary_edge_base(){}
int index(void) const
{ return _index; }
void set_index(int index)
{ _index = index; }
private:
int _index;
}; // Primary_edge_base
}; // class Edge_traits_base
class Tanimoto_node_traits
{
public:
typedef MultipartiteNetworkFunctors::Tanimoto_coefficient Similarity_function;
class Unweighted_node_base
{
protected:
Unweighted_node_base(){}
template<class Neighbour_iterator>
double _compute_predicate_value_1( Neighbour_iterator rbegin, Neighbour_iterator rend, int size )
{ return double(1.0); }
template<class Neighbour_iterator>
double _compute_predicate_value_2( Neighbour_iterator rbegin, Neighbour_iterator rend, int size )
{ return double(size+1); }
}; // Node_base
class Weighted_node_base
{
protected:
Weighted_node_base():_avg(-1.0),_squared_norm(-1.0){}
template<class Neighbour_iterator>
double _compute_predicate_value_1( Neighbour_iterator rbegin, Neighbour_iterator rend, int size )
{
if( _avg == double(-1.0) )
{
_avg = double(0.0);
for( Neighbour_iterator nit = rbegin; nit != rend; nit++ )
_avg += nit->weight();
_avg = _avg/double(size);
}
return _avg;
}
template<class Neighbour_iterator>
double _compute_predicate_value_2( Neighbour_iterator rbegin, Neighbour_iterator rend, int size )
{
if( _squared_norm == double(-1.0) )
{
_squared_norm = double(0.0);
for( Neighbour_iterator nit = rbegin; nit != rend; nit++ )
_squared_norm += (nit->weight())*(nit->weight());
double avgg = _compute_predicate_value_1(rbegin,rend,size);
_squared_norm += avgg*avgg;
}
return _squared_norm;
}
private:
double _avg, _squared_norm;
}; // Weighted_node_base
}; // class Tanimoto_node_traits
class Minimum_weight_sum_node_traits
{
public:
typedef MultipartiteNetworkFunctors::Minimum_weight_sum Similarity_function;
class Unweighted_node_base
{
protected:
Unweighted_node_base(){}
template<class Neighbour_iterator>
double _compute_predicate_value_1( Neighbour_iterator rbegin, Neighbour_iterator rend, int size )
{ return double(size); }
template<class Neighbour_iterator>
double _compute_predicate_value_2( Neighbour_iterator rbegin, Neighbour_iterator rend, int size )
{ return double(-1.0); }
}; // Node_base
class Weighted_node_base
{
protected:
Weighted_node_base():_weight_sum(-1.0){}
template<class Neighbour_iterator>
double _compute_predicate_value_1( Neighbour_iterator rbegin, Neighbour_iterator rend, int size )
{
if( _weight_sum == double(-1.0) )
{
_weight_sum = double(0.0);
for( Neighbour_iterator nit = rbegin; nit != rend; nit++ )
_weight_sum += nit->weight();
}
return _weight_sum;
}
template<class Neighbour_iterator>
double _compute_predicate_value_2( Neighbour_iterator rbegin, Neighbour_iterator rend, int size )
{return double(-1.0); }
private:
double _weight_sum;
}; // Weighted_node_base
}; // class Minimum_weight_sum_node_traits
template< class PNT>
class Unweighted_edge_traits: public Edge_traits_base, PNT
{
private:
typedef PNT Predicate_node_traits;
typedef Edge_traits_base Traits_base;
typedef typename Traits_base::Neighbour_element_base Neighbour_base;
typedef typename Traits_base::Primary_edge_base Primary_edge_base;
public:
typedef typename Predicate_node_traits::Unweighted_node_base Node_base;
typedef typename Predicate_node_traits::Similarity_function Similarity_function;
class Neighbour_element: public Neighbour_base
{
typedef Neighbour_base Base;
public:
Neighbour_element(int group, int ind, double w, int edge_ind ):
Base(group,ind,edge_ind){}
double weight(void) const
{ return double(1.0); }
};
typedef Is_greater_element<Neighbour_element> Compare_pred;
typedef std::set<Neighbour_element,Compare_pred> Neighbour_set;
typedef typename Neighbour_set::iterator Neighbour_iterator;
class Edge_base : public Primary_edge_base
{
typedef Primary_edge_base Base;
public:
Edge_base():Base(){}
void set_weight( const double& w )
{}
double weight(void) const
{ return 1.0; }
}; // Edge_base
}; // Unweighted_edge_traits
template< class PNT >
class Weighted_edge_traits : public Edge_traits_base, PNT
{
typedef PNT Predicate_node_traits;
typedef Edge_traits_base Traits_base;
typedef typename Traits_base::Neighbour_element_base Neighbour_base;
typedef typename Traits_base::Primary_edge_base Primary_edge_base;
public:
typedef typename Predicate_node_traits::Weighted_node_base Node_base;
typedef typename Predicate_node_traits::Similarity_function Similarity_function;
class Neighbour_element: public Neighbour_base
{
typedef Neighbour_base Base;
public:
Neighbour_element(int group, int ind, double w, int edge_ind ):
_weight(w), Base(group,ind,edge_ind){}
double weight(void) const
{ return _weight; }
private:
double _weight;
}; // class Neighbour_element
typedef Is_greater_element<Neighbour_element> Compare_pred;
typedef std::set<Neighbour_element, Compare_pred> Neighbour_set;
typedef typename Neighbour_set::iterator Neighbour_iterator;
class Edge_base : public Primary_edge_base
{
typedef Primary_edge_base Base;
public:
Edge_base():_w(-1.0),Base(){}
void set_weight( const double& w )
{ _w = w; }
double weight(void) const
{ return _w; }
private:
double _w;
}; // Edge_base
}; // Weighted_edge_traits
typedef Unweighted_edge_traits<Tanimoto_node_traits> Standard_unweighted_edge_traits;
typedef Weighted_edge_traits<Tanimoto_node_traits> Standard_weighted_edge_traits;
typedef Unweighted_edge_traits<Minimum_weight_sum_node_traits> MSW_unweighted_edge_traits;
typedef Weighted_edge_traits<Minimum_weight_sum_node_traits> MSW_weighted_edge_traits;
} // namespace MultipartiteNetwork
///////////////////////////////////////////////////////////////////////////
/////////////////////// Multipartite Network Types ////////////////////////
///////////////////////////////////////////////////////////////////////////
namespace MultipartiteNetwork{
template < class TRAITS >
class Multipartite_network_node :
public TRAITS::Edge_weight_traits::Node_base
{
public:
typedef TRAITS Network_traits;
typedef typename Network_traits::Edge_weight_traits Edge_weight_traits;
typedef typename Edge_weight_traits::Neighbour_element Neighbour_element;
typedef typename Edge_weight_traits::Neighbour_set Neighbour_set;
typedef typename Edge_weight_traits::Neighbour_iterator Neighbour_iterator;
public:
Multipartite_network_node(){};
void set_name(const std::string& name)
{ _name = name;}
void set_group(int group)
{ _group = group;}
void set_index(int index)
{ _index = index;}
std::string name(void) const
{ return _name;}
int group(void) const
{ return _group;}
int index(void) const
{ return _index;}
double compute_predicate_value_1()
{ return _compute_predicate_value_1( neighbours_begin(), neighbours_end(), number_of_neighbours() ); }
double compute_predicate_value_2()
{ return _compute_predicate_value_2(neighbours_begin(), neighbours_end(), number_of_neighbours() ); }
void insert_neighbour(Neighbour_element ne)
{ _neighbours.insert(ne); }
int number_of_neighbours(void) const
{ return _neighbours.size(); }
Neighbour_iterator find_neighbour( const Neighbour_element nelm ) const
{ return _neighbours.find(nelm); }
Neighbour_iterator neighbours_begin()
{ return _neighbours.begin(); }
Neighbour_iterator neighbours_end()
{ return _neighbours.end(); }
bool is_marked()
{ return _mark;}
void set_mark(bool m)
{ _mark=m; }
private:
int _index;
int _group;
Neighbour_set _neighbours;
std::string _name;
bool _mark;
}; // Multipartite_network_node
template< class TRAITS >
class Multipartite_network_edge:
public TRAITS::Edge_weight_traits::Edge_base
{
public:
typedef TRAITS Network_traits;
typedef typename Network_traits::Node_type Node;
private:
typedef typename Network_traits::Edge_weight_traits Edge_weight_traits;
public:
Multipartite_network_edge()
{
_mark = false;
}
void set_adjacent_nodes( int group1, int ind1, int group2, int ind2 )
{
_group1 = group1;
_group2 = group2;
_ind1 = ind1;
_ind2 = ind2;
}
std::pair<int,int> source_node(void) const
{ return std::make_pair(_group1,_ind1); }
std::pair<int,int> target_node(void) const
{ return std::make_pair(_group2,_ind2); }
bool mark()
{ return _mark;}
void set_mark(bool m)
{ _mark = m;}
private:
int _group1, _group2, _ind1,_ind2;
bool _mark;
}; // Multipartite_network_edge
template< class TRAITS >
class Multipartite_network
{
public:
typedef TRAITS Network_traits;
typedef typename Network_traits::Node_type Node;
typedef typename Network_traits::Edge_type Edge;
typedef typename Network_traits::Neighbour_element Neighbour_element;
typedef typename Node::Neighbour_iterator Neighbour_iterator;
public:
Multipartite_network():_number_of_nodes(-1), _is_connected(-1){}
void extract_bipartite_network_from_csv( char *filename );
void construct_bipartite_network_from_edge_list
( std::vector<std::string> nodes_1, std::vector<std::string> nodes_2,
std::vector<int> endpoints_1, std::vector<int> endpoints_2, std::vector<double> weights);
void construct_bipartite_network_from_edge_list( int *n_1, char **nodes_1, int *n_2, char **nodes_2,
int *e, int *endpoints_1, int *endpoints_2, double *weights);
void export_as_txt();
// void Extract_multipartite_network(int i, std::string filename);
int number_of_node_groups(void) const
{ return _nodes.size(); }
int number_of_nodes(void)
{
if(_number_of_nodes == -1)
{
_number_of_nodes = 0;
for( int i=0; i<_nodes.size(); i++ )
_number_of_nodes += _nodes[i].size();
}
return _number_of_nodes;
}
int number_of_nodes_in_group(int group) const
{ return _nodes[group].size(); }
int number_of_edges(void) const
{ return _edges.size(); }
Node& node(int group, int index)
{ return _nodes[group][index]; }
// const Node& node(int group, int index) const
// { return _nodes[group][index]; }
Edge& edge(int ind)
{ return _edges[ind]; }
void unmark_all_nodes()
{
for(int i=0; i<_nodes.size(); i++)
for(int j=0; j<_nodes[i].size(); j++)
_nodes[i][j].set_mark(false);
}
bool is_connected()
{
if(_is_connected != -1)
return _is_connected;
if(number_of_nodes() == 0)
return false;
std::queue< std::pair<int, int> > node_queue;
unmark_all_nodes();
node_queue.push(std::make_pair(0,0));
while(node_queue.size() != 0)
{
std::pair<int, int> node_ip = node_queue.front();
node_queue.pop();
if( !node(node_ip.first,node_ip.second).is_marked() )
{
node(node_ip.first,node_ip.second).set_mark(true);
for( Neighbour_iterator nit = node(node_ip.first,node_ip.second).neighbours_begin();
nit != node(node_ip.first,node_ip.second).neighbours_end(); nit++ )
{
int g = nit->group(), i = nit->index();
if( !node(g,i).is_marked() )
node_queue.push(std::make_pair(g,i));
}
} // if( !node(node_ip.first,node_ip.second).is_marked() )
} // while(node_queue.size() != 0)
_is_connected = 1;
for(int i=0; i<_nodes.size(); i++)
for(int j=0; j<_nodes[i].size(); j++)
if( !_nodes[i][j].is_marked() )
{
_is_connected = 0;
return false;
}
return true;
} // bool is_connected()
void print_edge(int ind, std::ostream &os = std::cout)
{
std::pair<int,int> snd = _edges[ind].source_node(),
tnd = _edges[ind].target_node();
os << _nodes[snd.first][snd.second].name() << " - "
<< _nodes[tnd.first][tnd.second].name() << std::endl;
}
private:
int _number_of_nodes;
std::vector< std::vector<Node> > _nodes;
std::vector<Edge> _edges;
int _is_connected;
}; // class Multipartite_network
// Auxiliary type for the main clustering class.
// Does not appear in the traits class.
template <class TRAITS>
class Edge_cluster_node
{
private:
typedef TRAITS Network_traits;
typedef typename Network_traits::Edge_type Edge;
typedef typename Network_traits::Node_type Node;
typedef typename std::map< int, std::set<int>* >::iterator Container_iterator;
public:
Edge_cluster_node( int cluster_index, const Edge& ed, int number_of_node_groups)
{
_nodes = new std::map< int, std::set<int>* >();
std::pair<int,int> source_nd = ed.source_node(),
target_nd = ed.target_node();
insert_network_node(source_nd.first, source_nd.second);
insert_network_node(target_nd.first, target_nd.second);
if( source_nd.first == target_nd.first )
_sum_of_group_size_squares = 4;
else
_sum_of_group_size_squares = 2;
_edge = ed;
_cluster_index = cluster_index;
_father_index = cluster_index;
_number_of_edges = 1;
_number_of_nodes = 2;
_depth = 0;
}
double cluster_partition_density(void) const
{
if( _number_of_edges+1-_number_of_nodes == 0 )
return double(0.0);
return double(_number_of_edges+1-_number_of_nodes)/
( (double(_number_of_nodes)*double(_number_of_nodes))
- _sum_of_group_size_squares - double(2*_number_of_nodes)+double(2) );
}
bool is_representative(void) const
{ return _cluster_index == _father_index;}
void set_father_index(int new_index)
{ _father_index = new_index; }
int cluster_index(void) const
{ return _cluster_index; }
int father_index(void) const
{ return _father_index; }
bool insert_network_node( int group, int index )
{
std::set<int>* set_p;
Container_iterator s_it;
if( (s_it = _nodes->find(group)) == _nodes->end() )
set_p = (*_nodes)[group] = new std::set<int>();
else
set_p = s_it->second;
bool inserted = set_p->insert(index).second;
if(inserted)
{
_number_of_nodes++;
int size = set_p->size();
_sum_of_group_size_squares += (double(size)*double(size) - double(size-1)*double(size-1)) ;
}
return inserted;
}
std::map< int, std::set<int>* >* node_container_pointer() const
{ return _nodes; }
void set_node_container_pointer( std::map< int, std::set<int>* >* cont )
{ _nodes = cont; }
int depth(void) const
{ return _depth;}
Edge edge(void) const
{ return _edge; }
void set_number_of_edges( int num )
{ _number_of_edges = num; }
void set_depth( int new_depth )
{ _depth = new_depth; }
int number_of_nodes(void) const
{ return _number_of_nodes; }
int number_of_edges(void) const
{ return _number_of_edges; }
double sum_of_group_size_squares(void) const
{ return _sum_of_group_size_squares;}
void set_number_of_nodes( int num )
{ _number_of_nodes = num; }
void set_sum_of_group_size_squares( double sgsq )
{ _sum_of_group_size_squares = sgsq;}
private:
int _cluster_index, _father_index, _depth, _number_of_edges, _number_of_nodes;
std::map< int, std::set<int>* >* _nodes;
double _sum_of_group_size_squares; // In reality, always an integer
// but double here to avoid overflow for large values.
// This value is maintained for the reason
// of a more efficient computation of
// the partition density.
Edge _edge;
}; // class Edge_cluster_node
template <class TRAITS>
class Single_linkage_clustering
{
private:
typedef TRAITS Network_traits;
typedef Edge_cluster_node<Network_traits> Union_find_node;
typedef typename Network_traits::Multipartite_network Multipartite_network;
typedef typename Network_traits::Node_type Node;
typedef typename Network_traits::Edge_type Edge;
typedef typename Node::Neighbour_element Neighbour_element;
typedef typename Node::Neighbour_iterator Neighbour_iterator;
typedef typename Network_traits::Similarity_function Similarity_function;
struct Edge_link
{
int edge_index_1, edge_index_2;
double similarity;
}; // Edge_link
struct Merge_event
{
std::map<int,std::set<int> > groupings;
double similarity, partition_density;
int added_links;
};
struct Is_greater_edge_link
{
bool operator()( const Edge_link& el1, const Edge_link& el2 )
{
if(el1.similarity > el2.similarity)
return true;
if(el1.similarity < el2.similarity)
return false;
if(el1.edge_index_1 < el2.edge_index_1)
return true;
if(el1.edge_index_1 > el2.edge_index_1)
return false;
if(el1.edge_index_2 < el2.edge_index_2)
return true;
return false;
}
}; // Is_greater_edge_link
struct Is_greater_cluster
{
bool operator()( const std::vector<Edge>& ev1, const std::vector<Edge>& ev2 )
{
if(ev2.size() == 0 && ev1.size() != 0)
return true;
if(ev1.size() == 0)
return false;
if( ev1[0].source_node().first < ev2[0].source_node().first )
return true;
if( ev1[0].source_node().first > ev2[0].source_node().first )
return false;
if( ev1[0].source_node().second < ev2[0].source_node().second )
return true;
if( ev1[0].source_node().second > ev2[0].source_node().second )
return false;
if( ev1[0].target_node().first < ev2[0].target_node().first )
return true;
if( ev1[0].target_node().first > ev2[0].target_node().first )
return false;
if( ev1[0].target_node().second < ev2[0].target_node().second )
return true;
return false;
}
}; // Is_greater_cluster
struct Merge_tree_node
{
std::vector<int> children;
std::string name;
int edge_weight, total_depth, parent, edges_in_subtree, edge_range_begin, edge_range_end;
int number_of_children()
{ return children.size(); }
};
private:
Union_find_node& find(int cluster_index)
{
Union_find_node &ufn = _union_find_ds[cluster_index];
if( ufn.is_representative() )
return ufn;
Union_find_node &new_father_node = find(ufn.father_index());
ufn.set_father_index( new_father_node.cluster_index() );
return new_father_node;
} // Union_find_node& find(int cluster_index)
double unionn( int cluster_index1, int cluster_index2 )
{
Union_find_node &ufn1 = find(cluster_index1),
&ufn2 = find(cluster_index2);
if( ufn1.depth() >= ufn2.depth() )
return unionn( ufn1, ufn2 );
else
return unionn( ufn2, ufn1 );
} // double unionn( int ..., int ... )
double unionn( Union_find_node& ufn1, Union_find_node& ufn2 )
{
ufn2.set_father_index(ufn1.cluster_index());
if(ufn1.depth() == ufn2.depth())
ufn1.set_depth( ufn1.depth()+1 );
ufn1.set_number_of_edges( ufn1.number_of_edges()+ufn2.number_of_edges() );
Union_find_node *big, *small;
if( ufn1.number_of_nodes() >= ufn2.number_of_nodes() )
{
big = &ufn1;
small = &ufn2;
}
else
{
big = &ufn2;
small = &ufn1;
}
std::map<int, std::set<int>* >* nd_ptr = small->node_container_pointer();
typename std::map<int, std::set<int>* >::iterator it;
for( it = nd_ptr->begin(); it != nd_ptr->end(); it++ )
{
std::set<int>* ns = it->second;
typename std::set<int>::iterator sit;
for( sit = ns->begin(); sit != ns->end(); sit++ )
big->insert_network_node(it->first, *sit);
ns->clear();
delete ns;
}
nd_ptr->clear();
delete nd_ptr;