diff options
| author | 2026-11-07 03:20:46 +0000 | |
|---|---|---|
| committer | 2026-11-07 04:21:37 +0000 | |
| commit | 556fea05affe6fe8866557d42b6c1ef6559d7880 (patch) | |
| tree | f8de5473f16e65fcbe10f280b4cf54ee7e32b5b7 /src | |
| download | lab-01-05-master.tar.gz | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.h | 66 | ||||
| -rw-r--r-- | src/distribution-test.c | 63 | ||||
| -rw-r--r-- | src/distribution.c | 57 | ||||
| -rw-r--r-- | src/distribution.h | 22 | ||||
| -rw-r--r-- | src/dynamic-array.h | 74 | ||||
| -rw-r--r-- | src/graph.c | 1311 | ||||
| -rw-r--r-- | src/graph.h | 71 | ||||
| -rw-r--r-- | src/main.c | 580 | ||||
| -rw-r--r-- | src/utils.c | 108 | ||||
| -rw-r--r-- | src/utils.h | 33 |
10 files changed, 2385 insertions, 0 deletions
diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..fa04908 --- /dev/null +++ b/src/config.h | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | #ifndef INCLUDED_CONFIG_H | ||
| 2 | #define INCLUDED_CONFIG_H | ||
| 3 | |||
| 4 | #define INITIAL_SCREEN_WIDTH 500 | ||
| 5 | #define INITIAL_SCREEN_HEIGHT 500 | ||
| 6 | #define WINDOW_TITLE "Graph visualization" | ||
| 7 | |||
| 8 | // // TODO(cmmm): compute | ||
| 9 | #define BUTTON_HEIGHT 30 | ||
| 10 | #define BUTTON_WIDTH 300 | ||
| 11 | #define PADDING 10 | ||
| 12 | |||
| 13 | #define VERTEX_RADIUS 20 | ||
| 14 | #define BASE_LINE_THICKNESS 6 | ||
| 15 | |||
| 16 | #define FONT_SIZE 20 | ||
| 17 | #define TEXT_SPACING 1 | ||
| 18 | |||
| 19 | #define INITIAL_VERTEX_COUNT 8 | ||
| 20 | |||
| 21 | #define MAX_VERTEX_COUNT ((size_t) 40) | ||
| 22 | #define MAX_WEIGHT ((double) 10) | ||
| 23 | #define MAX_CAPACITY ((double) MAX_WEIGHT) | ||
| 24 | |||
| 25 | #define DIST_N 10 | ||
| 26 | #define DIST_B 20 | ||
| 27 | #define DIST_R 1 | ||
| 28 | #define DIST_C 10 | ||
| 29 | |||
| 30 | #define MODEL_L ((double) 1.0) | ||
| 31 | #define MODEL_C_REP ((double) 1.0) | ||
| 32 | #define MODEL_C_SPRING ((double) 10.0) | ||
| 33 | #define MODEL_COOLING_FACTOR ((double) 0.9) | ||
| 34 | |||
| 35 | #define DARK_THEME | ||
| 36 | #if defined (LIGHT_THEME) | ||
| 37 | #define BACKGROUND_COLOR WHITE | ||
| 38 | #define VERTEX_COLOR RED | ||
| 39 | #define VERTEX_OUTLINE_COLOR MAROON | ||
| 40 | #define VERTEX_OUTLINE_HIGHLIGHTED_COLOR BLACK | ||
| 41 | #define VERTEX_TEXT_COLOR WHITE | ||
| 42 | #define EDGE_TEXT_COLOR BLACK | ||
| 43 | #define BUTTON_COLOR LIGHTGRAY | ||
| 44 | #define BUTTON_OUTLINE_COLOR GRAY | ||
| 45 | #define BUTTON_HOVER_COLOR MAROON | ||
| 46 | #define BUTTON_HOVER_OUTLINE_COLOR RED | ||
| 47 | #define BUTTON_TEXT_COLOR BLACK | ||
| 48 | #define BUTTON_TEXT_HOVER_COLOR WHITE | ||
| 49 | #elif defined (DARK_THEME) | ||
| 50 | #define BACKGROUND_COLOR BLACK | ||
| 51 | // #define EDGE_MIN_COLOR WHITE | ||
| 52 | // #define EDGE_MAX_COLOR WHITE | ||
| 53 | #define EDGE_TEXT_COLOR WHITE | ||
| 54 | #define VERTEX_COLOR RED | ||
| 55 | #define VERTEX_OUTLINE_COLOR MAROON | ||
| 56 | #define VERTEX_OUTLINE_HIGHLIGHTED_COLOR WHITE | ||
| 57 | #define VERTEX_TEXT_COLOR WHITE | ||
| 58 | #define BUTTON_COLOR DARKGRAY | ||
| 59 | #define BUTTON_OUTLINE_COLOR GRAY | ||
| 60 | #define BUTTON_HOVER_COLOR MAROON | ||
| 61 | #define BUTTON_HOVER_OUTLINE_COLOR RED | ||
| 62 | #define BUTTON_TEXT_COLOR LIGHTGRAY | ||
| 63 | #define BUTTON_TEXT_HOVER_COLOR LIGHTGRAY | ||
| 64 | #endif | ||
| 65 | |||
| 66 | #endif | ||
diff --git a/src/distribution-test.c b/src/distribution-test.c new file mode 100644 index 0000000..178bf73 --- /dev/null +++ b/src/distribution-test.c | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <time.h> | ||
| 6 | #include <raylib.h> | ||
| 7 | |||
| 8 | #include "distribution.h" | ||
| 9 | |||
| 10 | int | ||
| 11 | main (void) | ||
| 12 | { | ||
| 13 | const int screen_padding = 10; | ||
| 14 | const int notation_height = 20; | ||
| 15 | |||
| 16 | int screen_width = 800; | ||
| 17 | int screen_height = 450; | ||
| 18 | |||
| 19 | SetConfigFlags (FLAG_MSAA_4X_HINT | | ||
| 20 | FLAG_WINDOW_RESIZABLE | | ||
| 21 | FLAG_VSYNC_HINT | | ||
| 22 | FLAG_WINDOW_UNDECORATED); | ||
| 23 | InitWindow (screen_width, screen_height, "graph"); | ||
| 24 | SetTargetFPS (60); | ||
| 25 | |||
| 26 | srand (time (NULL)); | ||
| 27 | struct distribution dist; | ||
| 28 | // distribution_init (&dist, 20, 20, 1, 10); | ||
| 29 | distribution_init (&dist, 11, 20, 1, 10); | ||
| 30 | |||
| 31 | int arr[dist.n - 1]; | ||
| 32 | memset (arr, 0, sizeof (arr)); | ||
| 33 | int iterrations = 10000; | ||
| 34 | for (int i = 0; i < iterrations; ++i) | ||
| 35 | arr[distribution_rand (&dist)] += 1; | ||
| 36 | |||
| 37 | while (!WindowShouldClose ()) { | ||
| 38 | BeginDrawing (); | ||
| 39 | ClearBackground (WHITE); | ||
| 40 | |||
| 41 | screen_width = GetScreenWidth (); | ||
| 42 | screen_height = GetScreenHeight (); | ||
| 43 | |||
| 44 | const int rect_width = (screen_width - screen_padding)/(dist.n - 1) - screen_padding; | ||
| 45 | const int rect_max_height = screen_height - 2*screen_padding - notation_height; | ||
| 46 | for (int i = 0; i < dist.n - 1; ++i) { | ||
| 47 | const int rect_height = rect_max_height*arr[i]/iterrations; | ||
| 48 | DrawRectangle ((i + 1)*screen_padding + i*rect_width, | ||
| 49 | screen_padding + rect_max_height - rect_height, | ||
| 50 | rect_width, rect_height, BLACK); | ||
| 51 | |||
| 52 | char *perc; | ||
| 53 | asprintf (&perc, "%2d%%", arr[i]*100/iterrations); | ||
| 54 | DrawText (perc, (i + 1)*screen_padding + i*rect_width + rect_width/4, screen_height - screen_padding - 3*notation_height/4, notation_height, BLACK); | ||
| 55 | } | ||
| 56 | |||
| 57 | EndDrawing (); | ||
| 58 | } | ||
| 59 | |||
| 60 | CloseWindow (); | ||
| 61 | |||
| 62 | return EXIT_SUCCESS; | ||
| 63 | } | ||
diff --git a/src/distribution.c b/src/distribution.c new file mode 100644 index 0000000..7887ba3 --- /dev/null +++ b/src/distribution.c | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <assert.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | |||
| 6 | #include "distribution.h" | ||
| 7 | |||
| 8 | void | ||
| 9 | distribution_init (DISTRIBUTION dist, | ||
| 10 | unsigned int n, | ||
| 11 | unsigned int b, | ||
| 12 | unsigned int r, | ||
| 13 | int c) | ||
| 14 | { | ||
| 15 | assert (n > 0); | ||
| 16 | assert (b > 0); | ||
| 17 | assert (r > 0); | ||
| 18 | assert (b + r + c*(n - 1) > 0); | ||
| 19 | |||
| 20 | dist->n = n; | ||
| 21 | dist->b = b; | ||
| 22 | dist->r = r; | ||
| 23 | dist->c = c; | ||
| 24 | dist->p0 = r/(float)(b + r); | ||
| 25 | for (int i = 1; i < n; ++i) { | ||
| 26 | dist->p0 *= (r + i*c)/(float)(b + r + i*c); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | int | ||
| 31 | distribution_rand (DISTRIBUTION dist) | ||
| 32 | { | ||
| 33 | for (;;) { | ||
| 34 | double p = dist->p0; | ||
| 35 | double r = rand ()/(double) RAND_MAX; | ||
| 36 | // printf ("r = %f, p = %f\n", r, p); | ||
| 37 | unsigned int x = 0; | ||
| 38 | |||
| 39 | while (x < dist->n - 1) { | ||
| 40 | r -= p; | ||
| 41 | if (r < 0) | ||
| 42 | return x; | ||
| 43 | x += 1; | ||
| 44 | p *= ((dist->n - 1 - x) * | ||
| 45 | (dist->b + (x - 1)*dist->c) /(double) | ||
| 46 | (x*(dist->r + (dist->n - x)*dist->c))); | ||
| 47 | // printf ("p = %f\n", p); | ||
| 48 | } // putchar ('\n'); | ||
| 49 | } | ||
| 50 | // return dist->n; | ||
| 51 | } | ||
| 52 | |||
| 53 | double | ||
| 54 | distribution_nrand (DISTRIBUTION dist) | ||
| 55 | { | ||
| 56 | return distribution_rand (dist)/(double) (dist->n - 2); | ||
| 57 | } | ||
diff --git a/src/distribution.h b/src/distribution.h new file mode 100644 index 0000000..c4b20b9 --- /dev/null +++ b/src/distribution.h | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | #ifndef INCLUDED_DISTRIBUTION_H | ||
| 2 | #define INCLUDED_DISTRIBUTION_H | ||
| 3 | |||
| 4 | // Multivariate Pólya distribution | ||
| 5 | typedef struct distribution | ||
| 6 | { | ||
| 7 | unsigned int n; | ||
| 8 | unsigned int b; | ||
| 9 | unsigned int r; | ||
| 10 | int c; | ||
| 11 | double p0; | ||
| 12 | }* DISTRIBUTION; | ||
| 13 | |||
| 14 | void distribution_init (DISTRIBUTION dist, | ||
| 15 | unsigned int n, | ||
| 16 | unsigned int b, | ||
| 17 | unsigned int r, | ||
| 18 | int c); | ||
| 19 | int distribution_rand (DISTRIBUTION dist); | ||
| 20 | double distribution_nrand (DISTRIBUTION dist); | ||
| 21 | |||
| 22 | #endif | ||
diff --git a/src/dynamic-array.h b/src/dynamic-array.h new file mode 100644 index 0000000..bd470d5 --- /dev/null +++ b/src/dynamic-array.h | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | #ifndef INCLUDED_DYNAMIC_ARRAY_H | ||
| 2 | #define INCLUDED_DYNAMIC_ARRAY_H | ||
| 3 | |||
| 4 | #include "utils.h" | ||
| 5 | |||
| 6 | #ifndef DYNAMIC_ARRAY_INIT_CAP | ||
| 7 | #define DYNAMIC_ARRAY_INIT_CAP 256 | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ | ||
| 11 | ({ \ | ||
| 12 | if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ | ||
| 13 | if ((DYNAMIC_ARRAY).capacity == 0) \ | ||
| 14 | (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ | ||
| 15 | while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ | ||
| 16 | (DYNAMIC_ARRAY).capacity *= 2; \ | ||
| 17 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 18 | (DYNAMIC_ARRAY).capacity * \ | ||
| 19 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 20 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 21 | } \ | ||
| 22 | }) | ||
| 23 | |||
| 24 | #define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ | ||
| 25 | ({ \ | ||
| 26 | (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ | ||
| 27 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 28 | (DYNAMIC_ARRAY).capacity * \ | ||
| 29 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 30 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 31 | \ | ||
| 32 | }) | ||
| 33 | |||
| 34 | #define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ | ||
| 35 | ({ \ | ||
| 36 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ | ||
| 37 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ | ||
| 38 | }) | ||
| 39 | |||
| 40 | #define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ | ||
| 41 | ({ \ | ||
| 42 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ | ||
| 43 | memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ | ||
| 44 | (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 45 | (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ | ||
| 46 | }) | ||
| 47 | |||
| 48 | #define DYNAMIC_ARRAY_APPEND_OTHER(DYNAMIC_ARRAY, OTHER) \ | ||
| 49 | DYNAMIC_ARRAY_APPEND_MANY ((DYNAMIC_ARRAY), (OTHER).items, (OTHER).count) | ||
| 50 | |||
| 51 | #define DYNAMIC_ARRAY_COPY_OTHER(DYNAMIC_ARRAY, OTHER) \ | ||
| 52 | ({ \ | ||
| 53 | DYNAMIC_ARRAY_RESET ((DYNAMIC_ARRAY)); \ | ||
| 54 | DYNAMIC_ARRAY_APPEND_OTHER ((DYNAMIC_ARRAY), (OTHER)); \ | ||
| 55 | }) | ||
| 56 | |||
| 57 | #define DYNAMIC_ARRAY_ERASE(DYNAMIC_ARRAY, IDX) \ | ||
| 58 | ({ \ | ||
| 59 | memmove (&(DYNAMIC_ARRAY).items[(IDX) + 0], \ | ||
| 60 | &(DYNAMIC_ARRAY).items[(IDX) + 1], \ | ||
| 61 | ((DYNAMIC_ARRAY).count - (IDX) - 1) \ | ||
| 62 | * sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 63 | (DYNAMIC_ARRAY).count -= 1; \ | ||
| 64 | }) | ||
| 65 | |||
| 66 | #define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) | ||
| 67 | |||
| 68 | #define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) | ||
| 69 | |||
| 70 | #define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ | ||
| 71 | typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ | ||
| 72 | ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) | ||
| 73 | |||
| 74 | #endif | ||
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 | |||
| 12 | gsl_matrix * | ||
| 13 | shimbell_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 | |||
| 47 | size_t | ||
| 48 | graph_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 | |||
| 79 | static inline void | ||
| 80 | is_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 | |||
| 93 | bool | ||
| 94 | is_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 | |||
| 105 | bool | ||
| 106 | is_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 | |||
| 133 | void | ||
| 134 | generate_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 | |||
| 182 | size_t * | ||
| 183 | dijkstra_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 | |||
| 270 | size_t * | ||
| 271 | floyd_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 | |||
| 342 | void | ||
| 343 | graph_unhighlight (GRAPH g) | ||
| 344 | { | ||
| 345 | free (g->highlighted_path); | ||
| 346 | g->highlighted_path = NULL; | ||
| 347 | DYNAMIC_ARRAY_RESET (g->highlighted_edges); | ||
| 348 | } | ||
| 349 | |||
| 350 | void | ||
| 351 | graph_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 | |||
| 359 | void | ||
| 360 | print_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 | |||
| 370 | bool | ||
| 371 | are_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 | |||
| 401 | void | ||
| 402 | highlight_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 | ||
| 416 | double | ||
| 417 | max_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 | ||
| 486 | double | ||
| 487 | min_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 | |||
| 561 | size_t | ||
| 562 | count_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 | ||
| 589 | gsl_matrix * | ||
| 590 | min_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 | ||
| 655 | gsl_matrix * | ||
| 656 | min_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 | |||
| 741 | void | ||
| 742 | prufer_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 | |||
| 855 | bool | ||
| 856 | is_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 | ||
| 881 | bool | ||
| 882 | is_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 | |||
| 949 | void | ||
| 950 | graph_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 | |||
| 1129 | void | ||
| 1130 | graph_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 | |||
| 1209 | struct path { | ||
| 1210 | size_t *items; | ||
| 1211 | size_t count; | ||
| 1212 | size_t capacity; | ||
| 1213 | } typedef *PATH; | ||
| 1214 | |||
| 1215 | void | ||
| 1216 | find_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 | |||
| 1272 | void | ||
| 1273 | find_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 | } | ||
diff --git a/src/graph.h b/src/graph.h new file mode 100644 index 0000000..f55e975 --- /dev/null +++ b/src/graph.h | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | #ifndef INCLUDED_GRAPH_H | ||
| 2 | #define INCLUDED_GRAPH_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include <gsl/gsl_matrix.h> | ||
| 7 | #include <raylib.h> | ||
| 8 | #include "distribution.h" | ||
| 9 | |||
| 10 | enum graph_type { | ||
| 11 | GRAPH_TYPE_NONE, | ||
| 12 | GRAPH_TYPE_POSITIVE = 1 << 0, | ||
| 13 | GRAPH_TYPE_NEGATIVE = 1 << 1, | ||
| 14 | GRAPH_TYPE_POSITIVE_NEGATIVE = GRAPH_TYPE_POSITIVE | GRAPH_TYPE_NEGATIVE, | ||
| 15 | }; | ||
| 16 | |||
| 17 | struct edge { | ||
| 18 | size_t from; | ||
| 19 | size_t to; | ||
| 20 | }; | ||
| 21 | |||
| 22 | struct edges { | ||
| 23 | struct edge *items; | ||
| 24 | size_t count; | ||
| 25 | size_t capacity; | ||
| 26 | }; | ||
| 27 | |||
| 28 | typedef struct graph { | ||
| 29 | enum graph_type type; | ||
| 30 | size_t vertex_count; | ||
| 31 | gsl_matrix *adjecency_matrix; | ||
| 32 | gsl_matrix *weight_matrix; | ||
| 33 | gsl_matrix *capacity_matrix; | ||
| 34 | size_t *highlighted_path; | ||
| 35 | struct edges highlighted_edges; | ||
| 36 | } *GRAPH; | ||
| 37 | |||
| 38 | void generate_graph (GRAPH, DISTRIBUTION, | ||
| 39 | size_t vertex_count, enum graph_type); | ||
| 40 | void graph_unhighlight (GRAPH g); | ||
| 41 | void graph_edge_set (GRAPH, size_t from, size_t to, | ||
| 42 | double weight, double capacity); | ||
| 43 | void print_graph (GRAPH); | ||
| 44 | void highlight_graph (GRAPH, const gsl_matrix *); | ||
| 45 | |||
| 46 | // lab 1 | ||
| 47 | gsl_matrix *shimbell_method (GRAPH, size_t edge_count, bool max); | ||
| 48 | size_t graph_count_routes (GRAPH, size_t from, size_t to); | ||
| 49 | |||
| 50 | // lab 2 | ||
| 51 | size_t *dijkstra_positive (GRAPH, size_t from, size_t to, bool max); | ||
| 52 | size_t *floyd_warshall (GRAPH, size_t from, size_t to, bool max); | ||
| 53 | |||
| 54 | // lab 3 | ||
| 55 | double max_flow_ford_fulkerson (GRAPH, size_t from, size_t to); | ||
| 56 | double min_cost_flow (GRAPH, size_t from, size_t to, double required_flow); | ||
| 57 | |||
| 58 | // lab 4 | ||
| 59 | size_t count_spanning_trees (GRAPH); | ||
| 60 | gsl_matrix *min_weight_spannig_tree_prim (GRAPH); | ||
| 61 | gsl_matrix *min_weight_spannig_tree_boruvka (GRAPH); | ||
| 62 | void prufer_code (gsl_matrix *); | ||
| 63 | |||
| 64 | // lab 5 | ||
| 65 | bool is_graph_eulerian (GRAPH); | ||
| 66 | bool is_graph_hamiltonian (GRAPH); | ||
| 67 | void graph_ensure_eulerian (GRAPH); | ||
| 68 | void graph_ensure_hamiltonian (GRAPH); | ||
| 69 | void find_hamiltonian_cycles (GRAPH); | ||
| 70 | |||
| 71 | #endif | ||
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..d297e72 --- /dev/null +++ b/src/main.c | |||
| @@ -0,0 +1,580 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <assert.h> | ||
| 7 | #include <time.h> | ||
| 8 | #include <math.h> | ||
| 9 | #include <limits.h> | ||
| 10 | #include <raylib.h> | ||
| 11 | #include <raymath.h> | ||
| 12 | #include <gsl/gsl_matrix.h> | ||
| 13 | #include <gsl/gsl_blas.h> | ||
| 14 | #include <gsl/gsl_eigen.h> | ||
| 15 | #include <gsl/gsl_sort_vector.h> | ||
| 16 | |||
| 17 | #include "config.h" | ||
| 18 | #include "utils.h" | ||
| 19 | #include "distribution.h" | ||
| 20 | #include "graph.h" | ||
| 21 | #include "dynamic-array.h" | ||
| 22 | |||
| 23 | #define BUTTONS(X, ...) \ | ||
| 24 | X(__VA_ARGS__, EDIT, "Edit edge" ) \ | ||
| 25 | X(__VA_ARGS__, NEW_POSITIVE, "New graph+" ) \ | ||
| 26 | X(__VA_ARGS__, NEW_POSITIVE_NEGATIVE, "New graph±" ) \ | ||
| 27 | X(__VA_ARGS__, ROUTE_COUNT, "Route count" ) \ | ||
| 28 | X(__VA_ARGS__, SHIMBELL_MIN, "Min Shimbell" ) \ | ||
| 29 | X(__VA_ARGS__, SHIMBELL_MAX, "Max Shimbell" ) \ | ||
| 30 | X(__VA_ARGS__, DIJKSTRA_POSITIVE_MIN, "Min Dijkstra+" ) \ | ||
| 31 | X(__VA_ARGS__, DIJKSTRA_POSITIVE_MAX, "Max Dijkstra+" ) \ | ||
| 32 | X(__VA_ARGS__, FLOYD_WARSHALL_MIN, "Min Floyd-Warshall" ) \ | ||
| 33 | X(__VA_ARGS__, FLOYD_WARSHALL_MAX, "Max Floyd-Warshall" ) \ | ||
| 34 | X(__VA_ARGS__, MAX_FLOW_FORD_FULKERSON, "Max flow Ford-Fulkerson" ) \ | ||
| 35 | X(__VA_ARGS__, MIN_COST_FLOW, "Min cost flow" ) \ | ||
| 36 | X(__VA_ARGS__, COUNT_SPANNING_TREES, "Count spanning trees" ) \ | ||
| 37 | X(__VA_ARGS__, MIN_WEIGHT_SPANNIG_TREE_PRIM, "Min weight spanning tree Prim" ) \ | ||
| 38 | X(__VA_ARGS__, MIN_WEIGHT_SPANNIG_TREE_BORUVKA, "Min weight spanning tree BorÅÆvka") \ | ||
| 39 | X(__VA_ARGS__, ENSURE_EULERIAN, "Ensure Eulerian" ) \ | ||
| 40 | X(__VA_ARGS__, ENSURE_HAMILTONIAN, "Ensure Hamiltonian" ) \ | ||
| 41 | |||
| 42 | #define ENUM_ENTRY(PREFIX, SYMBOL, ...) PREFIX##_##SYMBOL, | ||
| 43 | #define ENUM_ENTRY_VALUE(PREFIX, SYMBOL, VALUE, ...) [PREFIX##_##SYMBOL] = VALUE, | ||
| 44 | |||
| 45 | enum { | ||
| 46 | BUTTONS(ENUM_ENTRY, BUTTON) | ||
| 47 | BUTTON_COUNT | ||
| 48 | } button; | ||
| 49 | #define BUTTON_NONE BUTTON_COUNT | ||
| 50 | |||
| 51 | static const char *button_labels[] = { | ||
| 52 | BUTTONS(ENUM_ENTRY_VALUE, BUTTON) | ||
| 53 | }; | ||
| 54 | |||
| 55 | static Font font; | ||
| 56 | static Rectangle button_recs[BUTTON_COUNT]; | ||
| 57 | static int button_hover = BUTTON_NONE; | ||
| 58 | static bool button_pressed = false; | ||
| 59 | static double start_time; | ||
| 60 | static Vector2 *model_vertices = NULL; | ||
| 61 | static struct distribution dist = {0}; | ||
| 62 | static struct graph g = {0}; | ||
| 63 | static gsl_matrix *saved_adjecency_matrix = NULL; | ||
| 64 | static gsl_matrix *saved_weight_matrix = NULL; | ||
| 65 | static gsl_matrix *saved_capacity_matrix = NULL; | ||
| 66 | |||
| 67 | void | ||
| 68 | new_graph (size_t vertex_count, enum graph_type type) | ||
| 69 | { | ||
| 70 | distribution_init (&dist, DIST_N*vertex_count, DIST_B, DIST_R, DIST_C); | ||
| 71 | generate_graph (&g, &dist, vertex_count, type); | ||
| 72 | print_graph (&g); | ||
| 73 | start_time = GetTime (); | ||
| 74 | |||
| 75 | // Initialize vertex positions in a circle | ||
| 76 | model_vertices = realloc (model_vertices, vertex_count*(sizeof *model_vertices)); | ||
| 77 | for (size_t i = 0; i < vertex_count; ++i) { | ||
| 78 | model_vertices[i].x = cosf (PI + 2*PI*i/(float)vertex_count); | ||
| 79 | model_vertices[i].y = sinf (PI + 2*PI*i/(float)vertex_count); | ||
| 80 | } | ||
| 81 | |||
| 82 | // Print graph in dot format | ||
| 83 | // puts ("digraph G {"); | ||
| 84 | // for (int i = 0; i < vertex_count; i++) { | ||
| 85 | // for (int j = 0; j < vertex_count; j++) { | ||
| 86 | // if (gsl_matrix_get (m, i, j) == 0) | ||
| 87 | // continue; | ||
| 88 | // printf (" %d -> %d [label=\"%g\"]\n", i, j, gsl_matrix_get (m, i, j)); | ||
| 89 | // } | ||
| 90 | // } puts ("}"); | ||
| 91 | } | ||
| 92 | |||
| 93 | void | ||
| 94 | startup (void) | ||
| 95 | { | ||
| 96 | SetConfigFlags (FLAG_MSAA_4X_HINT | | ||
| 97 | FLAG_WINDOW_RESIZABLE | | ||
| 98 | FLAG_VSYNC_HINT | | ||
| 99 | FLAG_WINDOW_UNDECORATED); | ||
| 100 | InitWindow (INITIAL_SCREEN_WIDTH, INITIAL_SCREEN_HEIGHT, WINDOW_TITLE); | ||
| 101 | SetTargetFPS (60); | ||
| 102 | |||
| 103 | const int codepoints_count = 96 + 256 + 2; | ||
| 104 | int codepoints[codepoints_count]; | ||
| 105 | for (int i = 0; i < 95; i++) codepoints[ 0 + i] = 0x0020 + i; | ||
| 106 | for (int i = 0; i < 255; i++) codepoints[96 + i] = 0x0400 + i; | ||
| 107 | codepoints[codepoints_count - 2] = 0x16f; | ||
| 108 | codepoints[codepoints_count - 1] = 0x0b1; | ||
| 109 | |||
| 110 | font = LoadFontEx ("./res/iosevka-term-slab-regular.ttf", FONT_SIZE, codepoints, codepoints_count); | ||
| 111 | SetTextureFilter (font.texture, TEXTURE_FILTER_ANISOTROPIC_16X); | ||
| 112 | |||
| 113 | // srand (time (NULL)); | ||
| 114 | srand (4); | ||
| 115 | |||
| 116 | // Initialize buttons | ||
| 117 | for (int i = 0; i < BUTTON_COUNT; ++i) { | ||
| 118 | Rectangle rec = { | ||
| 119 | .x = PADDING, | ||
| 120 | .y = PADDING + i*(BUTTON_HEIGHT + PADDING), | ||
| 121 | .width = BUTTON_WIDTH, | ||
| 122 | .height = BUTTON_HEIGHT, | ||
| 123 | }; | ||
| 124 | button_recs[i] = rec; | ||
| 125 | } | ||
| 126 | |||
| 127 | start_time = GetTime (); | ||
| 128 | |||
| 129 | new_graph (INITIAL_VERTEX_COUNT, GRAPH_TYPE_POSITIVE); | ||
| 130 | } | ||
| 131 | |||
| 132 | static inline size_t | ||
| 133 | read_vertex (char *label) | ||
| 134 | { | ||
| 135 | size_t v; | ||
| 136 | for (;;) { | ||
| 137 | prompt (label, "%zu", &v); | ||
| 138 | if (v >= g.vertex_count) { | ||
| 139 | printf ("ERROR: Vertex out of range [0; %zu)!\n", g.vertex_count); | ||
| 140 | } else return v; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | static inline size_t | ||
| 145 | read_vertex_count (void) | ||
| 146 | { | ||
| 147 | size_t vertex_count; | ||
| 148 | for (;;) { | ||
| 149 | prompt ("VERTEX_COUNT", "%zu", &vertex_count); | ||
| 150 | if (vertex_count > MAX_VERTEX_COUNT) { | ||
| 151 | printf ("ERROR: Vertex count cannot be larger than %zu!\n", MAX_VERTEX_COUNT); | ||
| 152 | } else return vertex_count; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | static inline void | ||
| 157 | handle_buttons (void) | ||
| 158 | { | ||
| 159 | button_hover = BUTTON_NONE; | ||
| 160 | button_pressed = false; | ||
| 161 | for (int i = 0; i < BUTTON_COUNT; ++i) { | ||
| 162 | if (CheckCollisionPointRec (GetMousePosition (), button_recs[i])) { | ||
| 163 | button_hover = i; | ||
| 164 | if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT)) | ||
| 165 | button_pressed = true; | ||
| 166 | break; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | if (button_pressed) { | ||
| 170 | if (button_hover != BUTTON_NONE) { | ||
| 171 | graph_unhighlight (&g); | ||
| 172 | if (saved_adjecency_matrix) { | ||
| 173 | gsl_matrix_free (g.adjecency_matrix); | ||
| 174 | g.adjecency_matrix = saved_adjecency_matrix; | ||
| 175 | saved_adjecency_matrix = NULL; | ||
| 176 | start_time = GetTime (); | ||
| 177 | } | ||
| 178 | if (saved_weight_matrix) { | ||
| 179 | gsl_matrix_free (g.weight_matrix); | ||
| 180 | g.weight_matrix = saved_weight_matrix; | ||
| 181 | saved_weight_matrix = NULL; | ||
| 182 | } | ||
| 183 | if (saved_capacity_matrix) { | ||
| 184 | gsl_matrix_free (g.capacity_matrix); | ||
| 185 | g.capacity_matrix = saved_capacity_matrix; | ||
| 186 | saved_capacity_matrix = NULL; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | switch (button_hover) { | ||
| 190 | case BUTTON_EDIT: { | ||
| 191 | size_t from = read_vertex ("FROM"); | ||
| 192 | size_t to = read_vertex ("TO"); | ||
| 193 | if (to == from) { | ||
| 194 | printf("ERROR: Self-loops are not allowed!\n"); | ||
| 195 | break; | ||
| 196 | } | ||
| 197 | |||
| 198 | double weight; | ||
| 199 | for (;;) { | ||
| 200 | prompt ("WEIGHT", "%lf", &weight); | ||
| 201 | if (weight > MAX_WEIGHT) { | ||
| 202 | printf ("ERROR: Weight cannot be larger than %lf!\n", MAX_WEIGHT); | ||
| 203 | } else if (g.type == GRAPH_TYPE_POSITIVE && weight < 0) { | ||
| 204 | printf ("ERROR: Weight cannot be negative in a positive graph!\n"); | ||
| 205 | } else if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE && weight < -MAX_WEIGHT) { | ||
| 206 | printf ("ERROR: Weight cannot be less than %lf!\n", -MAX_WEIGHT); | ||
| 207 | } else break; | ||
| 208 | } | ||
| 209 | |||
| 210 | double capacity; | ||
| 211 | for (;;) { | ||
| 212 | prompt ("CAPACITY", "%lf", &capacity); | ||
| 213 | if (weight > MAX_WEIGHT) { | ||
| 214 | printf ("ERROR: Capacity cannot be larger than %lf!\n", MAX_CAPACITY); | ||
| 215 | } else break; | ||
| 216 | } | ||
| 217 | |||
| 218 | graph_unhighlight (&g); | ||
| 219 | graph_edge_set (&g, from, to, weight, capacity); | ||
| 220 | print_graph (&g); | ||
| 221 | } break; | ||
| 222 | |||
| 223 | case BUTTON_NEW_POSITIVE: { | ||
| 224 | new_graph (read_vertex_count (), GRAPH_TYPE_POSITIVE); | ||
| 225 | } break; | ||
| 226 | |||
| 227 | case BUTTON_NEW_POSITIVE_NEGATIVE: { | ||
| 228 | new_graph (read_vertex_count (), GRAPH_TYPE_POSITIVE_NEGATIVE); | ||
| 229 | } break; | ||
| 230 | |||
| 231 | case BUTTON_ROUTE_COUNT: { | ||
| 232 | size_t from = read_vertex ("FROM"); | ||
| 233 | size_t to = read_vertex ("TO"); | ||
| 234 | size_t routes = graph_count_routes (&g, from, to); | ||
| 235 | if (routes) printf ("ROUTE COUNT FROM %zu TO %zu: %zu\n", from, to, routes); | ||
| 236 | else printf ("NO ROUTES FROM %zu TO %zu\n", from, to); | ||
| 237 | } break; | ||
| 238 | |||
| 239 | case BUTTON_SHIMBELL_MIN: { | ||
| 240 | int edge_count = 0; | ||
| 241 | do { | ||
| 242 | printf ("SHIMBELL METHOD EDGE COUNT: "); | ||
| 243 | scanf ("%d", &edge_count); | ||
| 244 | while (getchar () != '\n'); | ||
| 245 | } while (!edge_count); | ||
| 246 | gsl_matrix *sh = shimbell_method (&g, edge_count, false); | ||
| 247 | puts ("PATH MATRIX:"); | ||
| 248 | print_gsl_matrix (sh); | ||
| 249 | gsl_matrix_free (sh); | ||
| 250 | } break; | ||
| 251 | |||
| 252 | case BUTTON_SHIMBELL_MAX: { | ||
| 253 | int edge_count = 0; | ||
| 254 | do { | ||
| 255 | printf ("SHIMBELL METHOD EDGE COUNT: "); | ||
| 256 | scanf ("%d", &edge_count); | ||
| 257 | while (getchar () != '\n'); | ||
| 258 | } while (!edge_count); | ||
| 259 | gsl_matrix *sh = shimbell_method (&g, edge_count, true); | ||
| 260 | puts ("PATH MATRIX:"); | ||
| 261 | print_gsl_matrix (sh); | ||
| 262 | gsl_matrix_free (sh); | ||
| 263 | } break; | ||
| 264 | |||
| 265 | case BUTTON_DIJKSTRA_POSITIVE_MIN: { | ||
| 266 | size_t from = read_vertex ("FROM"); | ||
| 267 | size_t to = read_vertex ("TO"); | ||
| 268 | g.highlighted_path = dijkstra_positive (&g, from, to, false); | ||
| 269 | } break; | ||
| 270 | |||
| 271 | case BUTTON_DIJKSTRA_POSITIVE_MAX: { | ||
| 272 | size_t from = read_vertex ("FROM"); | ||
| 273 | size_t to = read_vertex ("TO"); | ||
| 274 | g.highlighted_path = dijkstra_positive (&g, from, to, true); | ||
| 275 | } break; | ||
| 276 | |||
| 277 | case BUTTON_FLOYD_WARSHALL_MIN: { | ||
| 278 | size_t from = read_vertex ("FROM"); | ||
| 279 | size_t to = read_vertex ("TO"); | ||
| 280 | g.highlighted_path = floyd_warshall (&g, from, to, false); | ||
| 281 | } break; | ||
| 282 | |||
| 283 | case BUTTON_FLOYD_WARSHALL_MAX: { | ||
| 284 | size_t from = read_vertex ("FROM"); | ||
| 285 | size_t to = read_vertex ("TO"); | ||
| 286 | g.highlighted_path = floyd_warshall (&g, from, to, true); | ||
| 287 | } break; | ||
| 288 | |||
| 289 | case BUTTON_MAX_FLOW_FORD_FULKERSON: { | ||
| 290 | size_t from = read_vertex ("FROM"); | ||
| 291 | size_t to = read_vertex ("TO"); | ||
| 292 | double max_flow = max_flow_ford_fulkerson (&g, from, to); | ||
| 293 | printf ("MAX_FLOW: %.0lf\n", max_flow); | ||
| 294 | } break; | ||
| 295 | |||
| 296 | case BUTTON_MIN_COST_FLOW: { | ||
| 297 | size_t from = read_vertex ("FROM"); | ||
| 298 | size_t to = read_vertex ("TO"); | ||
| 299 | double max_flow = max_flow_ford_fulkerson(&g, from, to); | ||
| 300 | if (max_flow == 0) { | ||
| 301 | printf ("NO PATH FROM %zu TO %zu\n", from, to); | ||
| 302 | break; | ||
| 303 | } | ||
| 304 | double required_flow = floor (max_flow*2/3); | ||
| 305 | printf ("REQUIRED_FLOW: [%.0lf*2/3] = %.0lf\n", max_flow, required_flow); | ||
| 306 | |||
| 307 | double min_cost = min_cost_flow (&g, from, to, required_flow); | ||
| 308 | printf ("MIN_COST: %.0lf\n", min_cost); | ||
| 309 | } break; | ||
| 310 | |||
| 311 | case BUTTON_COUNT_SPANNING_TREES: { | ||
| 312 | double spannig_tree_count = count_spanning_trees (&g); | ||
| 313 | printf ("SPANNING TREE COUNT %.0lf\n", spannig_tree_count); | ||
| 314 | } break; | ||
| 315 | |||
| 316 | case BUTTON_MIN_WEIGHT_SPANNIG_TREE_PRIM: { | ||
| 317 | gsl_matrix *minimal_spanning_tree = min_weight_spannig_tree_prim (&g); | ||
| 318 | prufer_code (minimal_spanning_tree); | ||
| 319 | highlight_graph (&g, minimal_spanning_tree); | ||
| 320 | gsl_matrix_free (minimal_spanning_tree); | ||
| 321 | } break; | ||
| 322 | |||
| 323 | case BUTTON_MIN_WEIGHT_SPANNIG_TREE_BORUVKA: { | ||
| 324 | gsl_matrix *minimal_spanning_tree = min_weight_spannig_tree_boruvka (&g); | ||
| 325 | prufer_code (minimal_spanning_tree); | ||
| 326 | highlight_graph (&g, minimal_spanning_tree); | ||
| 327 | gsl_matrix_free (minimal_spanning_tree); | ||
| 328 | } break; | ||
| 329 | |||
| 330 | case BUTTON_ENSURE_EULERIAN: { | ||
| 331 | saved_adjecency_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 332 | gsl_matrix_memcpy (saved_adjecency_matrix, g.adjecency_matrix); | ||
| 333 | saved_weight_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 334 | gsl_matrix_memcpy (saved_weight_matrix, g.weight_matrix); | ||
| 335 | saved_capacity_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 336 | gsl_matrix_memcpy (saved_capacity_matrix, g.capacity_matrix); | ||
| 337 | graph_ensure_eulerian (&g); | ||
| 338 | start_time = GetTime (); | ||
| 339 | } break; | ||
| 340 | |||
| 341 | case BUTTON_ENSURE_HAMILTONIAN: { | ||
| 342 | saved_adjecency_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 343 | gsl_matrix_memcpy (saved_adjecency_matrix, g.adjecency_matrix); | ||
| 344 | saved_weight_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 345 | gsl_matrix_memcpy (saved_weight_matrix, g.weight_matrix); | ||
| 346 | saved_capacity_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 347 | gsl_matrix_memcpy (saved_capacity_matrix, g.capacity_matrix); | ||
| 348 | graph_ensure_hamiltonian (&g); | ||
| 349 | start_time = GetTime (); | ||
| 350 | } break; | ||
| 351 | default: break; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | static inline void | ||
| 357 | draw_buttons (void) | ||
| 358 | { | ||
| 359 | for (int i = 0; i < BUTTON_COUNT; i++) { | ||
| 360 | DrawRectangleRec (button_recs[i], i == button_hover ? BUTTON_HOVER_COLOR | ||
| 361 | : BUTTON_COLOR); | ||
| 362 | DrawRectangleLinesEx (button_recs[i], 2, i == button_hover ? BUTTON_HOVER_OUTLINE_COLOR | ||
| 363 | : BUTTON_OUTLINE_COLOR); | ||
| 364 | Vector2 text_center = Vector2Scale ( | ||
| 365 | MeasureTextEx (font, button_labels[i], | ||
| 366 | FONT_SIZE, TEXT_SPACING), | ||
| 367 | 0.5); | ||
| 368 | Vector2 rect_center = { | ||
| 369 | .x = button_recs[i].x + button_recs[i].width/2, | ||
| 370 | .y = button_recs[i].y + button_recs[i].height/2 + 1, | ||
| 371 | }; | ||
| 372 | DrawTextEx (font, button_labels[i], Vector2Subtract (rect_center, | ||
| 373 | text_center), | ||
| 374 | FONT_SIZE, TEXT_SPACING, i == button_hover ? BUTTON_TEXT_HOVER_COLOR | ||
| 375 | : BUTTON_TEXT_COLOR); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | |||
| 379 | static inline void | ||
| 380 | progress_model (void) | ||
| 381 | { | ||
| 382 | size_t n = g.vertex_count; | ||
| 383 | Vector2 force_on_vertices[MAX_VERTEX_COUNT] = {0}; | ||
| 384 | for (size_t i = 0; i < n; ++i) { | ||
| 385 | for (size_t j = 0; j < n; ++j) { | ||
| 386 | if (i == j) | ||
| 387 | continue; | ||
| 388 | Vector2 dir = Vector2Normalize (Vector2Subtract (model_vertices[j], | ||
| 389 | model_vertices[i])); | ||
| 390 | if (gsl_matrix_get (g.adjecency_matrix, i, j) || | ||
| 391 | gsl_matrix_get (g.adjecency_matrix, j, i)) { | ||
| 392 | float distance = Vector2Distance (model_vertices[i], | ||
| 393 | model_vertices[j]); | ||
| 394 | float mag = MODEL_C_SPRING*logf (distance/MODEL_L); | ||
| 395 | force_on_vertices[i] = Vector2Add (force_on_vertices[i], | ||
| 396 | Vector2Scale (dir, mag)); | ||
| 397 | } else { | ||
| 398 | float square_distance = Vector2DistanceSqr (model_vertices[j], | ||
| 399 | model_vertices[i]); | ||
| 400 | float mag = MODEL_C_REP/square_distance; | ||
| 401 | force_on_vertices[i] = Vector2Add (force_on_vertices[i], | ||
| 402 | Vector2Scale (dir, -mag)); | ||
| 403 | } | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | float heat = exp ((-1 + MODEL_COOLING_FACTOR)*(GetTime () - start_time))/100; | ||
| 408 | for (int i = 0; i < n; ++i) { | ||
| 409 | model_vertices[i] = Vector2Add (model_vertices[i], | ||
| 410 | Vector2Scale (force_on_vertices[i], heat)); | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | void | ||
| 415 | draw_edges (Vector2 *vertex_screen_positions) | ||
| 416 | { | ||
| 417 | size_t n = g.vertex_count; | ||
| 418 | for (size_t i = 0; i < n; ++i) { | ||
| 419 | for (size_t j = 0; j < n; ++j) { | ||
| 420 | if (!gsl_matrix_get (g.adjecency_matrix, i, j)) continue; | ||
| 421 | double capacity = gsl_matrix_get ( | ||
| 422 | g.capacity_matrix, i, j); | ||
| 423 | float line_thickness = Remap (capacity, 0, MAX_CAPACITY, 4, 8); | ||
| 424 | float arrow_side = 3*line_thickness; | ||
| 425 | float arrow_height = arrow_side*sqrt (3)/2; | ||
| 426 | |||
| 427 | Vector2 dir = Vector2Normalize (Vector2Subtract ( | ||
| 428 | vertex_screen_positions[j], | ||
| 429 | vertex_screen_positions[i])); | ||
| 430 | |||
| 431 | Vector2 line_offset = Vector2Scale (dir, 1.5*VERTEX_RADIUS); | ||
| 432 | Vector2 line_begin = Vector2Add (vertex_screen_positions[i], | ||
| 433 | line_offset); | ||
| 434 | Vector2 line_end = Vector2Subtract ( | ||
| 435 | Vector2Subtract (vertex_screen_positions[j], line_offset), | ||
| 436 | Vector2Scale (dir, arrow_height)); | ||
| 437 | |||
| 438 | double weight = gsl_matrix_get (g.weight_matrix, i, j); | ||
| 439 | float brightness = Remap (Normalize (fabs (weight), 0, MAX_WEIGHT), | ||
| 440 | 0, 1, 0.25, 0.75); | ||
| 441 | Color line_color = ColorBrightness (BLACK, 1 - brightness); | ||
| 442 | if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE) { | ||
| 443 | if (weight < 0) { | ||
| 444 | line_color.b = 255/6; | ||
| 445 | line_color.g = 255/6; | ||
| 446 | } else { | ||
| 447 | line_color.r = 255/6; | ||
| 448 | line_color.b = 255/6; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | if (g.highlighted_path) { | ||
| 452 | for (size_t v = 0; g.highlighted_path[v] != n; ++v) { | ||
| 453 | if (g.highlighted_path[v + 0] == i && | ||
| 454 | g.highlighted_path[v + 1] == j || | ||
| 455 | g.highlighted_path[v + 0] == j && | ||
| 456 | g.highlighted_path[v + 1] == i) { | ||
| 457 | if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE) { | ||
| 458 | line_color = weight < 0 ? (Color) { 0xff, 0x00, 0x00, 0xff } | ||
| 459 | : (Color) { 0x00, 0xff, 0x00, 0xff }; | ||
| 460 | } else { | ||
| 461 | line_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR; | ||
| 462 | } break; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | } else if (g.highlighted_edges.count) { | ||
| 466 | for (size_t v = 0; v < g.highlighted_edges.count; ++v) { | ||
| 467 | if (g.highlighted_edges.items[v].from == i && | ||
| 468 | g.highlighted_edges.items[v].to == j) { | ||
| 469 | if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE) { | ||
| 470 | line_color = weight < 0 ? (Color) { 0xff, 0x00, 0x00, 0xff } | ||
| 471 | : (Color) { 0x00, 0xff, 0x00, 0xff }; | ||
| 472 | } else { | ||
| 473 | line_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR; | ||
| 474 | } break; | ||
| 475 | } | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | const Vector2 side = Vector2Scale (dir, -arrow_side/2); | ||
| 480 | DrawLineEx (line_begin, line_end, line_thickness, line_color); | ||
| 481 | DrawTriangle ( | ||
| 482 | Vector2Add (line_end, Vector2Scale (dir, arrow_height)), | ||
| 483 | Vector2Add (line_end, Vector2Rotate (side, PI/2)), | ||
| 484 | Vector2Add (line_end, Vector2Rotate (side, -PI/2)), | ||
| 485 | line_color); | ||
| 486 | |||
| 487 | char *anot; | ||
| 488 | asprintf (&anot, "%.0lf/%.0lf", capacity, weight); | ||
| 489 | Vector2 center = Vector2Scale ( | ||
| 490 | MeasureTextEx (font, anot, FONT_SIZE, TEXT_SPACING), 0.5); | ||
| 491 | Vector2 midpoint = Vector2Lerp(line_begin, line_end, 0.5); | ||
| 492 | Rectangle bounding_box = { | ||
| 493 | .x = midpoint.x - center.x - 4, | ||
| 494 | .y = midpoint.y - center.y, | ||
| 495 | .width = 2*(center.x + 4), | ||
| 496 | .height = 2*center.y, | ||
| 497 | }; | ||
| 498 | DrawRectangleRounded(bounding_box, 0.5, 16, ColorAlpha (BACKGROUND_COLOR, 0.75)); | ||
| 499 | DrawTextEx (font, anot, | ||
| 500 | Vector2Subtract (midpoint, center), | ||
| 501 | FONT_SIZE, TEXT_SPACING, EDGE_TEXT_COLOR); | ||
| 502 | free (anot); | ||
| 503 | } | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | void | ||
| 508 | draw_vertices (Vector2 *vertex_screen_positions) | ||
| 509 | { | ||
| 510 | for (size_t i = 0; i < g.vertex_count; ++i) { | ||
| 511 | Color outline_color = VERTEX_OUTLINE_COLOR; | ||
| 512 | |||
| 513 | if (g.highlighted_path) { | ||
| 514 | for (size_t v = 0; g.highlighted_path[v] != g.vertex_count; ++v) { | ||
| 515 | if (g.highlighted_path[v] == i) | ||
| 516 | outline_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR; | ||
| 517 | } | ||
| 518 | } else if (g.highlighted_edges.count) { | ||
| 519 | for (size_t v = 0; v < g.highlighted_edges.count; ++v) { | ||
| 520 | if (g.highlighted_edges.items[v].from == i || | ||
| 521 | g.highlighted_edges.items[v].to == i) { | ||
| 522 | outline_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR; | ||
| 523 | } | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | DrawCircleV (vertex_screen_positions[i], 1.2*VERTEX_RADIUS, outline_color); | ||
| 528 | DrawCircleV (vertex_screen_positions[i], VERTEX_RADIUS, VERTEX_COLOR); | ||
| 529 | char *anot; | ||
| 530 | asprintf (&anot, "%zu", i); | ||
| 531 | Vector2 center = Vector2Scale ( | ||
| 532 | MeasureTextEx (font, anot, FONT_SIZE, TEXT_SPACING), | ||
| 533 | 0.5); | ||
| 534 | DrawTextEx (font, anot, | ||
| 535 | Vector2Subtract (vertex_screen_positions[i], center), | ||
| 536 | FONT_SIZE, TEXT_SPACING, VERTEX_TEXT_COLOR); | ||
| 537 | free (anot); | ||
| 538 | } | ||
| 539 | } | ||
| 540 | |||
| 541 | int | ||
| 542 | main (void) | ||
| 543 | { | ||
| 544 | startup (); | ||
| 545 | while (!WindowShouldClose ()) { | ||
| 546 | if (IsKeyDown (KEY_SPACE)) | ||
| 547 | start_time = GetTime (); | ||
| 548 | handle_buttons (); | ||
| 549 | progress_model (); | ||
| 550 | |||
| 551 | // Find model space position supremums | ||
| 552 | Vector2 pos_max = { -INFINITY, -INFINITY }; | ||
| 553 | Vector2 pos_min = { INFINITY, INFINITY }; | ||
| 554 | for (size_t i = 0; i < g.vertex_count; ++i) { | ||
| 555 | if (model_vertices[i].x > pos_max.x) pos_max.x = model_vertices[i].x; | ||
| 556 | if (model_vertices[i].y > pos_max.y) pos_max.y = model_vertices[i].y; | ||
| 557 | if (model_vertices[i].x < pos_min.x) pos_min.x = model_vertices[i].x; | ||
| 558 | if (model_vertices[i].y < pos_min.y) pos_min.y = model_vertices[i].y; | ||
| 559 | } | ||
| 560 | |||
| 561 | // Remap model space positions to screen space | ||
| 562 | Vector2 vertex_screen_positions[g.vertex_count]; | ||
| 563 | for (size_t i = 0; i < g.vertex_count; ++i) { | ||
| 564 | vertex_screen_positions[i] = (Vector2) { | ||
| 565 | .x = Remap (model_vertices[i].x, pos_min.x, pos_max.x, 2*VERTEX_RADIUS + BUTTON_WIDTH + PADDING, GetScreenWidth () - 2*VERTEX_RADIUS), | ||
| 566 | .y = Remap (model_vertices[i].y, pos_min.y, pos_max.y, 2*VERTEX_RADIUS, GetScreenHeight () - 2*VERTEX_RADIUS), | ||
| 567 | }; | ||
| 568 | } | ||
| 569 | |||
| 570 | BeginDrawing (); | ||
| 571 | ClearBackground (BACKGROUND_COLOR); | ||
| 572 | draw_buttons (); | ||
| 573 | draw_edges (vertex_screen_positions); | ||
| 574 | draw_vertices (vertex_screen_positions); | ||
| 575 | // DrawFPS (2, 0); | ||
| 576 | EndDrawing (); | ||
| 577 | } CloseWindow (); | ||
| 578 | |||
| 579 | return EXIT_SUCCESS; | ||
| 580 | } | ||
diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..d96c3b6 --- /dev/null +++ b/src/utils.c | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | #include "utils.h" | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | void | ||
| 5 | print_gsl_matrix (const gsl_matrix *m) | ||
| 6 | { | ||
| 7 | int pad = 2; | ||
| 8 | |||
| 9 | for (size_t i = 0; i < log10sz (m->size1) + 3; i++) | ||
| 10 | putchar (' '); | ||
| 11 | for (size_t j = 0; j < m->size2; j++) | ||
| 12 | printf ("[%0*zu]", log10sz (m->size2) + 1 + pad, j); | ||
| 13 | putchar ('\n'); | ||
| 14 | for (size_t i = 0; i < m->size1; i++) { | ||
| 15 | printf ("[%0*zu]", log10sz (m->size1) + 1, i); | ||
| 16 | for (size_t j = 0; j < m->size2; j++) | ||
| 17 | printf (" %*.0f ", log10sz (m->size2) + 1 + pad, gsl_matrix_get (m, i, j)); | ||
| 18 | putchar ('\n'); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | void | ||
| 23 | prompt (char *label, char *fmt, void *where) | ||
| 24 | { | ||
| 25 | printf ("%s: ", label); | ||
| 26 | scanf (fmt, where); | ||
| 27 | while (getchar () != '\n'); | ||
| 28 | } | ||
| 29 | |||
| 30 | int | ||
| 31 | log10sz (size_t sz) | ||
| 32 | { | ||
| 33 | return ((sz >= 10000000000000000000lu) ? 19 : | ||
| 34 | (sz >= 1000000000000000000lu) ? 18 : | ||
| 35 | (sz >= 100000000000000000lu) ? 17 : | ||
| 36 | (sz >= 10000000000000000lu) ? 16 : | ||
| 37 | (sz >= 1000000000000000lu) ? 15 : | ||
| 38 | (sz >= 100000000000000lu) ? 14 : | ||
| 39 | (sz >= 10000000000000lu) ? 13 : | ||
| 40 | (sz >= 1000000000000lu) ? 12 : | ||
| 41 | (sz >= 100000000000lu) ? 11 : | ||
| 42 | (sz >= 1000000000lu) ? 9 : | ||
| 43 | (sz >= 100000000lu) ? 8 : | ||
| 44 | (sz >= 10000000lu) ? 7 : | ||
| 45 | (sz >= 1000000lu) ? 6 : | ||
| 46 | (sz >= 100000lu) ? 5 : | ||
| 47 | (sz >= 10000lu) ? 4 : | ||
| 48 | (sz >= 1000lu) ? 3 : | ||
| 49 | (sz >= 100lu) ? 2 : | ||
| 50 | (sz >= 10lu) ? 1 : 0u); | ||
| 51 | } | ||
| 52 | |||
| 53 | gsl_matrix *gsl_matrix_minor (const gsl_matrix *m, size_t row, size_t col) { | ||
| 54 | int n = m->size1 > m->size2 ? m->size1 : m->size2; | ||
| 55 | gsl_matrix *minor = gsl_matrix_alloc (n - 1, n - 1); | ||
| 56 | |||
| 57 | for (size_t i = 0; i < n; ++i) { | ||
| 58 | if (i == row) continue; | ||
| 59 | for (size_t j = 0; j < n; ++j) { | ||
| 60 | if (j == col) continue; | ||
| 61 | gsl_matrix_set (minor, | ||
| 62 | i < row ? i : i - 1, | ||
| 63 | j < col ? j : j - 1, | ||
| 64 | gsl_matrix_get (m, i, j)); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | return minor; | ||
| 69 | } | ||
| 70 | |||
| 71 | double | ||
| 72 | gsl_matrix_determinant (const gsl_matrix *m) | ||
| 73 | { | ||
| 74 | size_t n = m->size1; | ||
| 75 | |||
| 76 | if (n == 1) | ||
| 77 | return gsl_matrix_get (m, 0, 0); | ||
| 78 | if (n == 2) { | ||
| 79 | return | ||
| 80 | gsl_matrix_get (m, 0, 0)*gsl_matrix_get (m, 1, 1) - | ||
| 81 | gsl_matrix_get (m, 0, 1)*gsl_matrix_get (m, 1, 0); | ||
| 82 | } | ||
| 83 | |||
| 84 | double determinant = 0; | ||
| 85 | for (size_t i = 0; i < n; ++i) { | ||
| 86 | gsl_matrix *minor = gsl_matrix_minor (m, 0, i); | ||
| 87 | |||
| 88 | double minor_determinant = gsl_matrix_determinant(minor); | ||
| 89 | gsl_matrix_free (minor); | ||
| 90 | |||
| 91 | determinant += (i % 2 == 0 ? 1 : -1) * | ||
| 92 | gsl_matrix_get (m, 0, i) * | ||
| 93 | minor_determinant; | ||
| 94 | } | ||
| 95 | |||
| 96 | return determinant; | ||
| 97 | } | ||
| 98 | |||
| 99 | double | ||
| 100 | gsl_matrix_cofactor (const gsl_matrix *m, size_t i, size_t j) | ||
| 101 | { | ||
| 102 | gsl_matrix *minor = gsl_matrix_minor (m, i, j); | ||
| 103 | |||
| 104 | double minor_determinant = gsl_matrix_determinant (minor); | ||
| 105 | |||
| 106 | return (i + j) % 2 == 0 ? +minor_determinant | ||
| 107 | : -minor_determinant; | ||
| 108 | } | ||
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..6883a16 --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | #ifndef INCLUDED_UTILS_H | ||
| 2 | #define INCLUDED_UTILS_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include <gsl/gsl_spmatrix.h> | ||
| 7 | #include <assert.h> | ||
| 8 | |||
| 9 | #define MALLOC malloc | ||
| 10 | #define REALLOC realloc | ||
| 11 | #define FREE free | ||
| 12 | #define ASSERT assert | ||
| 13 | |||
| 14 | #define MIN(A, B) ({ \ | ||
| 15 | A < B ? A : B; \ | ||
| 16 | }) | ||
| 17 | |||
| 18 | #define SWAP(A, B) ({ \ | ||
| 19 | typeof (A) tmp = (A); \ | ||
| 20 | (A) = (B); \ | ||
| 21 | (B) = tmp; \ | ||
| 22 | }) | ||
| 23 | |||
| 24 | #define PRINT_EXP(EXP, FMT) printf (#EXP" = "FMT"\n", EXP) | ||
| 25 | |||
| 26 | extern void print_gsl_matrix (const gsl_matrix *); | ||
| 27 | extern void prompt (char *label, char *fmt, void *where); | ||
| 28 | extern int log10sz (size_t sz); | ||
| 29 | |||
| 30 | double gsl_matrix_determinant (const gsl_matrix *); | ||
| 31 | double gsl_matrix_cofactor (const gsl_matrix *, size_t i, size_t j); | ||
| 32 | |||
| 33 | #endif | ||
