1 /* -*- mode: C -*- */
2 /*
3 IGraph library.
4 Copyright (C) 2008-2012 Gabor Csardi <csardi.gabor@gmail.com>
5 334 Harvard st, 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 <stdlib.h>
26
print_vector(igraph_vector_t * v,FILE * f)27 void print_vector(igraph_vector_t *v, FILE *f) {
28 long int i;
29 for (i = 0; i < igraph_vector_size(v); i++) {
30 fprintf(f, " %li", (long int) VECTOR(*v)[i]);
31 }
32 fprintf(f, "\n");
33 }
34
check_simple()35 int check_simple() {
36
37 igraph_t g;
38 long int nodes = 100;
39 long int edges = 1000;
40 igraph_real_t p = 3.0 / nodes;
41 long int runs = 10;
42 long int r, e, ecount;
43 igraph_vector_t eids, pairs, path;
44
45 igraph_rng_seed(igraph_rng_default(), 42); /* make tests deterministic */
46
47 igraph_vector_init(&pairs, edges * 2);
48 igraph_vector_init(&path, 0);
49 igraph_vector_init(&eids, 0);
50
51 for (r = 0; r < runs; r++) {
52 igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, nodes, p,
53 /*directed=*/ 0, /*loops=*/ 0);
54 ecount = igraph_ecount(&g);
55 for (e = 0; e < edges; e++) {
56 long int edge = RNG_INTEGER(0, ecount - 1);
57 VECTOR(pairs)[2 * e] = IGRAPH_FROM(&g, edge);
58 VECTOR(pairs)[2 * e + 1] = IGRAPH_TO(&g, edge);
59 }
60 igraph_get_eids(&g, &eids, &pairs, /*path=*/ 0, 0, /*error=*/ 1);
61 for (e = 0; e < edges; e++) {
62 long int edge = VECTOR(eids)[e];
63 long int from1 = VECTOR(pairs)[2 * e];
64 long int to1 = VECTOR(pairs)[2 * e + 1];
65 long int from2 = IGRAPH_FROM(&g, edge);
66 long int to2 = IGRAPH_TO(&g, edge);
67 long int min1 = from1 < to1 ? from1 : to1;
68 long int max1 = from1 < to1 ? to1 : from1;
69 long int min2 = from2 < to2 ? from2 : to2;
70 long int max2 = from2 < to2 ? to2 : from2;
71 if (min1 != min2 || max1 != max2) {
72 return 11;
73 }
74 }
75
76 igraph_diameter(&g, /*res=*/ 0, /*from=*/ 0, /*to=*/ 0, &path,
77 IGRAPH_UNDIRECTED, /*unconn=*/ 1);
78 igraph_get_eids(&g, &eids, /*pairs=*/ 0, &path, 0, /*error=*/ 1);
79 for (e = 0; e < igraph_vector_size(&path) - 1; e++) {
80 long int edge = VECTOR(eids)[e];
81 long int from1 = VECTOR(path)[e];
82 long int to1 = VECTOR(path)[e + 1];
83 long int from2 = IGRAPH_FROM(&g, edge);
84 long int to2 = IGRAPH_TO(&g, edge);
85 long int min1 = from1 < to1 ? from1 : to1;
86 long int max1 = from1 < to1 ? to1 : from1;
87 long int min2 = from2 < to2 ? from2 : to2;
88 long int max2 = from2 < to2 ? to2 : from2;
89 if (min1 != min2 || max1 != max2) {
90 return 12;
91 }
92 }
93
94 igraph_destroy(&g);
95 }
96
97 igraph_vector_destroy(&path);
98 igraph_vector_destroy(&pairs);
99 igraph_vector_destroy(&eids);
100
101 return 0;
102 }
103
check_multi()104 int check_multi() {
105
106 igraph_t g;
107 igraph_vector_t vec;
108 igraph_vector_t eids, eids2;
109 int ret;
110 long int i;
111
112 igraph_real_t q1[] = { 0, 1, 0, 1 };
113 igraph_real_t q2[] = { 0, 1, 0, 1, 0, 1 };
114 igraph_real_t q3[] = { 1, 0, 3, 4, 1, 0, 0, 1, 3, 4, 0, 1 };
115
116 igraph_vector_init(&eids, 0);
117
118 /*********************************/
119 igraph_small(&g, /*n=*/ 10, /*directed=*/ 1,
120 0, 1, 0, 1, 1, 0, 1, 2, 3, 4, 3, 4, 3, 4, 3, 5, 3, 7,
121 9, 8,
122 -1);
123
124 igraph_vector_view(&vec, q1, sizeof(q1) / sizeof(igraph_real_t));
125 igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/ 1, /*error=*/ 1);
126 igraph_vector_sort(&eids);
127 print_vector(&eids, stdout);
128
129 igraph_vector_view(&vec, q2, sizeof(q2) / sizeof(igraph_real_t));
130 igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/ 0, /*error=*/ 1);
131 igraph_vector_sort(&eids);
132 print_vector(&eids, stdout);
133
134 igraph_vector_view(&vec, q2, sizeof(q2) / sizeof(igraph_real_t));
135 igraph_set_error_handler(igraph_error_handler_ignore);
136 ret = igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/ 1, /*error=*/1);
137 if (ret != IGRAPH_EINVAL) {
138 return 1;
139 }
140 igraph_set_error_handler(igraph_error_handler_abort);
141
142 igraph_destroy(&g);
143 /*********************************/
144
145 /*********************************/
146 igraph_small(&g, /*n=*/10, /*directed=*/0,
147 0, 1, 1, 0, 0, 1, 3, 4, 3, 4, 5, 4, 9, 8,
148 -1);
149
150 igraph_vector_view(&vec, q1, sizeof(q1) / sizeof(igraph_real_t));
151 igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/1, /*error=*/ 1);
152 igraph_vector_sort(&eids);
153 print_vector(&eids, stdout);
154
155 igraph_vector_view(&vec, q3, sizeof(q3) / sizeof(igraph_real_t));
156 igraph_set_error_handler(igraph_error_handler_ignore);
157 ret = igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/0, /*error=*/ 1);
158 if (ret != IGRAPH_EINVAL) {
159 return 2;
160 }
161 igraph_set_error_handler(igraph_error_handler_abort);
162
163 igraph_destroy(&g);
164
165 /*********************************/
166
167 igraph_vector_destroy(&eids);
168
169 /*********************************/
170 /* Speed tests */
171
172 #define NODES 10000
173 igraph_barabasi_game(&g, /*n=*/ NODES, /*power=*/ 1.0, /*m=*/ 3,
174 /*outseq=*/ 0, /*outpref=*/ 0, /*A=*/ 1,
175 /*directed=*/ 1, IGRAPH_BARABASI_BAG,
176 /*start_from=*/ 0);
177 igraph_simplify(&g, /*multiple=*/ 1, /*loops=*/ 0, /*edge_comb=*/ 0);
178
179 igraph_vector_init(&eids, NODES / 2);
180 igraph_random_sample(&eids, 0, igraph_ecount(&g) - 1, NODES / 2);
181 igraph_vector_init(&vec, NODES);
182 for (i = 0; i < NODES / 2; i++) {
183 VECTOR(vec)[2 * i] = IGRAPH_FROM(&g, VECTOR(eids)[i]);
184 VECTOR(vec)[2 * i + 1] = IGRAPH_TO(&g, VECTOR(eids)[i]);
185 }
186 igraph_vector_init(&eids2, 0);
187 igraph_get_eids_multi(&g, &eids2, &vec, 0, /*directed=*/ 1, /*error=*/ 1);
188 if (!igraph_vector_all_e(&eids, &eids2)) {
189 return 3;
190 }
191
192 /**/
193
194 for (i = 0; i < NODES / 2; i++) {
195 VECTOR(vec)[2 * i] = IGRAPH_TO(&g, VECTOR(eids)[i]);
196 VECTOR(vec)[2 * i + 1] = IGRAPH_FROM(&g, VECTOR(eids)[i]);
197 }
198 igraph_get_eids_multi(&g, &eids2, &vec, 0, /*directed=*/ 0, /*error=*/ 1);
199 if (!igraph_vector_all_e(&eids, &eids2)) {
200 return 4;
201 }
202
203 igraph_vector_destroy(&eids);
204 igraph_vector_destroy(&eids2);
205 igraph_vector_destroy(&vec);
206 igraph_destroy(&g);
207
208 /*********************************/
209
210 return 0;
211 }
212
main()213 int main() {
214 int ret;
215
216 if ( (ret = check_simple()) != 0) {
217 return ret;
218 }
219 if ( (ret = check_multi()) != 0) {
220 return ret;
221 }
222
223 return 0;
224 }
225