1 /* -*- mode: C -*- */
2 /*
3 IGraph library.
4 Copyright (C) 2008-2012 Gabor Csardi <csardi.gabor@gmail.com>
5 334 Harvard street, 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_matrix(const igraph_matrix_t * m)26 int print_matrix(const igraph_matrix_t *m) {
27 long int nrow = igraph_matrix_nrow(m);
28 long int ncol = igraph_matrix_ncol(m);
29 long int i, j;
30 igraph_real_t val;
31
32 for (i = 0; i < nrow; i++) {
33 printf("%li:", i);
34 for (j = 0; j < ncol; j++) {
35 val = MATRIX(*m, i, j);
36 if (igraph_is_inf(val)) {
37 if (val < 0) {
38 printf("-inf");
39 } else {
40 printf(" inf");
41 }
42 } else {
43 printf(" %3.0f", val);
44 }
45 }
46 printf("\n");
47 }
48 return 0;
49 }
50
main()51 int main() {
52
53 igraph_t g;
54 igraph_vector_t weights;
55 igraph_real_t weights_data_0[] = { 0, 2, 1, 0, 5, 2, 1, 1, 0, 2, 2, 8, 1, 1, 3, 1, 1, 4, 2, 1 };
56 igraph_real_t weights_data_1[] = { 6, 7, 8, -4, -2, -3, 9, 2, 7 };
57 igraph_real_t weights_data_2[] = { 6, 7, 2, -4, -2, -3, 9, 2, 7 };
58 igraph_matrix_t res;
59
60 /* Graph with only positive weights */
61 igraph_small(&g, 10, IGRAPH_DIRECTED,
62 0, 1, 0, 2, 0, 3, 1, 2, 1, 4, 1, 5,
63 2, 3, 2, 6, 3, 2, 3, 6,
64 4, 5, 4, 7, 5, 6, 5, 8, 5, 9,
65 7, 5, 7, 8, 8, 9,
66 5, 2,
67 2, 1,
68 -1);
69
70 igraph_vector_view(&weights, weights_data_0,
71 sizeof(weights_data_0) / sizeof(igraph_real_t));
72
73 igraph_matrix_init(&res, 0, 0);
74 igraph_shortest_paths_bellman_ford(&g, &res, igraph_vss_all(), igraph_vss_all(),
75 &weights, IGRAPH_OUT);
76 print_matrix(&res);
77
78 igraph_matrix_destroy(&res);
79 igraph_destroy(&g);
80
81 printf("\n");
82
83 /***************************************/
84
85 /* Graph with negative weights */
86 igraph_small(&g, 5, IGRAPH_DIRECTED,
87 0, 1, 0, 3, 1, 3, 1, 4, 2, 1, 3, 2, 3, 4, 4, 0, 4, 2, -1);
88
89 igraph_vector_view(&weights, weights_data_1,
90 sizeof(weights_data_1) / sizeof(igraph_real_t));
91
92 igraph_matrix_init(&res, 0, 0);
93 igraph_shortest_paths_bellman_ford(&g, &res, igraph_vss_all(),
94 igraph_vss_all(), &weights, IGRAPH_OUT);
95 print_matrix(&res);
96
97 /***************************************/
98
99 /* Same graph with negative loop */
100 igraph_set_error_handler(igraph_error_handler_ignore);
101 igraph_vector_view(&weights, weights_data_2,
102 sizeof(weights_data_2) / sizeof(igraph_real_t));
103 if (igraph_shortest_paths_bellman_ford(&g, &res, igraph_vss_all(),
104 igraph_vss_all(),
105 &weights, IGRAPH_OUT) != IGRAPH_ENEGLOOP) {
106 return 1;
107 }
108
109 igraph_matrix_destroy(&res);
110 igraph_destroy(&g);
111
112 if (!IGRAPH_FINALLY_STACK_EMPTY) {
113 return 1;
114 }
115
116 return 0;
117 }
118