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
|
/*
IGraph library.
Copyright (C) 2021 The igraph development team <igraph@igraph.org>
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, see <https://www.gnu.org/licenses/>.
*/
#include <igraph.h>
#include "test_utilities.h"
void test_print_destroy(igraph_t *g, igraph_vector_t *weights, float resolution, igraph_matrix_t *modmat, igraph_bool_t directed) {
igraph_integer_t i, j;
IGRAPH_ASSERT(igraph_modularity_matrix(g, weights, resolution, modmat, directed) == IGRAPH_SUCCESS);
for (i = 0; i < igraph_matrix_nrow(modmat); i++) {
for (j = 0; j < igraph_matrix_ncol(modmat); j++) {
if (fabs(MATRIX(*modmat, i, j)) < 1e-10) {
MATRIX(*modmat, i, j) = 0;
}
}
}
print_matrix(modmat);
igraph_destroy(g);
igraph_matrix_destroy(modmat);
if (weights) {
igraph_vector_destroy(weights);
}
}
int main(void) {
igraph_t g;
igraph_vector_t weights;
igraph_vector_int_t membership;
igraph_matrix_t modmat;
igraph_real_t modularity, test_modularity;
igraph_integer_t i, j;
printf("No vertices:\n");
igraph_small(&g, 0, /*directed*/0, -1);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, NULL, 1.0, &modmat, 0);
printf("No edges:\n");
igraph_small(&g, 3, /*directed*/0, -1);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, NULL, 1.0, &modmat, 0);
printf("Triangle with no resolution should give the adjacency matrix:\n");
igraph_small(&g, 3, /*directed*/0, 0,1, 0,2, 1,2, -1);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, NULL, 0.0, &modmat, 0);
printf("Triangle and point with self-loop, undirected :\n");
igraph_small(&g, 4, /*directed*/0, 0,1, 0,2, 1,2, 3,3, -1);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, NULL, 1.0, &modmat, 0);
printf("Triangle and point with self-loop, directed, but direction ignored:\n");
igraph_small(&g, 4, /*directed*/1, 0,1, 0,2, 1,2, 3,3, -1);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, NULL, 1.0, &modmat, 0);
printf("Triangle and point with self-loop, directed:\n");
igraph_small(&g, 4, /*directed*/1, 0,1, 0,2, 1,2, 3,3, -1);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, NULL, 1.0, &modmat, 1);
printf("Triangle with weights 0, 1, 2:\n");
igraph_small(&g, 3, /*directed*/0, 0,1, 0,2, 1,2, -1);
igraph_vector_init_int(&weights, 3, 0, 1, 2);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, &weights, 1.0, &modmat, 0);
printf("Triangle with weights 0, -1, -2:\n");
igraph_small(&g, 3, /*directed*/0, 0,1, 0,2, 1,2, -1);
igraph_vector_init_int(&weights, 3, 0, -1, -2);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, &weights, 1.0, &modmat, 0);
printf("Directed triangle with weights 0, 1, 2:\n");
igraph_small(&g, 3, /*directed*/1, 0,1, 0,2, 1,2, -1);
igraph_vector_init_int(&weights, 3, 0, 1, 2);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, &weights, 1.0, &modmat, 1);
printf("Triangle with weights -1, 0, 1 will cause divisions by zero:\n");
igraph_small(&g, 3, /*directed*/0, 0,1, 0,2, 1,2, -1);
igraph_vector_init_int(&weights, 3, -1, 0, 1);
igraph_matrix_init(&modmat, 0, 0);
test_print_destroy(&g, &weights, 1.0, &modmat, 0);
printf("Comparison with modularity:\n");
igraph_small(&g, 5, /*directed*/1, 0,1, 0,2, 1,2, 3,4, 4,0, -1);
igraph_vector_init_int(&weights, 5, 1, 2, 3, 4, 5);
igraph_vector_int_init_int(&membership, 5, 0, 0, 0, 1, 1);
igraph_matrix_init(&modmat, 0, 0);
IGRAPH_ASSERT(igraph_modularity_matrix(&g, &weights, 0.7, &modmat, 1) == IGRAPH_SUCCESS);
IGRAPH_ASSERT(igraph_modularity(&g, &membership, &weights, 0.7, 1, &modularity) == IGRAPH_SUCCESS);
print_matrix(&modmat);
test_modularity = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
test_modularity += MATRIX(modmat, i, j);
}
}
for (i = 3; i < 5; i++) {
for (j = 3; j < 5; j++) {
test_modularity += MATRIX(modmat, i, j);
}
}
printf("Modularity: %g, modularity via matrix: %g\n", modularity, test_modularity / igraph_vector_sum(&weights));
igraph_destroy(&g);
igraph_vector_int_destroy(&membership);
igraph_vector_destroy(&weights);
igraph_matrix_destroy(&modmat);
VERIFY_FINALLY_STACK();
return 0;
}
|