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 
print_vector(igraph_vector_t * v)26 void print_vector(igraph_vector_t *v) {
27     long int i, l = igraph_vector_size(v);
28     for (i = 0; i < l; i++) {
29         printf(" %li", (long int) VECTOR(*v)[i]);
30     }
31     printf("\n");
32 }
33 
main()34 int main() {
35 
36     igraph_t left, right, isec;
37     igraph_vector_t v;
38     igraph_vector_ptr_t glist;
39     igraph_t g1, g2, g3;
40     igraph_vector_t edge_map1, edge_map2;
41 
42     igraph_vector_init_int_end(&v, -1, 0, 1, 1, 2, 2, 3, -1);
43     igraph_create(&left, &v, 0, IGRAPH_DIRECTED);
44     igraph_vector_destroy(&v);
45 
46     igraph_vector_init_int_end(&v, -1, 1, 0, 5, 4, 1, 2, 3, 2, -1);
47     igraph_create(&right, &v, 0, IGRAPH_DIRECTED);
48     igraph_vector_destroy(&v);
49 
50     igraph_vector_init(&edge_map1, 0);
51     igraph_vector_init(&edge_map2, 0);
52 
53     igraph_intersection(&isec, &left, &right, &edge_map1, &edge_map2);
54     igraph_vector_init(&v, 0);
55     igraph_get_edgelist(&isec, &v, 0);
56     printf("---\n");
57     print_vector(&v);
58     print_vector(&edge_map1);
59     print_vector(&edge_map2);
60     printf("---\n");
61     igraph_vector_destroy(&v);
62     igraph_destroy(&left);
63     igraph_destroy(&right);
64     igraph_destroy(&isec);
65     igraph_vector_destroy(&edge_map1);
66     igraph_vector_destroy(&edge_map2);
67 
68     /* empty graph list */
69     igraph_vector_ptr_init(&glist, 0);
70     igraph_intersection_many(&isec, &glist, 0);
71     if (igraph_vcount(&isec) != 0 || !igraph_is_directed(&isec)) {
72         return 1;
73     }
74     igraph_destroy(&isec);
75     igraph_vector_ptr_destroy(&glist);
76 
77     /* graph list with an empty graph */
78     igraph_vector_ptr_init(&glist, 3);
79     igraph_vector_init_int_end(&v, -1, 0, 1, 1, 2, 2, 3, -1);
80     igraph_create(&g1, &v, 0, IGRAPH_DIRECTED);
81     igraph_vector_destroy(&v);
82     igraph_vector_init_int_end(&v, -1, 0, 1, 1, 2, 2, 3, -1);
83     igraph_create(&g2, &v, 0, IGRAPH_DIRECTED);
84     igraph_vector_destroy(&v);
85     igraph_empty(&g3, 10, IGRAPH_DIRECTED);
86 
87     VECTOR(glist)[0] = &g1;
88     VECTOR(glist)[1] = &g2;
89     VECTOR(glist)[2] = &g3;
90     igraph_intersection_many(&isec, &glist, 0);
91     if (igraph_ecount(&isec) != 0 || igraph_vcount(&isec) != 10) {
92         return 2;
93     }
94     igraph_destroy(&g1);
95     igraph_destroy(&g2);
96     igraph_destroy(&g3);
97     igraph_destroy(&isec);
98     igraph_vector_ptr_destroy(&glist);
99 
100     /* "proper" graph list */
101     igraph_vector_ptr_init(&glist, 3);
102     igraph_vector_init_int_end(&v, -1, 0, 1, 1, 2, 2, 3, -1);
103     igraph_create(&g1, &v, 0, IGRAPH_DIRECTED);
104     igraph_vector_destroy(&v);
105     igraph_vector_init_int_end(&v, -1, 0, 1, 1, 2, 2, 3, 3, 2, 4, 5, 6, 5, -1);
106     igraph_create(&g2, &v, 0, IGRAPH_DIRECTED);
107     igraph_vector_destroy(&v);
108     igraph_vector_init_int_end(&v, -1, 2, 3, 1, 0, 1, 2, 3, 2, 4, 5, 6, 5, 2, 3, -1);
109     igraph_create(&g3, &v, 0, IGRAPH_DIRECTED);
110     igraph_vector_destroy(&v);
111 
112     VECTOR(glist)[0] = &g1;
113     VECTOR(glist)[1] = &g2;
114     VECTOR(glist)[2] = &g3;
115     igraph_intersection_many(&isec, &glist, 0);
116     igraph_write_graph_edgelist(&isec, stdout);
117     igraph_destroy(&g1);
118     igraph_destroy(&g2);
119     igraph_destroy(&g3);
120     igraph_destroy(&isec);
121     igraph_vector_ptr_destroy(&glist);
122 
123     return 0;
124 }
125