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 
compute_and_print(igraph_t * graph,igraph_vector_t * weights,igraph_bool_t directed,igraph_bool_t unconn)22 void compute_and_print(igraph_t *graph, igraph_vector_t *weights, igraph_bool_t directed, igraph_bool_t unconn) {
23     igraph_real_t result;
24     igraph_real_t unconn_pairs;
25 
26     IGRAPH_ASSERT(igraph_average_path_length_dijkstra(graph, &result, &unconn_pairs, weights,
27                   directed, unconn) == IGRAPH_SUCCESS);
28 
29     printf("Result: ");
30     print_real(stdout, result, "%8g");
31     printf("\nUnconnected pairs: ");
32     print_real(stdout, unconn_pairs, "%8g");
33     printf("\n\n");
34 }
35 
main()36 int main() {
37     igraph_t g_0, g_1, g_2, g_3, g_lm;
38     igraph_vector_t weights_0, weights_3, weights_lm, weights_lm_neg;
39     igraph_real_t result;
40 
41     igraph_small(&g_0, 0, 0, -1);
42     igraph_small(&g_1, 1, 0, -1);
43     igraph_small(&g_2, 2, 0, -1);
44     igraph_small(&g_3, 2, 1, 0,1, 0,2, -1);
45     igraph_small(&g_lm, 6, 1, 0,1, 0,2, 1,1, 1,3, 2,0, 2,3, 3,4, 3,4, -1);
46 
47     igraph_vector_init(&weights_0, 0);
48     igraph_vector_init_int(&weights_3, 2, 1, 1);
49     igraph_vector_init_int(&weights_lm, 8, 0, 1, 2, 3, 4, 5, 6, 7);
50     igraph_vector_init_int(&weights_lm_neg, 8, -10, 1, 2, 3, 4, 5, 6, 7);
51 
52     printf("No vertices:\n");
53     compute_and_print(&g_0, &weights_0, 1, 1);
54 
55     printf("One vertex:\n");
56     compute_and_print(&g_1, &weights_0, 1, 1);
57 
58     printf("Two vertices:\n");
59     compute_and_print(&g_2, &weights_0, 1, 1);
60 
61     printf("Two vertices, inf for unconnected pairs:\n");
62     compute_and_print(&g_2, &weights_0, 1, 0);
63 
64     printf("Smallest bifurcating directed tree:\n");
65     compute_and_print(&g_3, &weights_3, 1, 1);
66 
67     printf("Smallest bifurcating directed tree, inf for unconnected pairs:\n");
68     compute_and_print(&g_3, &weights_3, 1, 0);
69 
70     printf("Graph with loops and multiple edges:\n");
71     compute_and_print(&g_lm, &weights_lm, 1, 1);
72 
73     printf("Graph with loops and multiple edges, ignoring direction:\n");
74     compute_and_print(&g_lm, &weights_lm, 0, 1);
75 
76     VERIFY_FINALLY_STACK();
77     igraph_set_error_handler(igraph_error_handler_ignore);
78 
79     printf("Checking incorrect weight length error handling.\n");
80     IGRAPH_ASSERT(igraph_average_path_length_dijkstra(&g_lm, &result, NULL, &weights_0,
81                   1, 1) == IGRAPH_EINVAL);
82 
83     printf("Checking negative weight error handling.\n");
84     IGRAPH_ASSERT(igraph_average_path_length_dijkstra(&g_lm, &result, NULL, &weights_lm_neg,
85                   1, 1) == IGRAPH_EINVAL);
86 
87     igraph_destroy(&g_0);
88     igraph_destroy(&g_1);
89     igraph_destroy(&g_2);
90     igraph_destroy(&g_3);
91     igraph_destroy(&g_lm);
92     igraph_vector_destroy(&weights_0);
93     igraph_vector_destroy(&weights_3);
94     igraph_vector_destroy(&weights_lm);
95     igraph_vector_destroy(&weights_lm_neg);
96 
97     VERIFY_FINALLY_STACK();
98     return 0;
99 }
100