-
-
Notifications
You must be signed in to change notification settings - Fork 396
/
spectral.c
431 lines (374 loc) · 16.1 KB
/
spectral.c
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
/* -*- mode: C -*- */
/* vim:set ts=4 sw=4 sts=4 et: */
/*
IGraph library.
Copyright (C) 2006-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
*/
#include "igraph_structural.h"
#include "igraph_interface.h"
#include "math/safe_intop.h"
#include <math.h>
static igraph_error_t igraph_i_laplacian_validate_weights(
const igraph_t* graph, const igraph_vector_t* weights
) {
igraph_integer_t no_of_edges;
if (weights == NULL) {
return IGRAPH_SUCCESS;
}
no_of_edges = igraph_ecount(graph);
if (igraph_vector_size(weights) != no_of_edges) {
IGRAPH_ERROR("Invalid weight vector length.", IGRAPH_EINVAL);
}
if (no_of_edges > 0) {
igraph_real_t minweight = igraph_vector_min(weights);
if (minweight < 0) {
IGRAPH_ERROR("Weight vector must be non-negative.", IGRAPH_EINVAL);
} else if (isnan(minweight)) {
IGRAPH_ERROR("Weight vector must not contain NaN values.", IGRAPH_EINVAL);
}
}
return IGRAPH_SUCCESS;
}
/**
* \function igraph_get_laplacian
* \brief Returns the Laplacian matrix of a graph.
*
* The Laplacian matrix \c L of a graph is defined as
* <code>L_ij = - A_ij</code> when <code>i != j</code> and
* <code>L_ii = d_i - A_ii</code>. Here \c A denotes the (possibly weighted)
* adjacency matrix and <code>d_i</code> is the degree (or strength, if weighted)
* of vertex \c i. In directed graphs, the \p mode parameter controls whether to use
* out- or in-degrees. Correspondingly, the rows or columns will sum to zero.
* In undirected graphs, <code>A_ii</code> is taken to be \em twice the number
* (or total weight) of self-loops, ensuring that <code>d_i = \sum_j A_ij</code>.
* Thus, the Laplacian of an undirected graph is the same as the Laplacian
* of a directed one obtained by replacing each undirected edge with two reciprocal
* directed ones.
*
* </para><para>
* More compactly, <code>L = D - A</code> where the \c D is a diagonal matrix
* containing the degrees. The Laplacian matrix can also be normalized, with several
* conventional normalization methods. See \ref igraph_laplacian_normalization_t for
* the methods available in igraph.
*
* </para><para>
* The first version of this function was written by Vincent Matossian.
*
* \param graph Pointer to the graph to convert.
* \param res Pointer to an initialized matrix object, the result is
* stored here. It will be resized if needed.
* \param mode Controls whether to use out- or in-degrees in directed graphs.
* If set to \c IGRAPH_ALL, edge directions will be ignored.
* \param normalization The normalization method to use when calculating the
* Laplacian matrix. See \ref igraph_laplacian_normalization_t for
* possible values.
* \param weights An optional vector containing non-negative edge weights,
* to calculate the weighted Laplacian matrix. Set it to a null pointer to
* calculate the unweighted Laplacian.
* \return Error code.
*
* Time complexity: O(|V|^2), |V| is the number of vertices in the graph.
*
* \example examples/simple/igraph_get_laplacian.c
*/
igraph_error_t igraph_get_laplacian(
const igraph_t *graph, igraph_matrix_t *res, igraph_neimode_t mode,
igraph_laplacian_normalization_t normalization,
const igraph_vector_t *weights
) {
igraph_integer_t no_of_nodes = igraph_vcount(graph);
igraph_integer_t no_of_edges = igraph_ecount(graph);
igraph_bool_t directed = igraph_is_directed(graph);
igraph_vector_t degree;
igraph_integer_t i;
IGRAPH_ASSERT(res != NULL);
IGRAPH_CHECK(igraph_i_laplacian_validate_weights(graph, weights));
IGRAPH_CHECK(igraph_matrix_resize(res, no_of_nodes, no_of_nodes));
igraph_matrix_null(res);
IGRAPH_VECTOR_INIT_FINALLY(°ree, no_of_nodes);
IGRAPH_CHECK(igraph_strength(graph, °ree, igraph_vss_all(), mode, IGRAPH_LOOPS, weights));
/* Value of 'mode' is validated in igraph_strength() call above. */
if (! directed) {
mode = IGRAPH_ALL;
} else if (mode == IGRAPH_ALL) {
directed = 0;
}
for (i = 0; i < no_of_nodes; i++) {
switch (normalization) {
case IGRAPH_LAPLACIAN_UNNORMALIZED:
MATRIX(*res, i, i) = VECTOR(degree)[i];
break;
case IGRAPH_LAPLACIAN_SYMMETRIC:
if (VECTOR(degree)[i] > 0) {
MATRIX(*res, i, i) = 1;
VECTOR(degree)[i] = 1.0 / sqrt(VECTOR(degree)[i]);
}
break;
case IGRAPH_LAPLACIAN_LEFT:
case IGRAPH_LAPLACIAN_RIGHT:
if (VECTOR(degree)[i] > 0) {
MATRIX(*res, i, i) = 1;
VECTOR(degree)[i] = 1.0 / VECTOR(degree)[i];
}
break;
default:
IGRAPH_ERROR("Invalid Laplacian normalization method.", IGRAPH_EINVAL);
}
}
for (i = 0; i < no_of_edges; i++) {
igraph_integer_t from = IGRAPH_FROM(graph, i);
igraph_integer_t to = IGRAPH_TO(graph, i);
igraph_real_t weight = weights ? VECTOR(*weights)[i] : 1.0;
igraph_real_t norm;
switch (normalization) {
case IGRAPH_LAPLACIAN_UNNORMALIZED:
MATRIX(*res, from, to) -= weight;
if (!directed) {
MATRIX(*res, to, from) -= weight;
}
break;
case IGRAPH_LAPLACIAN_SYMMETRIC:
norm = VECTOR(degree)[from] * VECTOR(degree)[to];
if (norm == 0 && weight != 0) {
IGRAPH_ERRORF(
"Found non-isolated vertex with zero %s-%s, "
"cannot perform symmetric normalization of Laplacian with '%s' mode.",
IGRAPH_EINVAL,
mode == IGRAPH_OUT ? "out" : "in", weights ? "strength" : "degree", mode == IGRAPH_OUT ? "out" : "in");
}
weight *= norm;
MATRIX(*res, from, to) -= weight;
if (!directed) {
MATRIX(*res, to, from) -= weight;
}
break;
case IGRAPH_LAPLACIAN_LEFT:
norm = VECTOR(degree)[from];
if (norm == 0 && weight != 0) {
IGRAPH_ERRORF(
"Found non-isolated vertex with zero in-%s, "
"cannot perform left stochastic normalization of Laplacian with 'in' mode.",
IGRAPH_EINVAL,
weights ? "strength" : "degree");
}
MATRIX(*res, from, to) -= weight * norm;
if (!directed) {
/* no failure possible in undirected case, as zero degrees occur only for isolated vertices */
MATRIX(*res, to, from) -= weight * VECTOR(degree)[to];
}
break;
case IGRAPH_LAPLACIAN_RIGHT:
norm = VECTOR(degree)[to];
if (norm == 0 && weight != 0) {
IGRAPH_ERRORF(
"Found non-isolated vertex with zero out-%s, "
"cannot perform right stochastic normalization of Laplacian with 'out' mode.",
IGRAPH_EINVAL,
weights ? "strength" : "degree");
}
MATRIX(*res, from, to) -= weight * norm;
if (!directed) {
/* no failure possible in undirected case, as zero degrees occur only for isolated vertices */
MATRIX(*res, to, from) -= weight * VECTOR(degree)[from];
}
break;
}
}
igraph_vector_destroy(°ree);
IGRAPH_FINALLY_CLEAN(1);
return IGRAPH_SUCCESS;
}
/**
* \function igraph_get_laplacian_sparse
* \brief Returns the Laplacian of a graph in a sparse matrix format.
*
* See \ref igraph_get_laplacian() for the definition of the Laplacian matrix.
*
* </para><para>
* The first version of this function was written by Vincent Matossian.
*
* \param graph Pointer to the graph to convert.
* \param sparseres Pointer to an initialized sparse matrix object, the
* result is stored here.
* \param mode Controls whether to use out- or in-degrees in directed graphs.
* If set to \c IGRAPH_ALL, edge directions will be ignored.
* \param normalization The normalization method to use when calculating the
* Laplacian matrix. See \ref igraph_laplacian_normalization_t for
* possible values.
* \param weights An optional vector containing non-negative edge weights,
* to calculate the weighted Laplacian matrix. Set it to a null pointer to
* calculate the unweighted Laplacian.
* \return Error code.
*
* Time complexity: O(|E|), |E| is the number of edges in the graph.
*
* \example examples/simple/igraph_get_laplacian_sparse.c
*/
igraph_error_t igraph_get_laplacian_sparse(
const igraph_t *graph, igraph_sparsemat_t *sparseres, igraph_neimode_t mode,
igraph_laplacian_normalization_t normalization,
const igraph_vector_t *weights
) {
igraph_integer_t no_of_nodes = igraph_vcount(graph);
igraph_integer_t no_of_edges = igraph_ecount(graph);
igraph_bool_t directed = igraph_is_directed(graph);
igraph_vector_t degree;
igraph_integer_t i;
igraph_integer_t nz;
if (directed) {
IGRAPH_SAFE_ADD(no_of_edges, no_of_nodes, &nz);
} else {
IGRAPH_SAFE_ADD(no_of_edges * 2, no_of_nodes, &nz);
}
IGRAPH_ASSERT(sparseres != NULL);
IGRAPH_CHECK(igraph_i_laplacian_validate_weights(graph, weights));
IGRAPH_CHECK(igraph_sparsemat_resize(sparseres, no_of_nodes, no_of_nodes, nz));
IGRAPH_VECTOR_INIT_FINALLY(°ree, no_of_nodes);
IGRAPH_CHECK(igraph_strength(graph, °ree, igraph_vss_all(), mode, IGRAPH_LOOPS, weights));
for (i = 0; i < no_of_nodes; i++) {
switch (normalization) {
case IGRAPH_LAPLACIAN_UNNORMALIZED:
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, i, i, VECTOR(degree)[i]));
break;
case IGRAPH_LAPLACIAN_SYMMETRIC:
if (VECTOR(degree)[i] > 0) {
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, i, i, 1));
VECTOR(degree)[i] = 1.0 / sqrt(VECTOR(degree)[i]);
}
break;
case IGRAPH_LAPLACIAN_LEFT:
case IGRAPH_LAPLACIAN_RIGHT:
if (VECTOR(degree)[i] > 0) {
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, i, i, 1));
VECTOR(degree)[i] = 1.0 / VECTOR(degree)[i];
}
break;
default:
IGRAPH_ERROR("Invalid Laplacian normalization method.", IGRAPH_EINVAL);
}
}
for (i = 0; i < no_of_edges; i++) {
igraph_integer_t from = IGRAPH_FROM(graph, i);
igraph_integer_t to = IGRAPH_TO(graph, i);
igraph_real_t weight = weights ? VECTOR(*weights)[i] : 1.0;
igraph_real_t norm;
switch (normalization) {
case IGRAPH_LAPLACIAN_UNNORMALIZED:
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, from, to, -weight));
if (!directed) {
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, to, from, -weight));
}
break;
case IGRAPH_LAPLACIAN_SYMMETRIC:
norm = VECTOR(degree)[from] * VECTOR(degree)[to];
if (norm == 0 && weight != 0) {
IGRAPH_ERRORF(
"Found non-isolated vertex with zero %s-%s, "
"cannot perform symmetric normalization of Laplacian with '%s' mode.",
IGRAPH_EINVAL,
mode == IGRAPH_OUT ? "out" : "in", weights ? "strength" : "degree", mode == IGRAPH_OUT ? "out" : "in");
}
weight *= norm;
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, from, to, -weight));
if (!directed) {
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, to, from, -weight));
}
break;
case IGRAPH_LAPLACIAN_LEFT:
norm = VECTOR(degree)[from];
if (norm == 0 && weight != 0) {
IGRAPH_ERRORF(
"Found non-isolated vertex with zero in-%s, "
"cannot perform left stochastic normalization of Laplacian with 'in' mode.",
IGRAPH_EINVAL,
weights ? "strength" : "degree");
}
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, from, to, -weight * norm));
if (!directed) {
/* no failure possible in undirected case, as zero degrees occur only for isolated vertices */
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, to, from, -weight * VECTOR(degree)[to]));
}
break;
case IGRAPH_LAPLACIAN_RIGHT:
norm = VECTOR(degree)[to];
if (norm == 0 && weight != 0) {
IGRAPH_ERRORF(
"Found non-isolated vertex with zero out-%s, "
"cannot perform right stochastic normalization of Laplacian with 'out' mode.",
IGRAPH_EINVAL,
weights ? "strength" : "degree");
}
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, from, to, -weight * norm));
if (!directed) {
/* no failure possible in undirected case, as zero degrees occur only for isolated vertices */
IGRAPH_CHECK(igraph_sparsemat_entry(sparseres, to, from, -weight * VECTOR(degree)[from]));
}
break;
}
}
igraph_vector_destroy(°ree);
IGRAPH_FINALLY_CLEAN(1);
return IGRAPH_SUCCESS;
}
/**
* \function igraph_laplacian
* \brief Returns the Laplacian matrix of a graph (deprecated).
*
* This function produces the Laplacian matrix of a graph in either dense or
* sparse format. When \p normalized is set to true, the type of normalization
* used depends on the directnedness of the graph: symmetric normalization
* is used for undirected graphs and left stochastic normalization for
* directed graphs.
*
* \param graph Pointer to the graph to convert.
* \param res Pointer to an initialized matrix object or \c NULL. The dense matrix
* result will be stored here.
* \param sparseres Pointer to an initialized sparse matrix object or \c NULL.
* The sparse matrix result will be stored here.
* \param mode Controls whether to use out- or in-degrees in directed graphs.
* If set to \c IGRAPH_ALL, edge directions will be ignored.
* \param normalized Boolean, whether to normalize the result.
* \param weights An optional vector containing non-negative edge weights,
* to calculate the weighted Laplacian matrix. Set it to a null pointer to
* calculate the unweighted Laplacian.
* \return Error code.
*
* \deprecated-by igraph_get_laplacian 0.10.0
*/
igraph_error_t igraph_laplacian(
const igraph_t *graph, igraph_matrix_t *res, igraph_sparsemat_t *sparseres,
igraph_bool_t normalized, const igraph_vector_t *weights
) {
igraph_laplacian_normalization_t norm_method = IGRAPH_LAPLACIAN_UNNORMALIZED;
if (!res && !sparseres) {
IGRAPH_ERROR("Laplacian: specify at least one of 'res' or 'sparseres'",
IGRAPH_EINVAL);
}
if (normalized) {
if (igraph_is_directed(graph)) {
norm_method = IGRAPH_LAPLACIAN_LEFT;
} else {
norm_method = IGRAPH_LAPLACIAN_SYMMETRIC;
}
}
if (res) {
IGRAPH_CHECK(igraph_get_laplacian(graph, res, IGRAPH_OUT, norm_method, weights));
}
if (sparseres) {
IGRAPH_CHECK(igraph_get_laplacian_sparse(graph, sparseres, IGRAPH_OUT, norm_method, weights));
}
return IGRAPH_SUCCESS;
}