1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2006-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 
26 #include <stdlib.h>
27 
print_vector(igraph_vector_t * v)28 void print_vector(igraph_vector_t *v) {
29     long int i, l = igraph_vector_size(v);
30     for (i = 0; i < l; i++) {
31         printf(" %li", (long int) VECTOR(*v)[i]);
32     }
33     printf("\n");
34 }
35 
check_evecs(const igraph_t * graph,const igraph_vector_ptr_t * vecs,const igraph_vector_ptr_t * evecs,int error_code)36 int check_evecs(const igraph_t *graph, const igraph_vector_ptr_t *vecs,
37                 const igraph_vector_ptr_t *evecs, int error_code) {
38 
39     igraph_bool_t directed = igraph_is_directed(graph);
40     long int i, n = igraph_vector_ptr_size(vecs);
41     if (igraph_vector_ptr_size(evecs) != n) {
42         exit(error_code + 1);
43     }
44 
45     for (i = 0; i < n; i++) {
46         igraph_vector_t *vvec = VECTOR(*vecs)[i];
47         igraph_vector_t *evec = VECTOR(*evecs)[i];
48         long int j, n2 = igraph_vector_size(evec);
49         if (igraph_vector_size(vvec) == 0 && n2 == 0) {
50             continue;
51         }
52         if (igraph_vector_size(vvec) != n2 + 1) {
53             exit(error_code + 2);
54         }
55         for (j = 0; j < n2; j++) {
56             long int edge = VECTOR(*evec)[j];
57             long int from = VECTOR(*vvec)[j];
58             long int to = VECTOR(*vvec)[j + 1];
59             if (directed) {
60                 if (from != IGRAPH_FROM(graph, edge) ||
61                     to   != IGRAPH_TO  (graph, edge)) {
62                     exit(error_code);
63                 }
64             } else {
65                 long int from2 = IGRAPH_FROM(graph, edge);
66                 long int to2 = IGRAPH_TO(graph, edge);
67                 long int min1 = from < to ? from : to;
68                 long int max1 = from < to ? to : from;
69                 long int min2 = from2 < to2 ? from2 : to2;
70                 long int max2 = from2 < to2 ? to2 : from2;
71                 if (min1 != min2 || max1 != max2) {
72                     exit(error_code + 3);
73                 }
74             }
75         }
76     }
77 
78     return 0;
79 }
80 
main()81 int main() {
82 
83     igraph_t g;
84     igraph_vector_ptr_t vecs, evecs;
85     igraph_vector_long_t pred, inbound;
86     long int i;
87     igraph_vs_t vs;
88 
89     igraph_ring(&g, 10, IGRAPH_DIRECTED, 0, 1);
90 
91     igraph_vector_ptr_init(&vecs, 5);
92     igraph_vector_ptr_init(&evecs, 5);
93     igraph_vector_long_init(&pred, 0);
94     igraph_vector_long_init(&inbound, 0);
95 
96     for (i = 0; i < igraph_vector_ptr_size(&vecs); i++) {
97         VECTOR(vecs)[i] = calloc(1, sizeof(igraph_vector_t));
98         igraph_vector_init(VECTOR(vecs)[i], 0);
99         VECTOR(evecs)[i] = calloc(1, sizeof(igraph_vector_t));
100         igraph_vector_init(VECTOR(evecs)[i], 0);
101     }
102     igraph_vs_vector_small(&vs, 1, 3, 5, 2, 1,  -1);
103 
104     igraph_get_shortest_paths(&g, &vecs, &evecs, 0, vs, IGRAPH_OUT, &pred, &inbound);
105 
106     check_evecs(&g, &vecs, &evecs, 10);
107 
108     for (i = 0; i < igraph_vector_ptr_size(&vecs); i++) {
109         print_vector(VECTOR(vecs)[i]);
110         igraph_vector_destroy(VECTOR(vecs)[i]);
111         free(VECTOR(vecs)[i]);
112         igraph_vector_destroy(VECTOR(evecs)[i]);
113         free(VECTOR(evecs)[i]);
114     }
115 
116     igraph_vector_long_print(&pred);
117     igraph_vector_long_print(&inbound);
118 
119     igraph_vector_ptr_destroy(&vecs);
120     igraph_vector_ptr_destroy(&evecs);
121     igraph_vector_long_destroy(&pred);
122     igraph_vector_long_destroy(&inbound);
123     igraph_vs_destroy(&vs);
124     igraph_destroy(&g);
125 
126     if (!IGRAPH_FINALLY_STACK_EMPTY) {
127         return 1;
128     }
129 
130     return 0;
131 }
132