4
Copyright (C) 2007 Gabor Csardi <csardi@rmki.kfki.hu>
5
MTA RMKI, Konkoly-Thege Miklos st. 29-33, Budapest 1121, Hungary
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
/* The original version of this file was written by Pascal Pons
25
The original copyright notice follows here. The FSF address was
26
fixed by Tamas Nepusz */
29
//-----------------------------------------------------------------------------
30
// Walktrap v0.2 -- Finds community structure of networks using random walks
31
// Copyright (C) 2004-2005 Pascal Pons
33
// This program is free software; you can redistribute it and/or modify
34
// it under the terms of the GNU General Public License as published by
35
// the Free Software Foundation; either version 2 of the License, or
36
// (at your option) any later version.
38
// This program is distributed in the hope that it will be useful,
39
// but WITHOUT ANY WARRANTY; without even the implied warranty of
40
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41
// GNU General Public License for more details.
43
// You should have received a copy of the GNU General Public License
44
// along with this program; if not, write to the Free Software
45
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
47
//-----------------------------------------------------------------------------
48
// Author : Pascal Pons
49
// Email : pons@liafa.jussieu.fr
50
// Web page : http://www.liafa.jussieu.fr/~pons/
51
// Location : Paris, France
53
//-----------------------------------------------------------------------------
54
// see readme.txt for more details
56
#include "walktrap_heap.h"
63
void Neighbor_heap::move_up(int index) {
64
while(H[index/2]->delta_sigma > H[index]->delta_sigma) {
65
Neighbor* tmp = H[index/2];
66
H[index]->heap_index = index/2;
67
H[index/2] = H[index];
68
tmp->heap_index = index;
74
void Neighbor_heap::move_down(int index) {
77
if((2*index < size) && (H[2*index]->delta_sigma < H[min]->delta_sigma))
79
if(2*index+1 < size && H[2*index+1]->delta_sigma < H[min]->delta_sigma)
82
Neighbor* tmp = H[min];
83
H[index]->heap_index = min;
85
tmp->heap_index = index;
93
Neighbor* Neighbor_heap::get_first() {
94
if(size == 0) return 0;
98
void Neighbor_heap::remove(Neighbor* N) {
99
if(N->heap_index == -1 || size == 0) return;
100
Neighbor* last_N = H[--size];
101
H[N->heap_index] = last_N;
102
last_N->heap_index = N->heap_index;
103
move_up(last_N->heap_index);
104
move_down(last_N->heap_index);
108
void Neighbor_heap::add(Neighbor* N) {
109
if(size >= max_size) return;
110
N->heap_index = size++;
111
H[N->heap_index] = N;
112
move_up(N->heap_index);
115
void Neighbor_heap::update(Neighbor* N) {
116
if(N->heap_index == -1) return;
117
move_up(N->heap_index);
118
move_down(N->heap_index);
121
long Neighbor_heap::memory() {
122
return (sizeof(Neighbor_heap) + long(max_size)*sizeof(Neighbor*));
125
Neighbor_heap::Neighbor_heap(int max_s) {
128
H = new Neighbor*[max_s];
131
Neighbor_heap::~Neighbor_heap() {
135
bool Neighbor_heap::is_empty() {
141
//#################################################################
143
void Min_delta_sigma_heap::move_up(int index) {
144
while(delta_sigma[H[index/2]] < delta_sigma[H[index]]) {
145
int tmp = H[index/2];
146
I[H[index]] = index/2;
147
H[index/2] = H[index];
154
void Min_delta_sigma_heap::move_down(int index) {
157
if(2*index < size && delta_sigma[H[2*index]] > delta_sigma[H[max]])
159
if(2*index+1 < size && delta_sigma[H[2*index+1]] > delta_sigma[H[max]])
173
int Min_delta_sigma_heap::get_max_community() {
174
if(size == 0) return -1;
178
void Min_delta_sigma_heap::remove_community(int community) {
179
if(I[community] == -1 || size == 0) return;
180
int last_community = H[--size];
181
H[I[community]] = last_community;
182
I[last_community] = I[community];
183
move_up(I[last_community]);
184
move_down(I[last_community]);
188
void Min_delta_sigma_heap::update(int community) {
189
if(community < 0 || community >= max_size) return;
190
if(I[community] == -1) {
191
I[community] = size++;
192
H[I[community]] = community;
194
move_up(I[community]);
195
move_down(I[community]);
198
long Min_delta_sigma_heap::memory() {
199
return (sizeof(Min_delta_sigma_heap) + long(max_size)*(2*sizeof(int) + sizeof(float)));
202
Min_delta_sigma_heap::Min_delta_sigma_heap(int max_s) {
207
delta_sigma = new float[max_s];
208
for(int i = 0; i < max_size; i++) {
214
Min_delta_sigma_heap::~Min_delta_sigma_heap() {
217
delete[] delta_sigma;
220
bool Min_delta_sigma_heap::is_empty() {