From 556fea05affe6fe8866557d42b6c1ef6559d7880 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sat, 7 Nov 2026 06:20:46 +0300 Subject: publish --- src/main.c | 580 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 580 insertions(+) create mode 100644 src/main.c (limited to 'src/main.c') 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 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +} -- cgit v1.3.1