aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: d297e72d9ba0765415a95fdd92b3a1bd7ceaabef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <raylib.h>
#include <raymath.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_eigen.h>
#include <gsl/gsl_sort_vector.h>

#include "config.h"
#include "utils.h"
#include "distribution.h"
#include "graph.h"
#include "dynamic-array.h"

#define BUTTONS(X, ...)                                                        \
  X(__VA_ARGS__, EDIT,                            "Edit edge"                       ) \
  X(__VA_ARGS__, NEW_POSITIVE,                    "New graph+"                      ) \
  X(__VA_ARGS__, NEW_POSITIVE_NEGATIVE,           "New graph±"                      ) \
  X(__VA_ARGS__, ROUTE_COUNT,                     "Route count"                     ) \
  X(__VA_ARGS__, SHIMBELL_MIN,                    "Min Shimbell"                    ) \
  X(__VA_ARGS__, SHIMBELL_MAX,                    "Max Shimbell"                    ) \
  X(__VA_ARGS__, DIJKSTRA_POSITIVE_MIN,           "Min Dijkstra+"                   ) \
  X(__VA_ARGS__, DIJKSTRA_POSITIVE_MAX,           "Max Dijkstra+"                   ) \
  X(__VA_ARGS__, FLOYD_WARSHALL_MIN,              "Min Floyd-Warshall"              ) \
  X(__VA_ARGS__, FLOYD_WARSHALL_MAX,              "Max Floyd-Warshall"              ) \
  X(__VA_ARGS__, MAX_FLOW_FORD_FULKERSON,         "Max flow Ford-Fulkerson"         ) \
  X(__VA_ARGS__, MIN_COST_FLOW,                   "Min cost flow"                   ) \
  X(__VA_ARGS__, COUNT_SPANNING_TREES,            "Count spanning trees"            ) \
  X(__VA_ARGS__, MIN_WEIGHT_SPANNIG_TREE_PRIM,    "Min weight spanning tree Prim"   ) \
  X(__VA_ARGS__, MIN_WEIGHT_SPANNIG_TREE_BORUVKA, "Min weight spanning tree Borůvka") \
  X(__VA_ARGS__, ENSURE_EULERIAN,                 "Ensure Eulerian"                 ) \
  X(__VA_ARGS__, ENSURE_HAMILTONIAN,              "Ensure Hamiltonian"              ) \

#define ENUM_ENTRY(PREFIX, SYMBOL, ...) PREFIX##_##SYMBOL,
#define ENUM_ENTRY_VALUE(PREFIX, SYMBOL, VALUE, ...) [PREFIX##_##SYMBOL] = VALUE,

enum {
  BUTTONS(ENUM_ENTRY, BUTTON)
  BUTTON_COUNT
} button;
#define BUTTON_NONE BUTTON_COUNT

static const char *button_labels[] = {
  BUTTONS(ENUM_ENTRY_VALUE, BUTTON)
};

static Font font;
static Rectangle button_recs[BUTTON_COUNT];
static int button_hover = BUTTON_NONE;
static bool button_pressed = false;
static double start_time;
static Vector2 *model_vertices = NULL;
static struct distribution dist = {0};
static struct graph g = {0};
static gsl_matrix *saved_adjecency_matrix = NULL;
static gsl_matrix *saved_weight_matrix = NULL;
static gsl_matrix *saved_capacity_matrix = NULL;

void
new_graph (size_t vertex_count, enum graph_type type)
{
  distribution_init (&dist, DIST_N*vertex_count, DIST_B, DIST_R, DIST_C);
  generate_graph (&g, &dist, vertex_count, type);
  print_graph (&g);
  start_time = GetTime ();

  // Initialize vertex positions in a circle
  model_vertices = realloc (model_vertices, vertex_count*(sizeof *model_vertices));
  for (size_t i = 0; i < vertex_count; ++i) {
    model_vertices[i].x = cosf (PI + 2*PI*i/(float)vertex_count);
    model_vertices[i].y = sinf (PI + 2*PI*i/(float)vertex_count);
  }

  // Print graph in dot format
  // puts ("digraph G {");
  // for (int i = 0; i < vertex_count; i++) {
  //   for (int j = 0; j < vertex_count; j++) {
  //     if (gsl_matrix_get (m, i, j) == 0)
  //       continue;
  //     printf ("        %d -> %d [label=\"%g\"]\n", i, j, gsl_matrix_get (m, i, j));
  //   }
  // } puts ("}");
}

void
startup (void)
{
  SetConfigFlags (FLAG_MSAA_4X_HINT |
                  FLAG_WINDOW_RESIZABLE |
                  FLAG_VSYNC_HINT |
                  FLAG_WINDOW_UNDECORATED);
  InitWindow (INITIAL_SCREEN_WIDTH, INITIAL_SCREEN_HEIGHT, WINDOW_TITLE);
  SetTargetFPS (60);

  const int codepoints_count = 96 + 256 + 2;
  int codepoints[codepoints_count];
  for (int i = 0; i <  95; i++) codepoints[ 0 + i] = 0x0020 + i;
  for (int i = 0; i < 255; i++) codepoints[96 + i] = 0x0400 + i;
  codepoints[codepoints_count - 2] = 0x16f;
  codepoints[codepoints_count - 1] = 0x0b1;

  font = LoadFontEx ("./res/iosevka-term-slab-regular.ttf", FONT_SIZE, codepoints, codepoints_count);
  SetTextureFilter (font.texture, TEXTURE_FILTER_ANISOTROPIC_16X);

  // srand (time (NULL));
  srand (4);

  // Initialize buttons
  for (int i = 0; i < BUTTON_COUNT; ++i) {
    Rectangle rec = {
      .x = PADDING,
      .y = PADDING + i*(BUTTON_HEIGHT + PADDING),
      .width  = BUTTON_WIDTH,
      .height = BUTTON_HEIGHT,
    };
    button_recs[i] = rec;
  }

  start_time = GetTime ();

  new_graph (INITIAL_VERTEX_COUNT, GRAPH_TYPE_POSITIVE);
}

static inline size_t
read_vertex (char *label)
{
  size_t v;
  for (;;) {
    prompt (label, "%zu", &v);
    if (v >= g.vertex_count) {
      printf ("ERROR: Vertex out of range [0; %zu)!\n", g.vertex_count);
    } else return v;
  }
}

static inline size_t
read_vertex_count (void)
{
  size_t vertex_count;
  for (;;) {
    prompt ("VERTEX_COUNT", "%zu", &vertex_count);
    if (vertex_count > MAX_VERTEX_COUNT) {
      printf ("ERROR: Vertex count cannot be larger than %zu!\n", MAX_VERTEX_COUNT);
    } else return vertex_count;
  }
}

static inline void
handle_buttons (void)
{
  button_hover = BUTTON_NONE;
  button_pressed = false;
  for (int i = 0; i < BUTTON_COUNT; ++i) {
    if (CheckCollisionPointRec (GetMousePosition (), button_recs[i])) {
      button_hover = i;
      if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT))
        button_pressed = true;
      break;
    }
  }
  if (button_pressed) {
    if (button_hover != BUTTON_NONE) {
      graph_unhighlight (&g);
      if (saved_adjecency_matrix) {
        gsl_matrix_free (g.adjecency_matrix);
        g.adjecency_matrix = saved_adjecency_matrix;
        saved_adjecency_matrix = NULL;
        start_time = GetTime ();
      }
      if (saved_weight_matrix) {
        gsl_matrix_free (g.weight_matrix);
        g.weight_matrix = saved_weight_matrix;
        saved_weight_matrix = NULL;
      }
      if (saved_capacity_matrix) {
        gsl_matrix_free (g.capacity_matrix);
        g.capacity_matrix = saved_capacity_matrix;
        saved_capacity_matrix = NULL;
      }
    }
    switch (button_hover) {
      case BUTTON_EDIT: {
        size_t from = read_vertex ("FROM");
        size_t to   = read_vertex ("TO");
        if (to == from) {
          printf("ERROR: Self-loops are not allowed!\n");
          break;
        }

        double weight;
        for (;;) {
          prompt ("WEIGHT", "%lf", &weight);
          if (weight > MAX_WEIGHT) {
            printf ("ERROR: Weight cannot be larger than %lf!\n", MAX_WEIGHT);
          } else if (g.type == GRAPH_TYPE_POSITIVE && weight < 0) {
            printf ("ERROR: Weight cannot be negative in a positive graph!\n");
          } else if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE && weight < -MAX_WEIGHT) {
            printf ("ERROR: Weight cannot be less than %lf!\n", -MAX_WEIGHT);
          } else break;
        }

        double capacity;
        for (;;) {
          prompt ("CAPACITY", "%lf", &capacity);
          if (weight > MAX_WEIGHT) {
            printf ("ERROR: Capacity cannot be larger than %lf!\n", MAX_CAPACITY);
          } else break;
        }

        graph_unhighlight (&g);
        graph_edge_set (&g, from, to, weight, capacity);
        print_graph (&g);
      } break;

      case BUTTON_NEW_POSITIVE: {
        new_graph (read_vertex_count (), GRAPH_TYPE_POSITIVE);
      } break;

      case BUTTON_NEW_POSITIVE_NEGATIVE: {
        new_graph (read_vertex_count (), GRAPH_TYPE_POSITIVE_NEGATIVE);
      } break;

      case BUTTON_ROUTE_COUNT: {
        size_t from   = read_vertex ("FROM");
        size_t to     = read_vertex ("TO");
        size_t routes = graph_count_routes (&g, from, to);
        if (routes) printf ("ROUTE COUNT FROM %zu TO %zu: %zu\n", from, to, routes);
        else        printf ("NO ROUTES FROM  %zu TO %zu\n", from, to);
      } break;

      case BUTTON_SHIMBELL_MIN: {
        int edge_count = 0;
        do {
          printf ("SHIMBELL METHOD EDGE COUNT: ");
          scanf ("%d", &edge_count);
          while (getchar () != '\n');
        } while (!edge_count);
        gsl_matrix *sh = shimbell_method (&g, edge_count, false);
        puts ("PATH MATRIX:");
        print_gsl_matrix (sh);
        gsl_matrix_free (sh);
      } break;

      case BUTTON_SHIMBELL_MAX: {
        int edge_count = 0;
        do {
          printf ("SHIMBELL METHOD EDGE COUNT: ");
          scanf ("%d", &edge_count);
          while (getchar () != '\n');
        } while (!edge_count);
        gsl_matrix *sh = shimbell_method (&g, edge_count, true);
        puts ("PATH MATRIX:");
        print_gsl_matrix (sh);
        gsl_matrix_free (sh);
      } break;

      case BUTTON_DIJKSTRA_POSITIVE_MIN: {
        size_t from = read_vertex ("FROM");
        size_t to   = read_vertex ("TO");
        g.highlighted_path = dijkstra_positive (&g, from, to, false);
      } break;

      case BUTTON_DIJKSTRA_POSITIVE_MAX: {
        size_t from = read_vertex ("FROM");
        size_t to   = read_vertex ("TO");
        g.highlighted_path = dijkstra_positive (&g, from, to, true);
      } break;

      case BUTTON_FLOYD_WARSHALL_MIN: {
        size_t from = read_vertex ("FROM");
        size_t to   = read_vertex ("TO");
        g.highlighted_path = floyd_warshall (&g, from, to, false);
      } break;

      case BUTTON_FLOYD_WARSHALL_MAX: {
        size_t from = read_vertex ("FROM");
        size_t to   = read_vertex ("TO");
        g.highlighted_path = floyd_warshall (&g, from, to, true);
      } break;

      case BUTTON_MAX_FLOW_FORD_FULKERSON: {
        size_t from = read_vertex ("FROM");
        size_t to   = read_vertex ("TO");
        double max_flow = max_flow_ford_fulkerson (&g, from, to);
        printf ("MAX_FLOW: %.0lf\n", max_flow);
      } break;

      case BUTTON_MIN_COST_FLOW: {
        size_t from = read_vertex ("FROM");
        size_t to   = read_vertex ("TO");
        double max_flow = max_flow_ford_fulkerson(&g, from, to);
        if (max_flow == 0) {
          printf ("NO PATH FROM %zu TO %zu\n", from, to);
          break;
        }
        double required_flow = floor (max_flow*2/3);
        printf ("REQUIRED_FLOW: [%.0lf*2/3] = %.0lf\n", max_flow, required_flow);

        double min_cost = min_cost_flow (&g, from, to, required_flow);
        printf ("MIN_COST: %.0lf\n", min_cost);
      } break;

      case BUTTON_COUNT_SPANNING_TREES: {
        double spannig_tree_count = count_spanning_trees (&g);
        printf ("SPANNING TREE COUNT %.0lf\n", spannig_tree_count);
      } break;

      case BUTTON_MIN_WEIGHT_SPANNIG_TREE_PRIM: {
        gsl_matrix *minimal_spanning_tree = min_weight_spannig_tree_prim (&g);
        prufer_code (minimal_spanning_tree);
        highlight_graph (&g, minimal_spanning_tree);
        gsl_matrix_free (minimal_spanning_tree);
      } break;

      case BUTTON_MIN_WEIGHT_SPANNIG_TREE_BORUVKA: {
        gsl_matrix *minimal_spanning_tree = min_weight_spannig_tree_boruvka (&g);
        prufer_code (minimal_spanning_tree);
        highlight_graph (&g, minimal_spanning_tree);
        gsl_matrix_free (minimal_spanning_tree);
      } break;

      case BUTTON_ENSURE_EULERIAN: {
        saved_adjecency_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count);
        gsl_matrix_memcpy (saved_adjecency_matrix, g.adjecency_matrix);
        saved_weight_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count);
        gsl_matrix_memcpy (saved_weight_matrix, g.weight_matrix);
        saved_capacity_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count);
        gsl_matrix_memcpy (saved_capacity_matrix, g.capacity_matrix);
        graph_ensure_eulerian (&g);
        start_time = GetTime ();
      } break;

      case BUTTON_ENSURE_HAMILTONIAN: {
        saved_adjecency_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count);
        gsl_matrix_memcpy (saved_adjecency_matrix, g.adjecency_matrix);
        saved_weight_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count);
        gsl_matrix_memcpy (saved_weight_matrix, g.weight_matrix);
        saved_capacity_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count);
        gsl_matrix_memcpy (saved_capacity_matrix, g.capacity_matrix);
        graph_ensure_hamiltonian (&g);
        start_time = GetTime ();
      } break;
      default: break;
    }
  }
}

static inline void
draw_buttons (void)
{
  for (int i = 0; i < BUTTON_COUNT; i++) {
    DrawRectangleRec (button_recs[i], i == button_hover ? BUTTON_HOVER_COLOR
                                                        : BUTTON_COLOR);
    DrawRectangleLinesEx (button_recs[i], 2, i == button_hover ? BUTTON_HOVER_OUTLINE_COLOR
                                                               : BUTTON_OUTLINE_COLOR);
    Vector2 text_center = Vector2Scale (
      MeasureTextEx (font, button_labels[i],
                     FONT_SIZE, TEXT_SPACING),
      0.5);
    Vector2 rect_center = {
      .x = button_recs[i].x + button_recs[i].width/2,
      .y = button_recs[i].y + button_recs[i].height/2 + 1,
    };
    DrawTextEx (font, button_labels[i], Vector2Subtract (rect_center,
                                                       text_center),
                FONT_SIZE, TEXT_SPACING, i == button_hover ? BUTTON_TEXT_HOVER_COLOR
                                                           : BUTTON_TEXT_COLOR);
  }
}

static inline void
progress_model (void)
{
  size_t n = g.vertex_count;
  Vector2 force_on_vertices[MAX_VERTEX_COUNT] = {0};
  for (size_t i = 0; i < n; ++i) {
    for (size_t j = 0; j < n; ++j) {
      if (i == j)
        continue;
      Vector2 dir = Vector2Normalize (Vector2Subtract (model_vertices[j],
                                                       model_vertices[i]));
      if (gsl_matrix_get (g.adjecency_matrix, i, j) ||
          gsl_matrix_get (g.adjecency_matrix, j, i)) {
        float distance = Vector2Distance (model_vertices[i],
                                          model_vertices[j]);
        float mag = MODEL_C_SPRING*logf (distance/MODEL_L);
        force_on_vertices[i] = Vector2Add (force_on_vertices[i],
                                         Vector2Scale (dir, mag));
      } else {
        float square_distance = Vector2DistanceSqr (model_vertices[j],
                                                    model_vertices[i]);
        float mag = MODEL_C_REP/square_distance;
        force_on_vertices[i] = Vector2Add (force_on_vertices[i],
                                         Vector2Scale (dir, -mag));
      }
    }
  }

  float heat = exp ((-1 + MODEL_COOLING_FACTOR)*(GetTime () - start_time))/100;
  for (int i = 0; i < n; ++i) {
    model_vertices[i] = Vector2Add (model_vertices[i],
                                    Vector2Scale (force_on_vertices[i], heat));
  }
}

void
draw_edges (Vector2 *vertex_screen_positions)
{
  size_t n = g.vertex_count;
  for (size_t i = 0; i < n; ++i) {
    for (size_t j = 0; j < n; ++j) {
      if (!gsl_matrix_get (g.adjecency_matrix, i, j)) continue;
      double capacity = gsl_matrix_get (
        g.capacity_matrix, i, j);
      float line_thickness = Remap (capacity, 0, MAX_CAPACITY, 4, 8);
      float arrow_side = 3*line_thickness;
      float arrow_height = arrow_side*sqrt (3)/2;

      Vector2 dir = Vector2Normalize (Vector2Subtract (
                                        vertex_screen_positions[j],
                                        vertex_screen_positions[i]));

      Vector2 line_offset = Vector2Scale (dir, 1.5*VERTEX_RADIUS);
      Vector2 line_begin = Vector2Add (vertex_screen_positions[i],
                                       line_offset);
      Vector2 line_end = Vector2Subtract (
        Vector2Subtract (vertex_screen_positions[j], line_offset),
        Vector2Scale (dir, arrow_height));

      double weight = gsl_matrix_get (g.weight_matrix, i, j);
      float brightness = Remap (Normalize (fabs (weight), 0, MAX_WEIGHT),
                                0, 1, 0.25, 0.75);
      Color line_color = ColorBrightness (BLACK, 1 - brightness);
      if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE) {
        if (weight < 0) {
          line_color.b = 255/6;
          line_color.g = 255/6;
        } else {
          line_color.r = 255/6;
          line_color.b = 255/6;
        }
      }
      if (g.highlighted_path) {
        for (size_t v = 0; g.highlighted_path[v] != n; ++v) {
          if (g.highlighted_path[v + 0] == i &&
              g.highlighted_path[v + 1] == j ||
              g.highlighted_path[v + 0] == j &&
              g.highlighted_path[v + 1] == i) {
            if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE) {
              line_color = weight < 0 ? (Color) { 0xff, 0x00, 0x00, 0xff }
                                      : (Color) { 0x00, 0xff, 0x00, 0xff };
            } else {
              line_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR;
            } break;
          }
        }
      } else if (g.highlighted_edges.count) {
        for (size_t v = 0; v < g.highlighted_edges.count; ++v) {
          if (g.highlighted_edges.items[v].from == i &&
              g.highlighted_edges.items[v].to   == j) {
            if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE) {
              line_color = weight < 0 ? (Color) { 0xff, 0x00, 0x00, 0xff }
                                      : (Color) { 0x00, 0xff, 0x00, 0xff };
            } else {
              line_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR;
            } break;
          }
        }
      }

      const Vector2 side = Vector2Scale (dir, -arrow_side/2);
      DrawLineEx (line_begin, line_end, line_thickness, line_color);
      DrawTriangle (
        Vector2Add (line_end, Vector2Scale (dir, arrow_height)),
        Vector2Add (line_end, Vector2Rotate (side,  PI/2)),
        Vector2Add (line_end, Vector2Rotate (side, -PI/2)),
        line_color);

      char *anot;
      asprintf (&anot, "%.0lf/%.0lf", capacity, weight);
      Vector2 center = Vector2Scale (
        MeasureTextEx (font, anot, FONT_SIZE, TEXT_SPACING), 0.5);
      Vector2 midpoint = Vector2Lerp(line_begin, line_end, 0.5);
      Rectangle bounding_box = {
        .x = midpoint.x - center.x - 4,
        .y = midpoint.y - center.y,
        .width  = 2*(center.x + 4),
        .height = 2*center.y,
      };
      DrawRectangleRounded(bounding_box, 0.5, 16, ColorAlpha (BACKGROUND_COLOR, 0.75));
      DrawTextEx (font, anot,
                  Vector2Subtract (midpoint, center),
                  FONT_SIZE, TEXT_SPACING, EDGE_TEXT_COLOR);
      free (anot);
    }
  }
}

void
draw_vertices (Vector2 *vertex_screen_positions)
{
  for (size_t i = 0; i < g.vertex_count; ++i) {
    Color outline_color = VERTEX_OUTLINE_COLOR;

    if (g.highlighted_path) {
      for (size_t v = 0; g.highlighted_path[v] != g.vertex_count; ++v) {
        if (g.highlighted_path[v] == i)
          outline_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR;
      }
    } else if (g.highlighted_edges.count) {
      for (size_t v = 0; v < g.highlighted_edges.count; ++v) {
        if (g.highlighted_edges.items[v].from == i ||
            g.highlighted_edges.items[v].to   == i) {
          outline_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR;
        }
      }
    }

    DrawCircleV (vertex_screen_positions[i], 1.2*VERTEX_RADIUS, outline_color);
    DrawCircleV (vertex_screen_positions[i],     VERTEX_RADIUS, VERTEX_COLOR);
    char *anot;
    asprintf (&anot, "%zu", i);
    Vector2 center = Vector2Scale (
      MeasureTextEx (font, anot, FONT_SIZE, TEXT_SPACING),
      0.5);
    DrawTextEx (font, anot,
                Vector2Subtract (vertex_screen_positions[i], center),
                FONT_SIZE, TEXT_SPACING, VERTEX_TEXT_COLOR);
    free (anot);
  }
}

int
main (void)
{
  startup ();
  while (!WindowShouldClose ()) {
    if (IsKeyDown (KEY_SPACE))
      start_time = GetTime ();
    handle_buttons ();
    progress_model ();

    // Find model space position supremums
    Vector2 pos_max = { -INFINITY, -INFINITY };
    Vector2 pos_min = {  INFINITY,  INFINITY };
    for (size_t i = 0; i < g.vertex_count; ++i) {
      if (model_vertices[i].x > pos_max.x) pos_max.x = model_vertices[i].x;
      if (model_vertices[i].y > pos_max.y) pos_max.y = model_vertices[i].y;
      if (model_vertices[i].x < pos_min.x) pos_min.x = model_vertices[i].x;
      if (model_vertices[i].y < pos_min.y) pos_min.y = model_vertices[i].y;
    }

    // Remap model space positions to screen space
    Vector2 vertex_screen_positions[g.vertex_count];
    for (size_t i = 0; i < g.vertex_count; ++i) {
      vertex_screen_positions[i] = (Vector2) {
        .x = Remap (model_vertices[i].x, pos_min.x, pos_max.x, 2*VERTEX_RADIUS + BUTTON_WIDTH + PADDING, GetScreenWidth  () - 2*VERTEX_RADIUS),
        .y = Remap (model_vertices[i].y, pos_min.y, pos_max.y, 2*VERTEX_RADIUS,                          GetScreenHeight () - 2*VERTEX_RADIUS),
      };
    }

    BeginDrawing ();
    ClearBackground (BACKGROUND_COLOR);
    draw_buttons ();
    draw_edges (vertex_screen_positions);
    draw_vertices (vertex_screen_positions);
    // DrawFPS (2, 0);
    EndDrawing ();
  } CloseWindow ();

  return EXIT_SUCCESS;
}