File: walktrap_communities.cpp

package info (click to toggle)
r-cran-igraph 1.0.1-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 18,160 kB
  • sloc: ansic: 173,529; cpp: 19,365; fortran: 4,550; yacc: 1,164; tcl: 931; lex: 484; makefile: 149; sh: 9
file content (843 lines) | stat: -rw-r--r-- 27,803 bytes parent folder | download | duplicates (8)
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
/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2007-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

/* The original version of this file was written by Pascal Pons
   The original copyright notice follows here. The FSF address was
   fixed by Tamas Nepusz */

// File: communities.cpp
//-----------------------------------------------------------------------------
// Walktrap v0.2 -- Finds community structure of networks using random walks
// Copyright (C) 2004-2005 Pascal Pons
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
// 02110-1301 USA
//-----------------------------------------------------------------------------
// Author   : Pascal Pons 
// Email    : pascal.pons@gmail.com
// Web page : http://www-rp.lip6.fr/~latapy/PP/walktrap.html
// Location : Paris, France
// Time	    : June 2005
//-----------------------------------------------------------------------------
// see readme.txt for more details

#include "walktrap_communities.h"
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>

#include "config.h"

namespace igraph {

namespace walktrap {

IGRAPH_THREAD_LOCAL int Probabilities::length = 0;
IGRAPH_THREAD_LOCAL Communities* Probabilities::C = 0;
IGRAPH_THREAD_LOCAL float* Probabilities::tmp_vector1 = 0;
IGRAPH_THREAD_LOCAL float* Probabilities::tmp_vector2 = 0;
IGRAPH_THREAD_LOCAL int* Probabilities::id = 0;
IGRAPH_THREAD_LOCAL int* Probabilities::vertices1 = 0;
IGRAPH_THREAD_LOCAL int* Probabilities::vertices2 = 0;
IGRAPH_THREAD_LOCAL int Probabilities::current_id = 0;


Neighbor::Neighbor() {
  next_community1 = 0;
  previous_community1 = 0;
  next_community2 = 0;	     
  previous_community2 = 0;
  heap_index = -1;
}

Probabilities::~Probabilities() {
  C->memory_used -= memory();
  if(P) delete[] P;
  if(vertices) delete[] vertices;
}

Probabilities::Probabilities(int community) {
  Graph* G = C->G;
  int nb_vertices1 = 0;
  int nb_vertices2 = 0;

  float initial_proba = 1./float(C->communities[community].size);
  int last =  C->members[C->communities[community].last_member];  
  for(int m = C->communities[community].first_member; m != last; m = C->members[m]) {
    tmp_vector1[m] = initial_proba;
    vertices1[nb_vertices1++] = m;
  }
  
  for(int t = 0; t < length; t++) {
    current_id++;    
    if(nb_vertices1 > (G->nb_vertices/2)) {
      nb_vertices2 = G->nb_vertices;
      for(int i = 0; i < G->nb_vertices; i++)
	tmp_vector2[i] = 0.;
      if(nb_vertices1 == G->nb_vertices) {
	for(int i = 0; i < G->nb_vertices; i++) {
	  float proba = tmp_vector1[i]/G->vertices[i].total_weight;
  	  for(int j = 0; j < G->vertices[i].degree; j++)
	    tmp_vector2[G->vertices[i].edges[j].neighbor] += proba*G->vertices[i].edges[j].weight;
	}
      }
      else {
	for(int i = 0; i < nb_vertices1; i++) {
	  int v1 = vertices1[i];
	  float proba = tmp_vector1[v1]/G->vertices[v1].total_weight;
  	  for(int j = 0; j < G->vertices[v1].degree; j++)
	    tmp_vector2[G->vertices[v1].edges[j].neighbor] += proba*G->vertices[v1].edges[j].weight;
	}
      }
    }
    else {
      nb_vertices2 = 0;
      for(int i = 0; i < nb_vertices1; i++) {
	int v1 = vertices1[i];
        float proba = tmp_vector1[v1]/G->vertices[v1].total_weight;
        for(int j = 0; j < G->vertices[v1].degree; j++) {
	  int v2 = G->vertices[v1].edges[j].neighbor;
	  if(id[v2] == current_id)
  	    tmp_vector2[v2] += proba*G->vertices[v1].edges[j].weight;
  	  else {
	    tmp_vector2[v2] = proba*G->vertices[v1].edges[j].weight;
	    id[v2] = current_id;
	    vertices2[nb_vertices2++] = v2;
	  }
        }
      }
    }
    float* tmp = tmp_vector2;
    tmp_vector2 = tmp_vector1;
    tmp_vector1 = tmp;

    int* tmp2 = vertices2;
    vertices2 = vertices1;
    vertices1 = tmp2;

    nb_vertices1 = nb_vertices2;
  }

  if(nb_vertices1 > (G->nb_vertices/2)) {
    P = new float[G->nb_vertices];
    size = G->nb_vertices;
    vertices = 0;
    if(nb_vertices1 == G->nb_vertices) {
      for(int i = 0; i < G->nb_vertices; i++)
	P[i] = tmp_vector1[i]/sqrt(G->vertices[i].total_weight);
    }
    else {
      for(int i = 0; i < G->nb_vertices; i++)
	P[i] = 0.;
      for(int i = 0; i < nb_vertices1; i++)
	P[vertices1[i]] = tmp_vector1[vertices1[i]]/sqrt(G->vertices[vertices1[i]].total_weight);
    }
  }
  else {
    P = new float[nb_vertices1];
    size = nb_vertices1;
    vertices = new int[nb_vertices1];
    int j = 0;
    for(int i = 0; i < G->nb_vertices; i++) {
      if(id[i] == current_id) {
	P[j] = tmp_vector1[i]/sqrt(G->vertices[i].total_weight);
	vertices[j] = i;
	j++;
      }
    }
  }
  C->memory_used += memory();
}

Probabilities::Probabilities(int community1, int community2) {
  // The two following probability vectors must exist.
  // Do not call this function if it is not the case.
  Probabilities* P1 = C->communities[community1].P;
  Probabilities* P2 = C->communities[community2].P;
  
  float w1 = float(C->communities[community1].size)/float(C->communities[community1].size + C->communities[community2].size);
  float w2 = float(C->communities[community2].size)/float(C->communities[community1].size + C->communities[community2].size);


  if(P1->size == C->G->nb_vertices) {
    P = new float[C->G->nb_vertices];
    size = C->G->nb_vertices;
    vertices = 0;
    
    if(P2->size == C->G->nb_vertices) {	// two full vectors
      for(int i = 0; i < C->G->nb_vertices; i++)
	P[i] = P1->P[i]*w1 + P2->P[i]*w2;
    }
    else {  // P1 full vector, P2 partial vector
      int j = 0;
      for(int i = 0; i < P2->size; i++) {
	for(; j < P2->vertices[i]; j++)
	  P[j] = P1->P[j]*w1;
	P[j] = P1->P[j]*w1 + P2->P[i]*w2;
	j++;
      }
      for(; j < C->G->nb_vertices; j++)
	P[j] = P1->P[j]*w1;
    }
  }
  else {
    if(P2->size == C->G->nb_vertices) { // P1 partial vector, P2 full vector
      P = new float[C->G->nb_vertices];
      size = C->G->nb_vertices;
      vertices = 0;

      int j = 0;
      for(int i = 0; i < P1->size; i++) {
	for(; j < P1->vertices[i]; j++)
	  P[j] = P2->P[j]*w2;
	P[j] = P1->P[i]*w1 + P2->P[j]*w2;
	j++;
      }
      for(; j < C->G->nb_vertices; j++)
	P[j] = P2->P[j]*w2;
    }
    else {  // two partial vectors
      int i = 0;
      int j = 0;
      int nb_vertices1 = 0;
      while((i < P1->size) && (j < P2->size)) {
	if(P1->vertices[i] < P2->vertices[j]) {
	  tmp_vector1[P1->vertices[i]] = P1->P[i]*w1;
	  vertices1[nb_vertices1++] = P1->vertices[i];
	  i++;
	  continue;
	}
	if(P1->vertices[i] > P2->vertices[j]) {
	  tmp_vector1[P2->vertices[j]] = P2->P[j]*w2;
	  vertices1[nb_vertices1++] = P2->vertices[j];
	  j++;
	  continue;
	}
	tmp_vector1[P1->vertices[i]] = P1->P[i]*w1 + P2->P[j]*w2;
	vertices1[nb_vertices1++] = P1->vertices[i];
	i++;
	j++;
      }
      if(i == P1->size) {
	for(; j < P2->size; j++) {
	  tmp_vector1[P2->vertices[j]] = P2->P[j]*w2;
	  vertices1[nb_vertices1++] = P2->vertices[j];
	}
      }
      else {
	for(; i < P1->size; i++) {
	  tmp_vector1[P1->vertices[i]] = P1->P[i]*w1;
	  vertices1[nb_vertices1++] = P1->vertices[i];
	}
      }

      if(nb_vertices1 > (C->G->nb_vertices/2)) {
	P = new float[C->G->nb_vertices];
	size = C->G->nb_vertices;
	vertices = 0;
	for(int i = 0; i < C->G->nb_vertices; i++)
	  P[i] = 0.;
	for(int i = 0; i < nb_vertices1; i++)
	  P[vertices1[i]] = tmp_vector1[vertices1[i]];
      }
      else {
	P = new float[nb_vertices1];
	size = nb_vertices1;
	vertices = new int[nb_vertices1];
	for(int i = 0; i < nb_vertices1; i++) {
	  vertices[i] = vertices1[i];
	  P[i] = tmp_vector1[vertices1[i]];
	}
      }
    }
  }

  C->memory_used += memory();
}

double Probabilities::compute_distance(const Probabilities* P2) const {
  double r = 0.;
  if(vertices) {
    if(P2->vertices) {  // two partial vectors
      int i = 0;
      int j = 0;
      while((i < size) && (j < P2->size)) {
	if(vertices[i] < P2->vertices[j]) {
	  r += P[i]*P[i];
	  i++;
	  continue;
	}
	if(vertices[i] > P2->vertices[j]) {
	  r += P2->P[j]*P2->P[j];
	  j++;
	  continue;
	}
	r += (P[i] - P2->P[j])*(P[i] - P2->P[j]);
	i++;
	j++;
      }
      if(i == size) {
	for(; j < P2->size; j++)
	  r += P2->P[j]*P2->P[j];
      }
      else {
	for(; i < size; i++)
	  r += P[i]*P[i];
      }
    }
    else {  // P1 partial vector, P2 full vector 

      int i = 0;
      for(int j = 0; j < size; j++) {
	for(; i < vertices[j]; i++)
	  r += P2->P[i]*P2->P[i];
	r += (P[j] - P2->P[i])*(P[j] - P2->P[i]);
	i++;
      }
      for(; i < P2->size; i++)
	r += P2->P[i]*P2->P[i];      
    }
  }
  else {
    if(P2->vertices) {  // P1 full vector, P2 partial vector
      int i = 0;
      for(int j = 0; j < P2->size; j++) {
	for(; i < P2->vertices[j]; i++)
	  r += P[i]*P[i];
	r += (P[i] - P2->P[j])*(P[i] - P2->P[j]);
	i++;
      }
      for(; i < size; i++)
	r += P[i]*P[i];
    }
    else {  // two full vectors
      for(int i = 0; i < size; i++)
	r += (P[i] - P2->P[i])*(P[i] - P2->P[i]);
    }
  }
  return r;
}

long Probabilities::memory() {
  if(vertices)
    return (sizeof(Probabilities) + long(size)*(sizeof(float) + sizeof(int)));
  else
    return (sizeof(Probabilities) + long(size)*sizeof(float));
}

Community::Community() {
  P = 0;
  first_neighbor = 0;
  last_neighbor = 0;
  sub_community_of = -1;
  sub_communities[0] = -1;
  sub_communities[1] = -1;
  sigma = 0.;
  internal_weight = 0.;
  total_weight = 0.;
}

Community::~Community() {
  if(P) delete P;
}


Communities::Communities(Graph* graph, int random_walks_length, 
			 long m, igraph_matrix_t *pmerges,
			 igraph_vector_t *pmodularity) {
  max_memory = m;
  memory_used = 0;
  G = graph;
  merges=pmerges;
  mergeidx=0;
  modularity=pmodularity;
  
  Probabilities::C = this;
  Probabilities::length = random_walks_length;
  Probabilities::tmp_vector1 = new float[G->nb_vertices];
  Probabilities::tmp_vector2 = new float[G->nb_vertices];
  Probabilities::id = new int[G->nb_vertices];
  for(int i = 0; i < G->nb_vertices; i++) Probabilities::id[i] = 0;
  Probabilities::vertices1 = new int[G->nb_vertices];
  Probabilities::vertices2 = new int[G->nb_vertices];
  Probabilities::current_id = 0;

  
  members = new int[G->nb_vertices];  
  for(int i = 0; i < G->nb_vertices; i++)
    members[i] = -1;

  H = new Neighbor_heap(G->nb_edges);
  communities = new Community[2*G->nb_vertices];

// init the n single vertex communities

  if(max_memory != -1)
    min_delta_sigma = new Min_delta_sigma_heap(G->nb_vertices*2);
  else min_delta_sigma = 0;
  
  for(int i = 0; i < G->nb_vertices; i++) {
    communities[i].this_community = i;
    communities[i].first_member = i;
    communities[i].last_member = i;
    communities[i].size = 1;
    communities[i].sub_community_of = 0;
  }

  nb_communities = G->nb_vertices;
  nb_active_communities = G->nb_vertices;

  for(int i = 0; i < G->nb_vertices; i++)
    for(int j = 0; j < G->vertices[i].degree; j++)
      if (i < G->vertices[i].edges[j].neighbor) {
	communities[i].total_weight += G->vertices[i].edges[j].weight/2.;
	communities[G->vertices[i].edges[j].neighbor].total_weight += G->vertices[i].edges[j].weight/2.;
	Neighbor* N = new Neighbor;
	N->community1 = i;
	N->community2 = G->vertices[i].edges[j].neighbor;
	N->delta_sigma = -1./double(min(G->vertices[i].degree,  G->vertices[G->vertices[i].edges[j].neighbor].degree));
	N->weight = G->vertices[i].edges[j].weight;
	N->exact = false;
	add_neighbor(N);
      }

  if(max_memory != -1) {
    memory_used += min_delta_sigma->memory();
    memory_used += 2*long(G->nb_vertices)*sizeof(Community);
    memory_used += long(G->nb_vertices)*(2*sizeof(float) + 3*sizeof(int)); // the static data of Probabilities class
    memory_used += H->memory() + long(G->nb_edges)*sizeof(Neighbor);
    memory_used += G->memory();    
  }

/*   int c = 0; */
  Neighbor* N = H->get_first();  
  if (N == 0)
    return;   /* this can happen if there are no edges */
  while(!N->exact) {
    update_neighbor(N, compute_delta_sigma(N->community1, N->community2));
    N->exact = true;
    N = H->get_first();
    if(max_memory != -1) manage_memory();
    /* TODO: this could use igraph_progress */
/*     if(!silent) { */
/*       c++; */
/*       for(int k = (500*(c-1))/G->nb_edges + 1; k <= (500*c)/G->nb_edges; k++) { */
/* 	if(k % 50 == 1) {cerr.width(2); cerr << endl << k/ 5 << "% ";} */
/* 	cerr << "."; */
/*       } */
/*     } */
  }
  
}

Communities::~Communities() {
  delete[] members;
  delete[] communities;
  delete H;
  if(min_delta_sigma) delete min_delta_sigma;
  
  delete[] Probabilities::tmp_vector1;
  delete[] Probabilities::tmp_vector2;
  delete[] Probabilities::id;
  delete[] Probabilities::vertices1;
  delete[] Probabilities::vertices2;
}

float Community::min_delta_sigma() {
  float r = 1.;
  for(Neighbor* N = first_neighbor; N != 0;) {
    if(N->delta_sigma < r) r = N->delta_sigma;
    if(N->community1 == this_community)
      N = N->next_community1;
    else
      N = N->next_community2;
  }
  return r;
}


void Community::add_neighbor(Neighbor* N) { // add a new neighbor at the end of the list
  if (last_neighbor) {
    if(last_neighbor->community1 == this_community)
      last_neighbor->next_community1 = N;
    else
      last_neighbor->next_community2 = N;
    
    if(N->community1 == this_community)
      N->previous_community1 = last_neighbor;
    else
      N->previous_community2 = last_neighbor;
  }
  else {
    first_neighbor = N;
    if(N->community1 == this_community)
      N->previous_community1 = 0;
    else
      N->previous_community2 = 0;
  }
  last_neighbor = N;
}

void Community::remove_neighbor(Neighbor* N) {	// remove a neighbor from the list
  if (N->community1 == this_community) {
    if(N->next_community1) {
//      if (N->next_community1->community1 == this_community)
	N->next_community1->previous_community1 = N->previous_community1;
//      else 
//	N->next_community1->previous_community2 = N->previous_community1;
    }
    else last_neighbor = N->previous_community1;
    if(N->previous_community1) {
      if (N->previous_community1->community1 == this_community)
	N->previous_community1->next_community1 = N->next_community1;
      else 
	N->previous_community1->next_community2 = N->next_community1;
    }
    else first_neighbor = N->next_community1;
  }
  else {
    if(N->next_community2) {
      if (N->next_community2->community1 == this_community)
	N->next_community2->previous_community1 = N->previous_community2;
      else 
	N->next_community2->previous_community2 = N->previous_community2;
    }
    else last_neighbor = N->previous_community2;
    if(N->previous_community2) {
//      if (N->previous_community2->community1 == this_community)
//	N->previous_community2->next_community1 = N->next_community2;
//      else 
	N->previous_community2->next_community2 = N->next_community2;
    }
    else first_neighbor = N->next_community2;
  }
}

void Communities::remove_neighbor(Neighbor* N) {
  communities[N->community1].remove_neighbor(N);
  communities[N->community2].remove_neighbor(N);
  H->remove(N);

  if(max_memory !=-1) {
    if(N->delta_sigma == min_delta_sigma->delta_sigma[N->community1]) {
      min_delta_sigma->delta_sigma[N->community1] = communities[N->community1].min_delta_sigma();
      if(communities[N->community1].P) min_delta_sigma->update(N->community1);
    }

    if(N->delta_sigma == min_delta_sigma->delta_sigma[N->community2]) {
      min_delta_sigma->delta_sigma[N->community2] = communities[N->community2].min_delta_sigma();
      if(communities[N->community2].P) min_delta_sigma->update(N->community2);
    }
  }
}

void Communities::add_neighbor(Neighbor* N) {
  communities[N->community1].add_neighbor(N);
  communities[N->community2].add_neighbor(N);
  H->add(N);

  if(max_memory !=-1) {
    if(N->delta_sigma < min_delta_sigma->delta_sigma[N->community1]) {
      min_delta_sigma->delta_sigma[N->community1] = N->delta_sigma;
      if(communities[N->community1].P) min_delta_sigma->update(N->community1);
    }

    if(N->delta_sigma < min_delta_sigma->delta_sigma[N->community2]) {
      min_delta_sigma->delta_sigma[N->community2] = N->delta_sigma;
      if(communities[N->community2].P) min_delta_sigma->update(N->community2);
    }
  }
}

void Communities::update_neighbor(Neighbor* N, float new_delta_sigma) {
  if(max_memory !=-1) {
    if(new_delta_sigma < min_delta_sigma->delta_sigma[N->community1]) {
      min_delta_sigma->delta_sigma[N->community1] = new_delta_sigma;
      if(communities[N->community1].P) min_delta_sigma->update(N->community1);
    }
   
    if(new_delta_sigma < min_delta_sigma->delta_sigma[N->community2]) {
      min_delta_sigma->delta_sigma[N->community2] = new_delta_sigma;
      if(communities[N->community2].P) min_delta_sigma->update(N->community2);
    }

    float old_delta_sigma = N->delta_sigma;
    N->delta_sigma = new_delta_sigma;
    H->update(N);

    if(old_delta_sigma == min_delta_sigma->delta_sigma[N->community1]) {
      min_delta_sigma->delta_sigma[N->community1] = communities[N->community1].min_delta_sigma();
      if(communities[N->community1].P) min_delta_sigma->update(N->community1);
    }

    if(old_delta_sigma == min_delta_sigma->delta_sigma[N->community2]) {
      min_delta_sigma->delta_sigma[N->community2] = communities[N->community2].min_delta_sigma();
      if(communities[N->community2].P) min_delta_sigma->update(N->community2);
    }
  }
  else {
    N->delta_sigma = new_delta_sigma;
    H->update(N);
  }
}

void Communities::manage_memory() {
  while((memory_used > max_memory) && !min_delta_sigma->is_empty()) {
    int c = min_delta_sigma->get_max_community();
    delete communities[c].P;
    communities[c].P = 0;
    min_delta_sigma->remove_community(c);
  }  
}



void Communities::merge_communities(Neighbor* merge_N) {
  int c1 = merge_N->community1;
  int c2 = merge_N->community2;
  
  communities[nb_communities].first_member = communities[c1].first_member;	// merge the 
  communities[nb_communities].last_member = communities[c2].last_member;	// two lists   
  members[communities[c1].last_member] = communities[c2].first_member;		// of members

  communities[nb_communities].size = communities[c1].size + communities[c2].size;
  communities[nb_communities].this_community = nb_communities;
  communities[nb_communities].sub_community_of = 0;
  communities[nb_communities].sub_communities[0] = c1;
  communities[nb_communities].sub_communities[1] = c2;
  communities[nb_communities].total_weight = communities[c1].total_weight + communities[c2].total_weight;
  communities[nb_communities].internal_weight = communities[c1].internal_weight + communities[c2].internal_weight + merge_N->weight;
  communities[nb_communities].sigma = communities[c1].sigma + communities[c2].sigma + merge_N->delta_sigma;
  
  communities[c1].sub_community_of = nb_communities;
  communities[c2].sub_community_of = nb_communities;

// update the new probability vector...
  
  if(communities[c1].P && communities[c2].P) communities[nb_communities].P = new Probabilities(c1, c2);

  if(communities[c1].P) {
    delete communities[c1].P; 
    communities[c1].P = 0;
    if(max_memory != -1) min_delta_sigma->remove_community(c1);
  }
  if(communities[c2].P) {
    delete communities[c2].P;
    communities[c2].P = 0;
    if(max_memory != -1) min_delta_sigma->remove_community(c2);
  }

  if(max_memory != -1) {
    min_delta_sigma->delta_sigma[c1] = -1.;		    // to avoid to update the min_delta_sigma for these communities
    min_delta_sigma->delta_sigma[c2] = -1.;		    // 
    min_delta_sigma->delta_sigma[nb_communities] = -1.;
  }
  
// update the new neighbors
// by enumerating all the neighbors of c1 and c2

  Neighbor* N1 = communities[c1].first_neighbor;
  Neighbor* N2 = communities[c2].first_neighbor;

  while(N1 && N2) { 
    int neighbor_community1;
    int neighbor_community2;
    
    if (N1->community1 == c1) neighbor_community1 = N1->community2;
    else neighbor_community1 = N1->community1;
    if (N2->community1 == c2) neighbor_community2 = N2->community2;
    else neighbor_community2 = N2->community1;

    if (neighbor_community1 < neighbor_community2) {
      Neighbor* tmp = N1;
      if (N1->community1 == c1) N1 = N1->next_community1;
      else N1 = N1->next_community2;
      remove_neighbor(tmp);
      Neighbor* N = new Neighbor;
      N->weight = tmp->weight;
      N->community1 = neighbor_community1;
      N->community2 = nb_communities;
      N->delta_sigma = (double(communities[c1].size+communities[neighbor_community1].size)*tmp->delta_sigma + double(communities[c2].size)*merge_N->delta_sigma)/(double(communities[c1].size+communities[c2].size+communities[neighbor_community1].size));//compute_delta_sigma(neighbor_community1, nb_communities);
      N->exact = false;
      delete tmp;
      add_neighbor(N); 
    }
    
    if (neighbor_community2 < neighbor_community1) {
      Neighbor* tmp = N2;
      if (N2->community1 == c2) N2 = N2->next_community1;
      else N2 = N2->next_community2;
      remove_neighbor(tmp);
      Neighbor* N = new Neighbor;
      N->weight = tmp->weight;
      N->community1 = neighbor_community2;
      N->community2 = nb_communities;
      N->delta_sigma = (double(communities[c1].size)*merge_N->delta_sigma + double(communities[c2].size+communities[neighbor_community2].size)*tmp->delta_sigma)/(double(communities[c1].size+communities[c2].size+communities[neighbor_community2].size));//compute_delta_sigma(neighbor_community2, nb_communities);
      N->exact = false;
      delete tmp;
      add_neighbor(N); 
    }
    
    if (neighbor_community1 == neighbor_community2) {
      Neighbor* tmp1 = N1;
      Neighbor* tmp2 = N2;
      bool exact = N1->exact && N2->exact;
      if (N1->community1 == c1) N1 = N1->next_community1;
      else N1 = N1->next_community2;
      if (N2->community1 == c2) N2 = N2->next_community1;
      else N2 = N2->next_community2;
      remove_neighbor(tmp1);
      remove_neighbor(tmp2);
      Neighbor* N = new Neighbor;
      N->weight = tmp1->weight + tmp2->weight;
      N->community1 = neighbor_community1;
      N->community2 = nb_communities;
      N->delta_sigma = (double(communities[c1].size+communities[neighbor_community1].size)*tmp1->delta_sigma + double(communities[c2].size+communities[neighbor_community1].size)*tmp2->delta_sigma - double(communities[neighbor_community1].size)*merge_N->delta_sigma)/(double(communities[c1].size+communities[c2].size+communities[neighbor_community1].size));
      N->exact = exact;
      delete tmp1;
      delete tmp2;
      add_neighbor(N);
    }
  }

  
  if(!N1) {
    while(N2) {
//      double delta_sigma2 = N2->delta_sigma;
      int neighbor_community;
      if (N2->community1 == c2) neighbor_community = N2->community2;
      else neighbor_community = N2->community1;
      Neighbor* tmp = N2;
      if (N2->community1 == c2) N2 = N2->next_community1;
      else N2 = N2->next_community2;
      remove_neighbor(tmp);
      Neighbor* N = new Neighbor;
      N->weight = tmp->weight;
      N->community1 = neighbor_community;
      N->community2 = nb_communities;
      N->delta_sigma = (double(communities[c1].size)*merge_N->delta_sigma + double(communities[c2].size+communities[neighbor_community].size)*tmp->delta_sigma)/(double(communities[c1].size+communities[c2].size+communities[neighbor_community].size));//compute_delta_sigma(neighbor_community, nb_communities);
      N->exact = false;
      delete tmp;
      add_neighbor(N);
    }
  }
  if(!N2) {
    while(N1) {
//      double delta_sigma1 = N1->delta_sigma;
      int neighbor_community;
      if (N1->community1 == c1) neighbor_community = N1->community2;
      else neighbor_community = N1->community1;
      Neighbor* tmp = N1;
      if (N1->community1 == c1) N1 = N1->next_community1;
      else N1 = N1->next_community2;
      remove_neighbor(tmp);
      Neighbor* N = new Neighbor;
      N->weight = tmp->weight;
      N->community1 = neighbor_community;
      N->community2 = nb_communities;
      N->delta_sigma = (double(communities[c1].size+communities[neighbor_community].size)*tmp->delta_sigma + double(communities[c2].size)*merge_N->delta_sigma)/(double(communities[c1].size+communities[c2].size+communities[neighbor_community].size));//compute_delta_sigma(neighbor_community, nb_communities);
      N->exact = false;
      delete tmp;
      add_neighbor(N);
    }
  }

  if(max_memory != -1) {
    min_delta_sigma->delta_sigma[nb_communities] = communities[nb_communities].min_delta_sigma();
    min_delta_sigma->update(nb_communities);
  } 

  nb_communities++;
  nb_active_communities--;
}

double Communities::merge_nearest_communities() {
  Neighbor* N = H->get_first();  
  while(!N->exact) {
    update_neighbor(N, compute_delta_sigma(N->community1, N->community2));
    N->exact = true;
    N = H->get_first();
    if(max_memory != -1) manage_memory();
  }

  double d = N->delta_sigma;
  remove_neighbor(N);

  merge_communities(N);
  if(max_memory != -1) manage_memory();
  
  if (merges) {
    MATRIX(*merges, mergeidx, 0)=N->community1;
    MATRIX(*merges, mergeidx, 1)=N->community2;
    mergeidx++;
  }

  if (modularity) {
    float Q = 0.;
    for(int i = 0; i < nb_communities; i++) {
      if(communities[i].sub_community_of == 0) {
	Q += (communities[i].internal_weight - communities[i].total_weight*communities[i].total_weight/G->total_weight)/G->total_weight;
      }
    }
    VECTOR(*modularity)[mergeidx]=Q;
  }

  delete N;

  /* This could use igraph_progress */
/*   if(!silent) { */
/*     for(int k = (500*(G->nb_vertices - nb_active_communities - 1))/(G->nb_vertices-1) + 1; k <= (500*(G->nb_vertices - nb_active_communities))/(G->nb_vertices-1); k++) { */
/*       if(k % 50 == 1) {cerr.width(2); cerr << endl << k/ 5 << "% ";} */
/*       cerr << "."; */
/*     } */
/*   } */
  return d;
}

double Communities::compute_delta_sigma(int community1, int community2) {
  if(!communities[community1].P) {
    communities[community1].P = new Probabilities(community1);
    if(max_memory != -1) min_delta_sigma->update(community1);
  }
  if(!communities[community2].P) {
    communities[community2].P = new Probabilities(community2);
    if(max_memory != -1) min_delta_sigma->update(community2);
  }
  
  return communities[community1].P->compute_distance(communities[community2].P)*double(communities[community1].size)*double(communities[community2].size)/double(communities[community1].size + communities[community2].size);
}

} }    /* end of namespaces */