aboutsummaryrefslogtreecommitdiff
path: root/src/graph.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/graph.c')
-rw-r--r--src/graph.c1311
1 files changed, 1311 insertions, 0 deletions
diff --git a/src/graph.c b/src/graph.c
new file mode 100644
index 0000000..dd7058b
--- /dev/null
+++ b/src/graph.c
@@ -0,0 +1,1311 @@
1#include "graph.h"
2#include "config.h"
3#include "utils.h"
4#include "dynamic-array.h"
5#include <string.h>
6#include <gsl/gsl_blas.h>
7#include <gsl/gsl_eigen.h>
8#include <gsl/gsl_sort_vector.h>
9
10#define ALLOC_SQUARE_GSL_MATRIX(n) gsl_matrix_calloc ((n), (n))
11
12gsl_matrix *
13shimbell_method (GRAPH g, size_t edge_count, bool max)
14{
15 size_t n = g->vertex_count;
16 gsl_matrix *adj = g->weight_matrix;
17 gsl_matrix *shimbell = ALLOC_SQUARE_GSL_MATRIX (n);
18
19 gsl_matrix_memcpy (shimbell, adj);
20 if (n == 1)
21 return shimbell;
22
23 gsl_matrix *tmp = ALLOC_SQUARE_GSL_MATRIX (n);
24 for (size_t l = 0; l < edge_count; ++l) {
25 gsl_matrix_memcpy (tmp, shimbell);
26 for (size_t i = 0; i < n; ++i) {
27 for (size_t j = 0; j < n; ++j) {
28 double bestval = max ? 0 : INFINITY;
29 for (size_t k = 0; k < n; ++k) {
30 double a = gsl_matrix_get (tmp, k, j);
31 double b = gsl_matrix_get (adj, i, k);
32 if (a == 0 || b == 0)
33 continue;
34 double val = a + b;
35 if (max ? val > bestval
36 : val < bestval) {
37 bestval = val;
38 }
39 } gsl_matrix_set (shimbell, i, j, bestval);
40 }
41 }
42 } gsl_matrix_free (tmp);
43
44 return shimbell;
45}
46
47size_t
48graph_count_routes (GRAPH g, size_t from, size_t to)
49{
50 size_t n = g->vertex_count;
51 gsl_matrix *adj = g->adjecency_matrix;
52 gsl_matrix *con = ALLOC_SQUARE_GSL_MATRIX(n);
53 gsl_matrix *pow = ALLOC_SQUARE_GSL_MATRIX(n);
54 gsl_matrix *tmp = ALLOC_SQUARE_GSL_MATRIX(n);
55
56 for (size_t e = 0; e < n*n; ++e)
57 con->data[e] = adj->data[e] != 0 ? 1 : 0;
58 gsl_matrix_memcpy (pow, con);
59 gsl_matrix_memcpy (tmp, con);
60
61 size_t count = gsl_matrix_get (pow, from, to);
62 for (size_t k = 0; k < n - 2; ++k) {
63 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
64 1.0, tmp, con,
65 0.0, pow);
66 count += gsl_matrix_get (pow, from, to);
67 gsl_matrix *swap = tmp;
68 tmp = pow;
69 pow = swap;
70 }
71
72 gsl_matrix_free (con);
73 gsl_matrix_free (tmp);
74 gsl_matrix_free (pow);
75
76 return count;
77}
78
79static inline void
80is_graph_connected_implementation (gsl_matrix *adj, bool *visited, size_t v)
81{
82 size_t n = adj->size1;
83 visited[v] = true;
84 for (size_t i = 0; i < n; ++i) {
85 if (!visited[i] &&
86 (gsl_matrix_get(adj, v, i) ||
87 gsl_matrix_get(adj, i, v))) {
88 is_graph_connected_implementation (adj, visited, i);
89 }
90 }
91}
92
93bool
94is_graph_connected (GRAPH g)
95{
96 size_t n = g->vertex_count;
97 bool visited[n];
98 memset (visited, 0, sizeof (visited));
99 is_graph_connected_implementation (g->adjecency_matrix, visited, 0);
100 for (size_t i = 0; i < n; ++i) {
101 if (!visited[i]) return false;
102 } return true;
103}
104
105bool
106is_graph_algebraically_connected (gsl_matrix *adj)
107{
108 size_t n = adj->size1;
109
110 gsl_matrix *laplacian = gsl_matrix_alloc (n, n);
111 for (size_t i = 0; i < n; ++i) {
112 double deg = 0;
113 for (size_t j = i + 1; j < n; ++j) {
114 double val = gsl_matrix_get (adj, i, j);
115 gsl_matrix_set (laplacian, j, i, val == 0.0 ? 0.0 : -val);
116 deg += gsl_matrix_get (adj, i, j);
117 } gsl_matrix_set (laplacian, i, i, deg);
118 }
119
120 gsl_vector *eval = gsl_vector_alloc (n);
121 gsl_eigen_symm_workspace *w = gsl_eigen_symm_alloc (n);
122 gsl_eigen_symm (laplacian, eval, w);
123 gsl_sort_vector (eval);
124 double fiedler = gsl_vector_get (eval, 1);
125
126 gsl_eigen_symm_free (w);
127 gsl_vector_free (eval);
128 gsl_matrix_free (laplacian);
129
130 return fiedler > 1e-12 ? true : false;
131}
132
133void
134generate_graph (GRAPH g, DISTRIBUTION dist,
135 size_t vertex_count, enum graph_type type)
136{
137 g->vertex_count = vertex_count;
138 g->type = type;
139
140 size_t n = vertex_count;
141 // size_t m = n - 1;
142 size_t m = MIN (n*3/2, (n*n - n)/2);
143
144 g -> adjecency_matrix = ALLOC_SQUARE_GSL_MATRIX(n);
145 g -> weight_matrix = ALLOC_SQUARE_GSL_MATRIX(n);
146 g -> capacity_matrix = ALLOC_SQUARE_GSL_MATRIX(n);
147
148 // for (size_t i = 0; i < n - 1; ++i) {
149 // size_t max_degree = n - i - 1;
150 // size_t degree = floor (distribution_nrand (dist)*(max_degree - 1)) + 1;
151
152 // for (size_t j = 0; j < degree; ++j) {
153 // double weight = floor ((MAX_WEIGHT - 1)*distribution_nrand (dist)) + 1;
154 // if (g->type == GRAPH_TYPE_POSITIVE_NEGATIVE && rand () % 2)
155 // weight = -weight;
156 // double capacity = floor (MAX_CAPACITY*distribution_nrand (dist));
157 // gsl_matrix_set (g -> adjecency_matrix, i, j + i + 1, 1);
158 // gsl_matrix_set (g -> weight_matrix, i, j + i + 1, weight);
159 // gsl_matrix_set (g -> capacity_matrix, i, j + i + 1, capacity);
160 // }
161 // }
162
163 for (size_t k = 0; k < m || !is_graph_connected (g); ++k) {
164 size_t r = floor (n*n*distribution_nrand (dist)) + 1;
165 // size_t r = floor (n*n*(rand ()/(double) RAND_MAX)) + 1;
166 ssize_t i = floor ((r - 1)/(double) n);
167 ssize_t j = (r - 1) % n;
168 // printf ("%2zu %zd %zd\n", r, i, j);
169
170 if (i < j && !gsl_matrix_get (g->adjecency_matrix, i, j)) {
171 double weight = floor ((MAX_WEIGHT - 1)*distribution_nrand (dist)) + 1;
172 if (g->type == GRAPH_TYPE_POSITIVE_NEGATIVE && rand () % 2)
173 weight = -weight;
174 double capacity = floor (MAX_CAPACITY*distribution_nrand (dist));
175 gsl_matrix_set (g -> adjecency_matrix, i, j, 1);
176 gsl_matrix_set (g -> weight_matrix, i, j, weight);
177 gsl_matrix_set (g -> capacity_matrix, i, j, capacity);
178 } else m += 1;
179 }
180}
181
182size_t *
183dijkstra_positive (GRAPH g, size_t from, size_t to, bool max)
184{
185 struct dijkstra_node
186 {
187 double distance;
188 size_t previous;
189 bool visited;
190 };
191
192 size_t n = g->vertex_count;
193
194 double cost[MAX_VERTEX_COUNT][MAX_VERTEX_COUNT];
195 for (size_t i = 0; i < n; ++i) {
196 for (size_t j = 0; j < n; ++j) {
197 double val = gsl_matrix_get (g->weight_matrix, i, j);
198 if (val == 0)
199 cost[i][j] = max ? -INFINITY : INFINITY;
200 else
201 cost[i][j] = val;
202 }
203 }
204
205 struct dijkstra_node nodes[MAX_VERTEX_COUNT];
206 for (size_t i = 0; i < n; ++i) {
207 nodes[i] = (struct dijkstra_node) {
208 .distance = cost[from][i],
209 .previous = from,
210 .visited = false,
211 };
212 }
213 nodes[from].distance = 0;
214 nodes[from].visited = true;
215
216 for (size_t count = 1; count < n - 1; ++count) {
217 size_t next;
218 double best_distance = max ? -INFINITY : INFINITY;
219 for (size_t i = 0; i < n; i++) {
220 if ((max ? nodes[i].distance > best_distance
221 : nodes[i].distance < best_distance)
222 && !nodes[i].visited) {
223 best_distance = nodes[i].distance;
224 next = i;
225 }
226 }
227
228 nodes[next].visited = true;
229 for (size_t i = 0; i < n; ++i) {
230 if (!nodes[i].visited) {
231 double distance = best_distance + cost[next][i];
232 if ((max ? distance > nodes[i].distance
233 : distance < nodes[i].distance)) {
234 nodes[i].distance = distance;
235 nodes[i].previous = next;
236 }
237 }
238 }
239 }
240
241 printf ("DISTANCE VECTOR: (%.f", nodes[0].distance);
242 for (size_t i = 0; i < n; ++i) {
243 printf (" %.f", nodes[i].distance);
244 } puts (")");
245
246 if (nodes[to].distance == (max ? -INFINITY : INFINITY)) {
247 printf ("NO PATH FROM %zu TO %zu\n", from, to);
248 return NULL;
249 }
250
251 size_t len = 1;
252 for (size_t i = to; i != from; i = nodes[i].previous)
253 len += 1;
254
255 size_t *path = malloc ((len + 1)*sizeof (*path));
256 size_t v = len;
257 path[v--] = n;
258 for (size_t i = to; i != from; i = nodes[i].previous)
259 path[v--] = i;
260 path[v] = from;
261
262 printf ("PATH: %zu", path[0]);
263 for (size_t i = 1; path[i] != n; ++i)
264 printf (" -> %zu", path[i]);
265 putchar ('\n');
266
267 return path;
268}
269
270size_t *
271floyd_warshall (GRAPH g, size_t from, size_t to, bool max)
272{
273 const size_t n = g->vertex_count;
274 double dist[MAX_VERTEX_COUNT][MAX_VERTEX_COUNT];
275 size_t next[MAX_VERTEX_COUNT][MAX_VERTEX_COUNT];
276
277 // initialize dist and next
278 for (size_t u = 0; u < n; ++u) {
279 for (size_t v = 0; v < n; ++v) {
280 if (gsl_matrix_get (g->weight_matrix, u, v) == 0) {
281 dist[u][v] = max ? -INFINITY : INFINITY;
282 next[u][v] = n;
283 } else {
284 dist[u][v] = gsl_matrix_get (g->weight_matrix, u, v);
285 next[u][v] = v;
286 }
287 dist[v][v] = 0;
288 next[v][v] = v;
289 }
290 }
291
292 // compute dist and next
293 for (size_t k = 0; k < n; ++k) {
294 for (size_t i = 0; i < n; ++i) {
295 for (size_t j = 0; j < n; ++j) {
296 if (max ? dist[i][j] < dist[i][k] + dist[k][j]
297 : dist[i][j] > dist[i][k] + dist[k][j]) {
298 dist[i][j] = dist[i][k] + dist[k][j];
299 next[i][j] = next[i][k];
300 }
301 }
302 }
303 }
304
305 puts ("DISTANCE MATRIX:");
306 printf (" ");
307 for (size_t j = 0; j < n; j++)
308 printf (" [%zu]", j);
309 putchar ('\n');
310 for (size_t i = 0; i < n; i++) {
311 printf ("[%zu]", i);
312 for (size_t j = 0; j < n; j++)
313 printf ("%4g", dist[i][j]);
314 putchar ('\n');
315 }
316
317 // find path
318 if (next[from][to] == n) {
319 printf ("NO PATH FROM %zu TO %zu\n", from, to);
320 return NULL;
321 }
322
323 size_t len = 1;
324 for (size_t v = from; v != to; v = next[v][to])
325 len += 1;
326
327 size_t *path = malloc ((len + 1)*sizeof (*path));
328 size_t e = 0;
329 for (size_t v = from; v != to; v = next[v][to])
330 path[e++] = v;
331 path[e++] = to;
332 path[e] = n;
333
334 printf ("PATH: %zu", path[0]);
335 for (size_t i = 1; path[i] != n; ++i)
336 printf (" -> %zu", path[i]);
337 putchar ('\n');
338
339 return path;
340}
341
342void
343graph_unhighlight (GRAPH g)
344{
345 free (g->highlighted_path);
346 g->highlighted_path = NULL;
347 DYNAMIC_ARRAY_RESET (g->highlighted_edges);
348}
349
350void
351graph_edge_set (GRAPH g, size_t from, size_t to,
352 double weight, double capacity)
353{
354 gsl_matrix_set (g -> adjecency_matrix, from, to, weight && capacity);
355 gsl_matrix_set (g -> weight_matrix, from, to, weight);
356 gsl_matrix_set (g -> capacity_matrix, from, to, capacity);
357}
358
359void
360print_graph (GRAPH g)
361{
362 puts ("ADJECENCY_MATRIX:");
363 print_gsl_matrix (g->adjecency_matrix);
364 puts ("WEIGHT/COST_MATRIX:");
365 print_gsl_matrix (g->weight_matrix);
366 puts ("CAPACITY_MATRIX:");
367 print_gsl_matrix (g->capacity_matrix);
368}
369
370bool
371are_vertices_connected (gsl_matrix *g, size_t from, size_t to)
372{
373 size_t n = g->size1;
374 struct stack {
375 size_t *items;
376 size_t count;
377 size_t capacity;
378 };
379
380 struct stack stack1 = {0}; DYNAMIC_ARRAY_ENSURE_CAPACITY (stack1, n);
381 struct stack stack2 = {0}; DYNAMIC_ARRAY_ENSURE_CAPACITY (stack2, n);
382 struct stack *front_stack = &stack1;
383 struct stack *back_stack = &stack2;
384 DYNAMIC_ARRAY_APPEND (*front_stack, from);
385 while (front_stack->count) {
386 DYNAMIC_ARRAY_RESET (*back_stack);
387 DYNAMIC_ARRAY_FOREACH (v, *front_stack) {
388 size_t vertex = *v;
389 for (size_t i = 0; i < n; ++i) {
390 if (gsl_matrix_get (g, vertex, i)) {
391 if (to == i) return true;
392 DYNAMIC_ARRAY_APPEND (*back_stack, i);
393 }
394 }
395 } SWAP (front_stack, back_stack);
396 }
397
398 return false;
399}
400
401void
402highlight_graph (GRAPH g, const gsl_matrix *m)
403{
404 DYNAMIC_ARRAY_RESET (g->highlighted_edges);
405 size_t n = g->vertex_count;
406 for (size_t i = 0; i < n; ++i) {
407 for (size_t j = 0; j < n; ++j) {
408 if (gsl_matrix_get (m, i, j))
409 DYNAMIC_ARRAY_APPEND (g->highlighted_edges, ((struct edge) {i, j}));
410 }
411 }
412}
413
414// https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm
415// https://algorithms.discrete.ma.tum.de/graph-algorithms/flow-ford-fulkerson/index_en.html
416double
417max_flow_ford_fulkerson (GRAPH g, size_t source, size_t sink)
418{
419 size_t n = g->vertex_count;
420 size_t parents[MAX_VERTEX_COUNT];
421 for (size_t i = 0; i < MAX_VERTEX_COUNT; ++i)
422 parents[i] = n;
423
424 gsl_matrix *residual_capacity_matrix = gsl_matrix_alloc (n, n);
425 gsl_matrix_memcpy (residual_capacity_matrix, g->capacity_matrix);
426 double max_flow = 0;
427
428 struct stack {
429 size_t *items;
430 size_t count;
431 size_t capacity;
432 };
433
434 DYNAMIC_ARRAY_RESET (g->highlighted_edges);
435 struct stack stack1 = {0}; DYNAMIC_ARRAY_ENSURE_CAPACITY (stack1, n);
436 struct stack stack2 = {0}; DYNAMIC_ARRAY_ENSURE_CAPACITY (stack2, n);
437 struct stack *front_stack = &stack1;
438 struct stack *back_stack = &stack2;
439 for (;;) {
440 bool visited[MAX_VERTEX_COUNT] = { false };
441 DYNAMIC_ARRAY_RESET (*front_stack);
442 DYNAMIC_ARRAY_APPEND (*front_stack, source);
443 visited[source] = true;
444 while (front_stack->count) {
445 DYNAMIC_ARRAY_RESET (*back_stack);
446 DYNAMIC_ARRAY_FOREACH (v, *front_stack) {
447 size_t vertex = *v;
448 for (size_t i = 0; i < n; ++i) {
449 if (!visited[i] &&
450 gsl_matrix_get (residual_capacity_matrix, vertex, i) > 0) {
451 parents[i] = vertex;
452 if (sink == i) goto found;
453 DYNAMIC_ARRAY_APPEND (*back_stack, i);
454 visited[i] = true;
455 }
456 }
457 } SWAP(front_stack, back_stack);
458 }
459 break;
460
461 found: {
462 double path_flow = INFINITY;
463 for (size_t v = sink; v != source; v = parents[v]) {
464 size_t u = parents[v];
465 DYNAMIC_ARRAY_APPEND (g->highlighted_edges, ((struct edge) {u, v}));
466 path_flow = fmin (path_flow, gsl_matrix_get (residual_capacity_matrix,
467 u, v));
468 }
469
470 for (size_t v = sink; v != source; v = parents[v]) {
471 size_t u = parents[v];
472 *gsl_matrix_ptr (residual_capacity_matrix, u, v) -= path_flow;
473 *gsl_matrix_ptr (residual_capacity_matrix, v, u) += path_flow;
474 }
475
476 max_flow += path_flow;
477 }
478 }
479
480 gsl_matrix_free (residual_capacity_matrix);
481
482 return max_flow;
483}
484
485// https://tema.spbstu.ru/userfiles/files/courses/2019-theory_graph/TG9.pdf
486double
487min_cost_flow (GRAPH g, size_t s, size_t t, double required_flow)
488{
489 size_t n = g->vertex_count;
490 gsl_matrix *residual_capacity_matrix = gsl_matrix_alloc (n, n);
491 gsl_matrix_memcpy (residual_capacity_matrix, g->capacity_matrix);
492 gsl_matrix *residual_cost_matrix = gsl_matrix_alloc (n, n);
493 gsl_matrix_memcpy (residual_cost_matrix, g->weight_matrix);
494 gsl_matrix *result_flow_matrix = gsl_matrix_calloc (n, n);
495 double costs[MAX_VERTEX_COUNT];
496 size_t parents[MAX_VERTEX_COUNT];
497 double flow;
498 double min_cost;
499
500 DYNAMIC_ARRAY_RESET (g->highlighted_edges);
501 while (flow < required_flow) {
502 // Bellman-Ford
503 for (size_t i = 0; i < n; ++i) {
504 costs[i] = INFINITY;
505 parents[i] = n;
506 } costs[s] = 0;
507 for (size_t i = 0; i < n - 1; ++i) {
508 for (size_t u = 0; u < n; ++u) {
509 for (size_t v = 0; v < n; ++v) {
510 if (!gsl_matrix_get (g->adjecency_matrix, u, v)) continue;
511 double cost = gsl_matrix_get (residual_cost_matrix, u, v);
512 if (costs[u] != INFINITY &&
513 cost != INFINITY &&
514 costs[u] + cost < costs[v]) {
515 costs[v] = costs[u] + cost;
516 parents[v] = u;
517 }
518 }
519 }
520 }
521
522 // If no path
523 if (parents[t] == n) return NAN;
524
525 // Find max flow
526 double path_flow = INFINITY;
527 for (size_t v = t; v != s; v = parents[v]) {
528 size_t u = parents[v];
529 path_flow = fmin (path_flow,
530 gsl_matrix_get (residual_capacity_matrix, u, v));
531 }
532
533 // Apply flow constraint
534 path_flow = fmin (path_flow, required_flow - flow);
535
536 // Update costs and capacities
537 for (int v = t; v != s; v = parents[v]) {
538 int u = parents[v];
539 DYNAMIC_ARRAY_APPEND (g->highlighted_edges, ((struct edge) {u, v}));
540 *gsl_matrix_ptr (residual_capacity_matrix, u, v) -= path_flow;
541 *gsl_matrix_ptr (result_flow_matrix, u, v) += path_flow;
542 min_cost += path_flow*gsl_matrix_get (residual_cost_matrix, u, v);
543
544 if (gsl_matrix_get (residual_capacity_matrix, u, v) == 0)
545 gsl_matrix_set (residual_cost_matrix, u, v, INFINITY);
546 }
547
548 flow += path_flow;
549 }
550
551 puts ("FLOW_MATRIX:");
552 print_gsl_matrix (result_flow_matrix);
553
554 gsl_matrix_free (residual_capacity_matrix);
555 gsl_matrix_free (residual_cost_matrix);
556 gsl_matrix_free (result_flow_matrix);
557
558 return min_cost;
559}
560
561size_t
562count_spanning_trees (GRAPH g)
563{
564 size_t n = g->vertex_count;
565 gsl_matrix *kirchhoff_matrix = gsl_matrix_calloc (n, n);
566
567 for (size_t i = 0; i < n; i++) {
568 for (size_t j = i; j < n; j++) {
569 if (gsl_matrix_get (g->adjecency_matrix, i, j) ||
570 gsl_matrix_get (g->adjecency_matrix, j, i)) {
571 *gsl_matrix_ptr (kirchhoff_matrix, i, i) += 1;
572 *gsl_matrix_ptr (kirchhoff_matrix, j, j) += 1;
573 *gsl_matrix_ptr (kirchhoff_matrix, i, j) = -1;
574 *gsl_matrix_ptr (kirchhoff_matrix, j, i) = -1;
575 }
576 }
577 }
578
579 printf ("KIRCHHOFF_MATRIX:\n");
580 print_gsl_matrix (kirchhoff_matrix);
581
582 size_t count = gsl_matrix_cofactor (kirchhoff_matrix, 0, 0);
583 gsl_matrix_free (kirchhoff_matrix);
584
585 return count;
586}
587
588// https://en.wikipedia.org/wiki/Prim%27s_algorithm
589gsl_matrix *
590min_weight_spannig_tree_prim (GRAPH g)
591{
592 size_t count = 0;
593 double total_weight = 0;
594 size_t n = g->vertex_count;
595 gsl_matrix *remaining_graph = gsl_matrix_alloc (n, n);
596
597 for (size_t i = 0; i < n; ++i) {
598 for (int j = 0; j < n; ++j) {
599 double weight = gsl_matrix_get (g->weight_matrix, i, j);
600 gsl_matrix_set (remaining_graph, i, j, weight == 0 ? INFINITY : weight);
601 }
602 }
603
604 gsl_matrix *spanning_tree = gsl_matrix_calloc (n, n);
605 bool visited[MAX_VERTEX_COUNT] = {0};
606 size_t spanning_tree_vertex_count = 0;
607 while (spanning_tree_vertex_count < g->vertex_count) {
608 double minimum_weight = INFINITY;
609 size_t a;
610 size_t b;
611 for (size_t i = 0 ; i < n; ++i) {
612 for (size_t j = 0; j < n; ++j) {
613 count += 1;
614 double weight = gsl_matrix_get (remaining_graph, i, j);
615 if (weight < minimum_weight &&
616 ((visited[i] != visited[j]) ||
617 spanning_tree_vertex_count == 0)) {
618 minimum_weight = weight;
619 a = i;
620 b = j;
621 }
622 }
623 }
624
625 if (!visited[a] || !visited[b]) {
626 if (!visited[a]) {
627 spanning_tree_vertex_count += 1;
628 visited[a] = true;
629 }
630
631 if (!visited[b]) {
632 spanning_tree_vertex_count += 1;
633 visited[b] = true;
634 }
635
636 total_weight += minimum_weight;
637 }
638
639 gsl_matrix_set (remaining_graph, a, b, INFINITY);
640 gsl_matrix_set (remaining_graph, b, a, INFINITY);
641 gsl_matrix_set (spanning_tree, a, b, minimum_weight);
642 gsl_matrix_set (spanning_tree, b, a, minimum_weight);
643 printf ("%zu %zu = %f\n", a, b, minimum_weight);
644 } gsl_matrix_free (remaining_graph);
645
646 printf ("MINIMUM_SPANNING_TREE_WEIGHTS:\n");
647 print_gsl_matrix (spanning_tree);
648 printf ("TOTAL_WEIGHT: %.0lf\n", total_weight);
649 printf ("ITERATION_COUNT: %zu\n", count);
650
651 return spanning_tree;
652}
653
654// https://en.wikipedia.org/wiki/Bor%C5%AFvka's_algorithm
655gsl_matrix *
656min_weight_spannig_tree_boruvka (GRAPH g)
657{
658 size_t n = g->vertex_count;
659 gsl_matrix *spanning_tree = gsl_matrix_calloc (n, n);
660
661 struct edge_weight {
662 size_t from;
663 size_t to;
664 double weight;
665 };
666
667 size_t number_of_components = g->vertex_count;
668 struct edge_weight lightest_edges[MAX_VERTEX_COUNT];
669 size_t parent_components[MAX_VERTEX_COUNT];
670 size_t next_components[MAX_VERTEX_COUNT];
671
672 for (size_t i = 0; i < n; ++i)
673 parent_components[i] = i;
674
675 size_t count = 0;
676 double min_weight = 0;
677 while (number_of_components > 1) {
678 for (size_t i = 0; i < n; ++i)
679 lightest_edges[i] = (struct edge_weight) { 0, 0, .weight = INFINITY };
680
681 for (size_t u = 0; u < n; ++u) {
682 for (size_t v = 0; v < n; ++v) {
683 if (!gsl_matrix_get (g->adjecency_matrix, u, v)) continue;
684 count += 1;
685 double weight =
686 gsl_matrix_get (g->weight_matrix, u, v) +
687 gsl_matrix_get (g->weight_matrix, v, u);
688
689 if (parent_components[u] != parent_components[v]) {
690 if (lightest_edges[parent_components[u]].weight > weight) {
691 lightest_edges[parent_components[u]].weight = weight;
692 lightest_edges[parent_components[u]].from = u;
693 lightest_edges[parent_components[u]].to = v;
694 }
695 if (lightest_edges[parent_components[v]].weight > weight) {
696 lightest_edges[parent_components[v]].weight = weight;
697 lightest_edges[parent_components[v]].from = u;
698 lightest_edges[parent_components[v]].to = v;
699 }
700 }
701 }
702 }
703
704 // Adding selected edges and combining components
705 for (size_t i = 0; i < number_of_components; ++i) {
706 count++;
707
708 double weight = lightest_edges[i].weight;
709 size_t u = lightest_edges[i].from;
710 size_t v = lightest_edges[i].to;
711
712 if (weight == INFINITY) continue;
713
714 if (!gsl_matrix_get (spanning_tree, u, v)) {
715 min_weight += weight;
716 gsl_matrix_set (spanning_tree, u, v, weight);
717 gsl_matrix_set (spanning_tree, v, u, weight);
718 }
719
720 // Do the join
721 if (parent_components[u] != parent_components[v]) {
722 number_of_components -= 1;
723 size_t component_to_remove = parent_components[v];
724 for (size_t j = 0; j < n; j++) {
725 if (parent_components[j] == component_to_remove) {
726 parent_components[j] = parent_components[u];
727 }
728 }
729 }
730 }
731 }
732
733 printf ("MINIMUM_SPANNING_TREE_WEIGHTS:\n");
734 print_gsl_matrix (spanning_tree);
735 printf ("TOTAL_WEIGHT: %.0lf\n", min_weight);
736 printf ("ITERATION_COUNT: %zu\n", count);
737
738 return spanning_tree;
739}
740
741void
742prufer_code (gsl_matrix *spanning_tree_matrix)
743{
744 size_t n = spanning_tree_matrix->size1;
745
746 gsl_matrix *tree = gsl_matrix_alloc (n, n);
747 gsl_matrix_memcpy (tree, spanning_tree_matrix);
748 struct {
749 size_t *items;
750 size_t count;
751 size_t capacity;
752 } prufer = {0};
753 struct {
754 size_t *items;
755 size_t count;
756 size_t capacity;
757 } weights = {0};
758
759 for (size_t i = 0; i < n - 1; ++i) {
760 // Find hanging vertex with the lowest number
761 size_t v, u;
762 for (size_t j = 0; j < n; ++j) {
763 size_t deg = 0;
764 for (size_t k = 0; k < n; ++k) {
765 if (gsl_matrix_get (tree, j, k) > 0) {
766 deg += 1;
767 u = k;
768 }
769 }
770
771 if (deg == 1) {
772 v = j;
773 break;
774 }
775 }
776
777 // Encode the only vertex neighbouring `v'
778 DYNAMIC_ARRAY_APPEND (prufer, u);
779 DYNAMIC_ARRAY_APPEND (weights, gsl_matrix_get (tree, u, v));
780
781 // "Pop"
782 gsl_matrix_set (tree, u, v, 0);
783 gsl_matrix_set (tree, v, u, 0);
784 }
785
786 printf ("MINIMAL_SPANNING_TREE:\n");
787 print_gsl_matrix (spanning_tree_matrix);
788
789 printf ("PRUFER_CODE: ");
790 DYNAMIC_ARRAY_FOREACH (p, prufer)
791 printf ("%zu ", *p);
792 printf ("\n");
793
794 printf ("SAVED_WEIGHTS: ");
795 DYNAMIC_ARRAY_FOREACH (w, weights)
796 printf ("%zu ", *w);
797 printf ("\n");
798
799 // Decoding
800 gsl_matrix *decoded_tree = gsl_matrix_calloc (n, n);
801
802 struct {
803 size_t *items;
804 size_t count;
805 size_t capacity;
806 } not_used_vertices = {0};
807 for (size_t i = 0; i < n; ++i)
808 DYNAMIC_ARRAY_APPEND (not_used_vertices, i);
809
810 for (size_t i = 0; i < n - 1; ++i) {
811 // Picking unused vertex v with the lowest number not in the rest
812 // of the Prufer code
813 size_t v;
814 for (size_t j = 0; j < not_used_vertices.count; ++j) {
815 bool met = false;
816 for (size_t k = i; k < n - 1; ++k) {
817 if (not_used_vertices.items[j] == prufer.items[k]) {
818 met = true;
819 break;
820 }
821 }
822
823 if (!met) {
824 v = not_used_vertices.items[j];
825
826 // Remove now used
827 DYNAMIC_ARRAY_ERASE (not_used_vertices, j);
828 break;
829 }
830 }
831
832 // Add edge
833 gsl_matrix_set (decoded_tree, v, prufer.items[i], weights.items[i]);
834 gsl_matrix_set (decoded_tree, prufer.items[i], v, weights.items[i]);
835 }
836
837 printf ("DECODED_MINIMAL_SPANNING_TREE:\n");
838 print_gsl_matrix (decoded_tree);
839
840 for (size_t i = 0; i < n; ++i) {
841 for (size_t j = 0; j < n; ++j) {
842 if (gsl_matrix_get (spanning_tree_matrix, i, j) !=
843 gsl_matrix_get (decoded_tree, i, j)) {
844 printf ("Error, matrices are not the same!\n");
845 return;
846 }
847 }
848 }
849 printf("Success, matrices the same!\n");
850
851 gsl_matrix_free (tree);
852 gsl_matrix_free (decoded_tree);
853}
854
855bool
856is_graph_eulerian (GRAPH g)
857{
858 size_t n = g->vertex_count;
859
860 if (n <= 2)
861 return false;
862
863 // Check if degree of all vertices is even
864 for (size_t i = 0; i < n; ++i) {
865 size_t degree = 0;
866 for (size_t j = 0; j < n; ++j) {
867 if (gsl_matrix_get (g->adjecency_matrix, i, j) ||
868 gsl_matrix_get (g->adjecency_matrix, j, i))
869 degree += 1;
870 }
871
872 if (degree % 2 != 0)
873 return false;
874 }
875
876 return true;
877}
878
879// https://tema.spbstu.ru/userfiles/files/courses/2019-theory_graph/TG12.pdf
880// page 33
881bool
882is_graph_hamiltonian (GRAPH g)
883{
884 size_t n = g->vertex_count;
885
886 // Find neighbours
887 struct {
888 size_t *items;
889 size_t count;
890 size_t capacity;
891 } neighbours[MAX_VERTEX_COUNT] = {0};
892 for (size_t i = 0; i < n; ++i) {
893 for (size_t j = 0; j < n; ++j) {
894 if (gsl_matrix_get (g->adjecency_matrix, i, j) ||
895 gsl_matrix_get (g->adjecency_matrix, j, i)) {
896 DYNAMIC_ARRAY_APPEND (neighbours[i], j);
897 }
898 }
899 }
900
901 struct {
902 size_t *items;
903 size_t count;
904 size_t capacity;
905 } not_visited_neighbours[MAX_VERTEX_COUNT] = {0};
906
907 struct {
908 size_t *items;
909 size_t count;
910 size_t capacity;
911 } path = {0};
912
913 size_t a = 0;
914 DYNAMIC_ARRAY_APPEND (path, a);
915 DYNAMIC_ARRAY_COPY_OTHER (not_visited_neighbours[a], neighbours[a]);
916
917 while (path.count) {
918 size_t x = path.items[path.count - 1];
919
920 if (not_visited_neighbours[x].count) {
921 size_t y = not_visited_neighbours[x].items[not_visited_neighbours[x].count - 1];
922 not_visited_neighbours[x].count -= 1;
923
924 bool y_in_path = false;
925 DYNAMIC_ARRAY_FOREACH(p, path) {
926 if (*p == y) {
927 y_in_path = true;
928 break;
929 }
930 }
931
932 if (!y_in_path) {
933 DYNAMIC_ARRAY_APPEND (path, y);
934 DYNAMIC_ARRAY_COPY_OTHER (not_visited_neighbours[y], neighbours[y]);
935 if (path.count == n &&
936 (gsl_matrix_get (g->adjecency_matrix, a, y) ||
937 gsl_matrix_get (g->adjecency_matrix, y, a))) {
938 return true;
939 }
940 }
941 } else {
942 path.count -= 1; // pop back
943 }
944 }
945
946 return false;
947}
948
949void
950graph_ensure_eulerian (GRAPH g)
951{
952 size_t n = g->vertex_count;
953
954 if (n <= 2) {
955 printf ("Not enough vertices for an Euler cycle (<3)\n");
956 return;
957 }
958
959 if (is_graph_eulerian (g)) {
960 printf ("Graph is Eulerian, no modifications needed\n");
961 } else {
962 // Find vertices with odd degrees
963 struct {
964 size_t *items;
965 size_t count;
966 size_t capacity;
967 } odd_degree_vertices = {0};
968 for (size_t i = 0; i < n; ++i) {
969 size_t degree = 0;
970 for (size_t j = 0; j < n; ++j) {
971 if (gsl_matrix_get (g->adjecency_matrix, i, j) ||
972 gsl_matrix_get (g->adjecency_matrix, j, i))
973 degree += 1;
974 }
975 if (degree % 2 != 0)
976 DYNAMIC_ARRAY_APPEND (odd_degree_vertices, i);
977 }
978
979 size_t first;
980
981 // Join vertices with odd degrees
982 add_edge:
983 if (odd_degree_vertices.count)
984 first = odd_degree_vertices.items[0];
985
986 // Find pair
987 for (size_t second_index = 1; second_index < odd_degree_vertices.count; ++second_index) {
988 size_t second = odd_degree_vertices.items[second_index];
989
990 // Help keep acyclic
991 size_t i = first < second ? first : second;
992 size_t j = first > second ? first : second;
993
994 // Add edge if there isn't
995 if (gsl_matrix_get (g->adjecency_matrix, i, j) == 0 &&
996 gsl_matrix_get (g->adjecency_matrix, j, i) == 0) {
997 double new_weight = floor (rand ()/(double) RAND_MAX*(MAX_WEIGHT - 1)) + 1;
998 double new_capacity = floor (rand ()/(double) RAND_MAX*(MAX_CAPACITY - 1)) + 1;
999 graph_edge_set (g, i, j, new_weight, new_capacity);
1000
1001 printf ("Added edge: %zu -> %zu (%.0lf/%.0lf)\n", i, j, new_capacity, new_weight);
1002
1003 // Update
1004 DYNAMIC_ARRAY_ERASE (odd_degree_vertices, second_index);
1005 DYNAMIC_ARRAY_ERASE (odd_degree_vertices, 0);
1006
1007 goto add_edge;
1008 }
1009 }
1010
1011 // If edge addition helped only partially, delete edges
1012 remove_edge:
1013 if (odd_degree_vertices.count)
1014 first = odd_degree_vertices.items[0];
1015
1016 for (size_t second_index = 1; second_index < odd_degree_vertices.count; ++second_index) {
1017 size_t second = odd_degree_vertices.items[second_index];
1018
1019 size_t i = first < second ? first : second;
1020 size_t j = first > second ? first : second;
1021
1022 // Don't delete edge if it will make graph unconnected
1023 size_t degree_i = 0;
1024 size_t degree_j = 0;
1025 for (size_t k = 0; k < n; ++k) {
1026 if (gsl_matrix_get (g->adjecency_matrix, i, k) ||
1027 gsl_matrix_get (g->adjecency_matrix, k, i))
1028 degree_i += 1;
1029 if (gsl_matrix_get (g->adjecency_matrix, j, k) ||
1030 gsl_matrix_get (g->adjecency_matrix, k, j))
1031 degree_j += 1;
1032 }
1033
1034 if (degree_i == 1 &&
1035 degree_j == 1)
1036 continue;
1037
1038 gsl_matrix_set (g -> adjecency_matrix, i, j, 0);
1039 gsl_matrix_set (g -> weight_matrix, i, j, 0);
1040 gsl_matrix_set (g -> capacity_matrix, i, j, 0);
1041 gsl_matrix_set (g -> adjecency_matrix, j, i, 0);
1042 gsl_matrix_set (g -> weight_matrix, j, i, 0);
1043 gsl_matrix_set (g -> capacity_matrix, j, i, 0);
1044
1045 printf ("Removed edge: %zu -> %zu\n", i, j);
1046
1047 // Update
1048 DYNAMIC_ARRAY_ERASE (odd_degree_vertices, second_index);
1049 DYNAMIC_ARRAY_ERASE (odd_degree_vertices, 0);
1050
1051 goto remove_edge;
1052 }
1053
1054 if (odd_degree_vertices.count)
1055 printf ("Unable to make graph Eulerian\n");
1056
1057 printf ("ADJECENCY_MATRIX:\n");
1058 print_gsl_matrix (g->adjecency_matrix);
1059 }
1060
1061 // Find Euler cycles
1062
1063 // Find neighbours
1064 struct {
1065 size_t *items;
1066 size_t count;
1067 size_t capacity;
1068 } neighbours[MAX_VERTEX_COUNT] = {0};
1069 for (size_t i = 0; i < n; ++i) {
1070 for (size_t j = 0; j < n; ++j) {
1071 if (gsl_matrix_get (g->adjecency_matrix, i, j) ||
1072 gsl_matrix_get (g->adjecency_matrix, j, i)) {
1073 DYNAMIC_ARRAY_APPEND (neighbours[i], j);
1074 }
1075 }
1076 }
1077
1078 // https://tema.spbstu.ru/userfiles/files/courses/2019-theory_graph/TG12.pdf
1079 // Page 27
1080 struct {
1081 size_t *items;
1082 size_t count;
1083 size_t capacity;
1084 } euler_cycle = {0};
1085 struct {
1086 size_t *items;
1087 size_t count;
1088 size_t capacity;
1089 } s = {0};
1090 DYNAMIC_ARRAY_APPEND (s, 0);
1091
1092 while (s.count) {
1093 size_t v = s.items[s.count - 1];
1094
1095 if (!neighbours[v].count) {
1096 s.count -= 1;
1097 DYNAMIC_ARRAY_APPEND (euler_cycle, v);
1098 } else {
1099 size_t u = neighbours[v].items[0];
1100 DYNAMIC_ARRAY_APPEND (s, u);
1101
1102 // Removing
1103 DYNAMIC_ARRAY_ERASE (neighbours[v], 0);
1104 for (size_t i = 0; i < neighbours[u].count; ++i) {
1105 if (neighbours[u].items[i] == v) {
1106 DYNAMIC_ARRAY_ERASE (neighbours[u], i);
1107 i -= 1;
1108 }
1109 }
1110 }
1111 }
1112
1113 printf ("EULER_CYCLE: ");
1114 DYNAMIC_ARRAY_FOREACH (vertex, euler_cycle) {
1115 if (vertex != euler_cycle.items)
1116 printf (" -> ");
1117 printf ("%zu", *vertex);
1118 } printf ("\n");
1119 DYNAMIC_ARRAY_APPEND (euler_cycle, n);
1120 free (g->highlighted_path);
1121 g->highlighted_path = euler_cycle.items;
1122
1123 for (size_t i = 0; i < MAX_VERTEX_COUNT; ++i)
1124 DYNAMIC_ARRAY_FREE (neighbours[i]);
1125 DYNAMIC_ARRAY_FREE (s);
1126 // DYNAMIC_ARRAY_FREE (euler_cycle);
1127}
1128
1129void
1130graph_ensure_hamiltonian (GRAPH g)
1131{
1132 size_t n = g->vertex_count;
1133
1134 if (n <= 2) {
1135 printf ("Not enough vertices for an Hamilton cycle (<3)\n");
1136 return;
1137 }
1138
1139 if (is_graph_hamiltonian (g)) {
1140 printf ("Graph is Hamiltonian, no modifications needed\n");
1141 } else {
1142 struct {
1143 struct vertex_degree {
1144 size_t vertex;
1145 size_t degree;
1146 } *items;
1147 size_t count;
1148 size_t capacity;
1149 } vertex_degree_pairs = {0};
1150 for (size_t i = 0; i < n; ++i) {
1151 size_t degree = 0;
1152 for (size_t j = 0; j < n; ++j) {
1153 if (gsl_matrix_get (g->adjecency_matrix, i, j) ||
1154 gsl_matrix_get (g->adjecency_matrix, j, i))
1155 degree += 1;
1156 }
1157
1158 DYNAMIC_ARRAY_APPEND (vertex_degree_pairs, ((struct vertex_degree) {i, degree}));
1159 }
1160
1161 // Add edges to vertices with low degree
1162 do {
1163 size_t min_idx = 0;
1164 for (size_t i = 1; i < n; ++i) {
1165 if (vertex_degree_pairs.items[i].degree <
1166 vertex_degree_pairs.items[min_idx].degree)
1167 min_idx = i;
1168 }
1169
1170 size_t vertex1 = vertex_degree_pairs.items[min_idx].vertex;
1171 size_t degree1 = vertex_degree_pairs.items[min_idx].degree;
1172 DYNAMIC_ARRAY_ERASE (vertex_degree_pairs, min_idx);
1173
1174 for (size_t k = 0; k < n; ++k) {
1175 size_t min_idx = 0;
1176 for (size_t i = 1; i < n; ++i) {
1177 if (vertex_degree_pairs.items[min_idx].vertex != vertex1 &&
1178 vertex_degree_pairs.items[i].degree <
1179 vertex_degree_pairs.items[min_idx].degree)
1180 min_idx = i;
1181 }
1182 size_t vertex2 = vertex_degree_pairs.items[min_idx].vertex;
1183 size_t degree2 = vertex_degree_pairs.items[min_idx].degree;
1184
1185 if (!gsl_matrix_get (g->adjecency_matrix, vertex1, vertex2) &&
1186 !gsl_matrix_get (g->adjecency_matrix, vertex2, vertex1)) {
1187 size_t i = vertex1 < vertex2 ? vertex1 : vertex2;
1188 size_t j = vertex1 > vertex2 ? vertex1 : vertex2;
1189
1190 double new_weight = floor (rand ()/(double) RAND_MAX*(MAX_WEIGHT - 1)) + 1;
1191 double new_capacity = floor (rand ()/(double) RAND_MAX*(MAX_CAPACITY - 1)) + 1;
1192 graph_edge_set (g, i, j, new_weight, new_capacity);
1193
1194 printf ("Added edge: %zu -> %zu (%.0lf/%.0lf)\n", i, j, new_capacity, new_weight);
1195
1196 DYNAMIC_ARRAY_ERASE (vertex_degree_pairs, min_idx);
1197 DYNAMIC_ARRAY_APPEND (vertex_degree_pairs, ((struct vertex_degree) {vertex2, degree2 + 1}));
1198 break;
1199 }
1200 }
1201
1202 DYNAMIC_ARRAY_APPEND (vertex_degree_pairs, ((struct vertex_degree) {vertex1, degree1 + 1}));
1203 } while (!is_graph_hamiltonian(g));
1204 }
1205
1206 find_hamiltonian_cycles (g);
1207}
1208
1209struct path {
1210 size_t *items;
1211 size_t count;
1212 size_t capacity;
1213} typedef *PATH;
1214
1215void
1216find_hamiltonian_recursive (GRAPH g, FILE *fd, size_t *cycle_count,
1217 size_t start, PATH path,
1218 double path_weight, PATH min_path,
1219 double *min_path_weight)
1220{
1221 if (*cycle_count >= 1000000)
1222 return;
1223
1224 // Path is built
1225 if (path->count == g->vertex_count) {
1226 size_t last = path->items[path->count - 1];
1227 if (gsl_matrix_get (g->adjecency_matrix, last, start) ||
1228 gsl_matrix_get (g->adjecency_matrix, start, last)) {
1229 *cycle_count += 1;
1230
1231 path_weight +=
1232 gsl_matrix_get (g->weight_matrix, start, last) +
1233 gsl_matrix_get (g->weight_matrix, last, start);
1234 DYNAMIC_ARRAY_APPEND (*path, start);
1235 if (*min_path_weight > path_weight) {
1236 DYNAMIC_ARRAY_COPY_OTHER (*min_path, *path);
1237 *min_path_weight = path_weight;
1238 }
1239
1240 for (size_t i = 0; i < path->count; ++i)
1241 fprintf (fd, "%zu ", path->items[i]);
1242 fprintf (fd, "@ %.0lf\n", path_weight);
1243 path->count -= 1;
1244 }
1245 return;
1246 }
1247
1248 // Continue building
1249 for (size_t i = 0; i < g->vertex_count; ++i) {
1250 size_t last = path->items[path->count - 1];
1251 if (!gsl_matrix_get (g->adjecency_matrix, last, i) &&
1252 !gsl_matrix_get (g->adjecency_matrix, i, last))
1253 continue;
1254
1255 bool used = false;
1256 DYNAMIC_ARRAY_FOREACH (v, *path) {
1257 if (*v == i) {
1258 used = true;
1259 break;
1260 }
1261 } if (used) continue;
1262
1263 double new_weight = path_weight
1264 + gsl_matrix_get (g->weight_matrix, last, i)
1265 + gsl_matrix_get (g->weight_matrix, i, last);
1266 DYNAMIC_ARRAY_APPEND (*path, i);
1267 find_hamiltonian_recursive (g, fd, cycle_count, start, path, new_weight, min_path, min_path_weight);
1268 path->count -= 1;
1269 }
1270}
1271
1272void
1273find_hamiltonian_cycles (GRAPH g)
1274{
1275 const char *filename = "hamiltonian-cycles.txt";
1276
1277 if (g->vertex_count > 20) {
1278 printf ("Graph is too large (>20v), iterative algorithm will take too long\n");
1279 return;
1280 }
1281
1282 FILE *fd = fopen (filename, "w+");
1283
1284
1285 struct path path = {0};
1286 struct path min_path = {0};
1287 size_t cycle_count = 0;
1288 double min_path_weight = INFINITY;
1289 for (size_t i = 0; i < g->vertex_count; ++i) {
1290 DYNAMIC_ARRAY_RESET (path);
1291 DYNAMIC_ARRAY_APPEND (path, i);
1292 find_hamiltonian_recursive (g, fd, &cycle_count, i, &path, 0, &min_path, &min_path_weight);
1293 }
1294
1295 fclose (fd);
1296
1297 printf ("CYCLES_TOTAL: %zu\n", cycle_count);
1298 printf ("All cycles are saved here: %s\n", filename);
1299 printf ("Cycle with minimum weight (%.0lf): ", min_path_weight);
1300 DYNAMIC_ARRAY_FOREACH (vertex, min_path) {
1301 if (vertex != min_path.items)
1302 printf (" -> ");
1303 printf ("%zu", *vertex);
1304 } printf ("\n");
1305 DYNAMIC_ARRAY_APPEND (min_path, g->vertex_count);
1306 free (g->highlighted_path);
1307 g->highlighted_path = min_path.items;
1308
1309
1310 DYNAMIC_ARRAY_FREE (path);
1311}