1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2009-2012  Gabor Csardi <csardi.gabor@gmail.com>
5    334 Harvard street, Cambridge, MA 02139 USA
6 
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.
11 
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.
16 
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
20    02110-1301 USA
21 
22 */
23 
24 #include <igraph.h>
25 #include <math.h>
26 
27 #define ALMOST_EQUALS(a, b) (fabs((a)-(b)) < 1e-8)
28 
main()29 int main() {
30 
31     igraph_t g;
32     igraph_real_t cent;
33     igraph_arpack_options_t arpack_options;
34 
35     /****************************/
36     /* in-star */
37     igraph_star(&g, 10, IGRAPH_STAR_IN, /*center=*/ 0);
38 
39     igraph_centralization_degree(&g, /*res=*/ 0,
40                                  /*mode=*/ IGRAPH_IN, IGRAPH_NO_LOOPS,
41                                  &cent, /*theoretical_max=*/ 0,
42                                  /*normalized=*/ 1);
43     if (cent != 1.0) {
44         fprintf(stderr, "in-star, degree: %g\n", cent);
45         return 1;
46     }
47 
48     igraph_centralization_betweenness(&g, /*res=*/ 0,
49                                       IGRAPH_UNDIRECTED, &cent,
50                                       /*theoretical_max=*/ 0,
51                                       /*normalized=*/ 1);
52     if (cent != 1.0) {
53         fprintf(stderr, "in-star, betweenness: %g\n", cent);
54         return 2;
55     }
56 
57     /* Skip closeness, as it is not well-defined for disconnected graphs such as an in-star. */
58 
59     igraph_destroy(&g);
60 
61     /****************************/
62     /* out-star */
63     igraph_star(&g, 10, IGRAPH_STAR_OUT, /*center=*/ 0);
64 
65     igraph_centralization_degree(&g, /*res=*/ 0,
66                                  /*mode=*/ IGRAPH_OUT, IGRAPH_NO_LOOPS,
67                                  &cent, /*theoretical_max=*/ 0,
68                                  /*normalized=*/ 1);
69     if (cent != 1.0) {
70         fprintf(stderr, "out-star, degree: %g\n", cent);
71         return 11;
72     }
73 
74     igraph_centralization_betweenness(&g, /*res=*/ 0,
75                                       IGRAPH_UNDIRECTED, &cent,
76                                       /*theoretical_max=*/ 0,
77                                       /*normalized=*/ 1);
78     if (cent != 1.0) {
79         fprintf(stderr, "out-star, betweenness: %g\n", cent);
80         return 12;
81     }
82 
83     /* Skip closeness, as it is not well-defined for disconnected graphs such as an out-star. */
84 
85     igraph_destroy(&g);
86 
87     /****************************/
88     /* undirected star */
89     igraph_star(&g, 10, IGRAPH_STAR_UNDIRECTED, /*center=*/ 0);
90 
91     igraph_centralization_degree(&g, /*res=*/ 0,
92                                  /*mode=*/ IGRAPH_ALL, IGRAPH_NO_LOOPS,
93                                  &cent, /*theoretical_max=*/ 0,
94                                  /*normalized=*/ 1);
95     if (cent != 1.0) {
96         fprintf(stderr, "undirected star, degree: %g\n", cent);
97         return 21;
98     }
99 
100     igraph_centralization_betweenness(&g, /*res=*/ 0,
101                                       IGRAPH_UNDIRECTED, &cent,
102                                       /*theoretical_max=*/ 0,
103                                       /*normalized=*/ 1);
104     if (cent != 1.0) {
105         fprintf(stderr, "undirected star, betweenness: %g\n", cent);
106         return 22;
107     }
108 
109     igraph_centralization_closeness(&g, /*res=*/ 0,
110                                     IGRAPH_ALL, &cent,
111                                     /*theoretical_max=*/ 0,
112                                     /*normalization=*/ 1);
113 
114     if (!ALMOST_EQUALS(cent, 1.0)) {
115         fprintf(stderr, "undirected star, closeness: %g\n", cent);
116         return 23;
117     }
118 
119     igraph_destroy(&g);
120 
121     /****************************/
122     /* single dyad */
123 
124     igraph_small(&g, /*n=*/ 10, /*directed=*/ 0,
125                  0, 1, -1);
126 
127     igraph_arpack_options_init(&arpack_options);
128     igraph_centralization_eigenvector_centrality(&g, /*vector=*/ 0,
129             /*value=*/ 0,
130             /*directed=*/ 1,
131             /*scale=*/ 1,
132             &arpack_options, &cent,
133             /*theoretical_max=*/ 0,
134             /*normalization=*/ 1);
135 
136     if (!ALMOST_EQUALS(cent, 1.0)) {
137         fprintf(stderr, "dyad, eigenvector centrality: %g\n", cent);
138         return 24;
139     }
140 
141     igraph_centralization_eigenvector_centrality(&g, /*vector=*/ 0,
142             /*value=*/ 0,
143             /*directed=*/ 1,
144             /*scale=*/ 0,
145             &arpack_options, &cent,
146             /*theoretical_max=*/ 0,
147             /*normalization=*/ 1);
148 
149     if (!ALMOST_EQUALS(cent, 1.0)) {
150         fprintf(stderr, "dyad, eigenvector centrality, not scaled: %g\n", cent);
151         return 25;
152     }
153 
154     igraph_destroy(&g);
155 
156     return 0;
157 }
158