1 /*
2    IGraph library.
3    Copyright (C) 2021  The igraph development team <igraph@igraph.org>
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 
19 #include <igraph.h>
20 #include "test_utilities.inc"
21 
main()22 int main() {
23     igraph_t g;
24     igraph_matrix_t result;
25     igraph_vector_bool_t types;
26 
27     printf("No vertices:\n");
28     igraph_small(&g, 0, 0, -1);
29     igraph_matrix_init(&result, 0, 0);
30     igraph_vector_bool_init(&types, 0);
31     IGRAPH_ASSERT(igraph_layout_bipartite(&g, &types, &result, /*hgap*/ 1.0, /*vgap*/ 1.0, /*maxiter*/ 100) == IGRAPH_SUCCESS);
32     print_matrix(&result);
33     igraph_vector_bool_destroy(&types);
34     igraph_matrix_destroy(&result);
35     igraph_destroy(&g);
36 
37     printf("1 vertex:\n");
38     igraph_small(&g, 1, 0, -1);
39     igraph_matrix_init(&result, 0, 0);
40     igraph_vector_bool_init_int(&types, 1, 0);
41     IGRAPH_ASSERT(igraph_layout_bipartite(&g, &types, &result, /*hgap*/ 1.0, /*vgap*/ 1.0, /*maxiter*/ 100) == IGRAPH_SUCCESS);
42     print_matrix(&result);
43     igraph_vector_bool_destroy(&types);
44     igraph_matrix_destroy(&result);
45     igraph_destroy(&g);
46 
47     printf("4 vertices, disconnected, not actually bipartite, with loops and multiple edges:\n");
48     igraph_small(&g, 4, 0, 0,0, 0,0, 0,0, 1,2, 1,2, 1,3, 1,3, 2,3, -1);
49     igraph_matrix_init(&result, 0, 0);
50     igraph_vector_bool_init_int(&types, 4, 0, 1, 0, 1);
51     IGRAPH_ASSERT(igraph_layout_bipartite(&g, &types, &result, /*hgap*/ 1.0, /*vgap*/ 1.0, /*maxiter*/ 100) == IGRAPH_SUCCESS);
52     print_matrix(&result);
53     igraph_vector_bool_destroy(&types);
54     igraph_matrix_destroy(&result);
55     igraph_destroy(&g);
56 
57     printf("10 vertices bipartite graph:\n");
58     igraph_small(&g, 10, 0, 0,5, 0,7, 1,6, 1,7, 1,8, 2,5, 3,8, -1);
59     igraph_matrix_init(&result, 0, 0);
60     igraph_vector_bool_init_int(&types, 10, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1);
61     IGRAPH_ASSERT(igraph_layout_bipartite(&g, &types, &result, /*hgap*/ 1.0, /*vgap*/ 1.0, /*maxiter*/100) == IGRAPH_SUCCESS);
62     print_matrix(&result);
63     igraph_vector_bool_destroy(&types);
64     igraph_matrix_destroy(&result);
65     igraph_destroy(&g);
66 
67     printf("10 vertices bipartite graph, no iterations:\n");
68     igraph_small(&g, 10, 0, 0,5, 0,7, 1,6, 1,7, 1,8, 2,5, 3,8, -1);
69     igraph_matrix_init(&result, 0, 0);
70     igraph_vector_bool_init_int(&types, 10, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1);
71     IGRAPH_ASSERT(igraph_layout_bipartite(&g, &types, &result, /*hgap*/ 1.0, /*vgap*/ 1.0, /*maxiter*/0) == IGRAPH_SUCCESS);
72     print_matrix(&result);
73     igraph_vector_bool_destroy(&types);
74     igraph_matrix_destroy(&result);
75     igraph_destroy(&g);
76 
77     printf("4 vertices with -10 true values for types:\n");
78     igraph_small(&g, 4, 0, 0,1, 1,2, 2,3, 3,0, -1);
79     igraph_matrix_init(&result, 0, 0);
80     igraph_vector_bool_init_int(&types, 4, 0, -10, 0, -10);
81     IGRAPH_ASSERT(igraph_layout_bipartite(&g, &types, &result, /*hgap*/ 1.0, /*vgap*/ 1.0, /*maxiter*/ 100) == IGRAPH_SUCCESS);
82     print_matrix(&result);
83     igraph_vector_bool_destroy(&types);
84     igraph_matrix_destroy(&result);
85     igraph_destroy(&g);
86 
87     printf("4 vertices, negative vgaps:\n");
88     igraph_small(&g, 4, 0, 0,1, 1,2, 2,3, 3,0, -1);
89     igraph_matrix_init(&result, 0, 0);
90     igraph_vector_bool_init_int(&types, 4, 0, 1, 0, 1);
91     IGRAPH_ASSERT(igraph_layout_bipartite(&g, &types, &result, /*hgap*/ 1.0, /*vgap*/ -1.0, /*maxiter*/ 100) == IGRAPH_SUCCESS);
92     print_matrix(&result);
93     igraph_vector_bool_destroy(&types);
94     igraph_matrix_destroy(&result);
95     igraph_destroy(&g);
96 
97     VERIFY_FINALLY_STACK();
98     igraph_set_error_handler(igraph_error_handler_ignore);
99 
100     printf("4 vertices, negative hgaps, emits error.\n");
101     igraph_small(&g, 4, 0, 0,1, 1,2, 2,3, 3,0, -1);
102     igraph_matrix_init(&result, 0, 0);
103     igraph_vector_bool_init_int(&types, 4, 0, 1, 0, 1);
104     IGRAPH_ASSERT(igraph_layout_bipartite(&g, &types, &result, /*hgap*/ -1.0, /*vgap*/ 1.0, /*maxiter*/ 100) == IGRAPH_EINVAL);
105     igraph_vector_bool_destroy(&types);
106     igraph_matrix_destroy(&result);
107     igraph_destroy(&g);
108 
109     VERIFY_FINALLY_STACK();
110     return 0;
111 }
112