/src/igraph/src/community/walktrap/walktrap_heap.cpp
Line | Count | Source |
1 | | /* |
2 | | igraph library. |
3 | | Copyright (C) 2007-2012 Gabor Csardi <csardi.gabor@gmail.com> |
4 | | 334 Harvard street, Cambridge, MA 02139 USA |
5 | | |
6 | | This program is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 2 of the License, or |
9 | | (at your option) any later version. |
10 | | |
11 | | This program is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with this program; if not, write to the Free Software |
18 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | | 02110-1301 USA |
20 | | |
21 | | */ |
22 | | |
23 | | /* The original version of this file was written by Pascal Pons |
24 | | The original copyright notice follows here. The FSF address was |
25 | | fixed by Tamas Nepusz */ |
26 | | |
27 | | // File: heap.cpp |
28 | | //----------------------------------------------------------------------------- |
29 | | // Walktrap v0.2 -- Finds community structure of networks using random walks |
30 | | // Copyright (C) 2004-2005 Pascal Pons |
31 | | // |
32 | | // This program is free software; you can redistribute it and/or modify |
33 | | // it under the terms of the GNU General Public License as published by |
34 | | // the Free Software Foundation; either version 2 of the License, or |
35 | | // (at your option) any later version. |
36 | | // |
37 | | // This program is distributed in the hope that it will be useful, |
38 | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
39 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
40 | | // GNU General Public License for more details. |
41 | | // |
42 | | // You should have received a copy of the GNU General Public License |
43 | | // along with this program; if not, write to the Free Software |
44 | | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
45 | | // 02110-1301 USA |
46 | | //----------------------------------------------------------------------------- |
47 | | // Author : Pascal Pons |
48 | | // Email : pascal.pons@gmail.com |
49 | | // Web page : http://www-rp.lip6.fr/~latapy/PP/walktrap.html |
50 | | // Location : Paris, France |
51 | | // Time : June 2005 |
52 | | //----------------------------------------------------------------------------- |
53 | | // see readme.txt for more details |
54 | | |
55 | | #include "walktrap_heap.h" |
56 | | |
57 | | using namespace igraph::walktrap; |
58 | | |
59 | 1.60M | void Neighbor_heap::move_up(int index) { |
60 | 2.18M | while (H[index / 2]->delta_sigma > H[index]->delta_sigma) { |
61 | 585k | Neighbor* tmp = H[index / 2]; |
62 | 585k | H[index]->heap_index = index / 2; |
63 | 585k | H[index / 2] = H[index]; |
64 | 585k | tmp->heap_index = index; |
65 | 585k | H[index] = tmp; |
66 | 585k | index = index / 2; |
67 | 585k | } |
68 | 1.60M | } |
69 | | |
70 | 979k | void Neighbor_heap::move_down(int index) { |
71 | 2.91M | while (true) { |
72 | 2.91M | int min = index; |
73 | 2.91M | if ((2 * index < size) && (H[2 * index]->delta_sigma < H[min]->delta_sigma)) { |
74 | 1.36M | min = 2 * index; |
75 | 1.36M | } |
76 | 2.91M | if (2 * index + 1 < size && H[2 * index + 1]->delta_sigma < H[min]->delta_sigma) { |
77 | 1.08M | min = 2 * index + 1; |
78 | 1.08M | } |
79 | 2.91M | if (min != index) { |
80 | 1.94M | Neighbor* tmp = H[min]; |
81 | 1.94M | H[index]->heap_index = min; |
82 | 1.94M | H[min] = H[index]; |
83 | 1.94M | tmp->heap_index = index; |
84 | 1.94M | H[index] = tmp; |
85 | 1.94M | index = min; |
86 | 1.94M | } else { |
87 | 979k | break; |
88 | 979k | } |
89 | 2.91M | } |
90 | 979k | } |
91 | | |
92 | 450k | Neighbor* Neighbor_heap::get_first() { |
93 | 450k | if (size == 0) { |
94 | 175 | return nullptr; |
95 | 449k | } else { |
96 | 449k | return H[0]; |
97 | 449k | } |
98 | 450k | } |
99 | | |
100 | 622k | void Neighbor_heap::remove(Neighbor* N) { |
101 | 622k | if (N->heap_index == -1 || size == 0) { |
102 | 0 | return; |
103 | 0 | } |
104 | 622k | Neighbor* last_N = H[--size]; |
105 | 622k | H[N->heap_index] = last_N; |
106 | 622k | last_N->heap_index = N->heap_index; |
107 | 622k | move_up(last_N->heap_index); |
108 | 622k | move_down(last_N->heap_index); |
109 | 622k | N->heap_index = -1; |
110 | 622k | } |
111 | | |
112 | 622k | void Neighbor_heap::add(Neighbor* N) { |
113 | 622k | if (size >= max_size) { |
114 | 0 | return; |
115 | 0 | } |
116 | 622k | N->heap_index = size++; |
117 | 622k | H[N->heap_index] = N; |
118 | 622k | move_up(N->heap_index); |
119 | 622k | } |
120 | | |
121 | 356k | void Neighbor_heap::update(Neighbor* N) { |
122 | 356k | if (N->heap_index == -1) { |
123 | 0 | return; |
124 | 0 | } |
125 | 356k | move_up(N->heap_index); |
126 | 356k | move_down(N->heap_index); |
127 | 356k | } |
128 | | |
129 | 6.67k | Neighbor_heap::Neighbor_heap(int max_s) { |
130 | 6.67k | max_size = max_s; |
131 | 6.67k | size = 0; |
132 | 6.67k | H = new Neighbor*[max_s]; |
133 | 6.67k | } |
134 | | |
135 | 6.67k | Neighbor_heap::~Neighbor_heap() { |
136 | 6.67k | delete[] H; |
137 | 6.67k | } |
138 | | |
139 | 93.7k | bool Neighbor_heap::is_empty() const { |
140 | 93.7k | return (size == 0); |
141 | 93.7k | } |