|
| 1 | +#include "config.h" |
| 2 | +#include <assert.h> |
| 3 | +#include <ccan/tal/tal.h> |
| 4 | +#include <common/setup.h> |
| 5 | +#include <inttypes.h> |
| 6 | +#include <plugins/askrene/graph.h> |
| 7 | +#include <stdio.h> |
| 8 | + |
| 9 | +#include "../algorithm.c" |
| 10 | + |
| 11 | +// 1->2 7 |
| 12 | +// 1->3 9 |
| 13 | +// 1->6 14 |
| 14 | +// 2->3 10 |
| 15 | +// 2->4 15 |
| 16 | +// 3->6 2 |
| 17 | +// 3->4 11 |
| 18 | +// 4->5 6 |
| 19 | +// 5->6 9 |
| 20 | + |
| 21 | +#define MAX_NODES 256 |
| 22 | +#define MAX_ARCS 256 |
| 23 | +#define DUAL_BIT 7 |
| 24 | + |
| 25 | +#define CHECK(arg) if(!(arg)){fprintf(stderr, "failed CHECK at line %d: %s\n", __LINE__, #arg); abort();} |
| 26 | + |
| 27 | +static void show(struct graph *graph, struct node node) |
| 28 | +{ |
| 29 | + printf("Showing node %" PRIu32 "\n", node.idx); |
| 30 | + for (struct arc arc = node_adjacency_begin(graph, node); |
| 31 | + !node_adjacency_end(arc); arc = node_adjacency_next(graph, arc)) { |
| 32 | + printf("arc id: %" PRIu32 ", (%" PRIu32 " -> %" PRIu32 ")\n", |
| 33 | + arc.idx, arc_tail(graph, arc).idx, |
| 34 | + arc_head(graph, arc).idx); |
| 35 | + } |
| 36 | + printf("\n"); |
| 37 | +} |
| 38 | + |
| 39 | +int main(int argc, char *argv[]) |
| 40 | +{ |
| 41 | + common_setup(argv[0]); |
| 42 | + printf("Allocating a memory context\n"); |
| 43 | + tal_t *ctx = tal(NULL, tal_t); |
| 44 | + assert(ctx); |
| 45 | + |
| 46 | + printf("Allocating a graph\n"); |
| 47 | + struct graph *graph = graph_new(ctx, MAX_NODES, MAX_ARCS, DUAL_BIT); |
| 48 | + assert(graph); |
| 49 | + |
| 50 | + s64 *capacity = tal_arrz(ctx, s64, MAX_ARCS); |
| 51 | + s64 *cost = tal_arrz(ctx, s64, MAX_ARCS); |
| 52 | + s64 *potential = tal_arrz(ctx, s64, MAX_NODES); |
| 53 | + s64 *distance = tal_arr(ctx, s64, MAX_NODES); |
| 54 | + struct arc *prev = tal_arr(ctx, struct arc, MAX_NODES); |
| 55 | + |
| 56 | + graph_add_arc(graph, arc_obj(0), node_obj(1), node_obj(2)); |
| 57 | + cost[0] = 7, capacity[0] = 1; |
| 58 | + graph_add_arc(graph, arc_obj(1), node_obj(1), node_obj(3)); |
| 59 | + cost[1] = 9, capacity[1] = 1; |
| 60 | + graph_add_arc(graph, arc_obj(2), node_obj(1), node_obj(6)); |
| 61 | + cost[2] = 14, capacity[2] = 1; |
| 62 | + graph_add_arc(graph, arc_obj(3), node_obj(2), node_obj(3)); |
| 63 | + cost[3] = 10, capacity[3] = 1; |
| 64 | + graph_add_arc(graph, arc_obj(4), node_obj(2), node_obj(4)); |
| 65 | + cost[4] = 15, capacity[4] = 1; |
| 66 | + graph_add_arc(graph, arc_obj(5), node_obj(3), node_obj(4)); |
| 67 | + cost[5] = 11, capacity[5] = 1; |
| 68 | + graph_add_arc(graph, arc_obj(6), node_obj(3), node_obj(6)); |
| 69 | + cost[6] = 2, capacity[6] = 1; |
| 70 | + graph_add_arc(graph, arc_obj(7), node_obj(4), node_obj(5)); |
| 71 | + cost[7] = 6, capacity[7] = 1; |
| 72 | + graph_add_arc(graph, arc_obj(8), node_obj(5), node_obj(6)); |
| 73 | + cost[8] = 9, capacity[8] = 1; |
| 74 | + |
| 75 | + show(graph, node_obj(1)); |
| 76 | + show(graph, node_obj(2)); |
| 77 | + show(graph, node_obj(3)); |
| 78 | + show(graph, node_obj(4)); |
| 79 | + show(graph, node_obj(5)); |
| 80 | + show(graph, node_obj(6)); |
| 81 | + |
| 82 | + struct node src = {.idx = 1}; |
| 83 | + struct node dst = {.idx = 6}; |
| 84 | + |
| 85 | + bool result = dijkstra_path(ctx, graph, src, dst, false, capacity, 1, |
| 86 | + cost, potential, prev, distance); |
| 87 | + CHECK(result); |
| 88 | + |
| 89 | + int pathlen = 0; |
| 90 | + int arc_sequence[] = {6, 1}; |
| 91 | + int node_sequence[] = {3, 1}; |
| 92 | + |
| 93 | + for (struct node cur = dst; cur.idx != src.idx;) { |
| 94 | + struct arc arc = prev[cur.idx]; |
| 95 | + printf("node(%" PRIu32 ") arc(%" PRIu32 ") - ", cur.idx, |
| 96 | + arc.idx); |
| 97 | + cur = arc_tail(graph, arc); |
| 98 | + CHECK(pathlen < 2); |
| 99 | + CHECK(cur.idx == node_sequence[pathlen]); |
| 100 | + CHECK(arc.idx == arc_sequence[pathlen]); |
| 101 | + pathlen++; |
| 102 | + } |
| 103 | + CHECK(pathlen == 2); |
| 104 | + |
| 105 | + for (size_t i = 1; i <= 6; i++) { |
| 106 | + printf("node: %zu, distance: %" PRIi64 "\n", i, distance[i]); |
| 107 | + } |
| 108 | + |
| 109 | + CHECK(distance[1] == 0); |
| 110 | + CHECK(distance[2] == 7); |
| 111 | + CHECK(distance[3] == 9); |
| 112 | + CHECK(distance[4] == 20); |
| 113 | + CHECK(distance[5] == 26); |
| 114 | + CHECK(distance[6] == 11); |
| 115 | + |
| 116 | + printf("Freeing memory\n"); |
| 117 | + ctx = tal_free(ctx); |
| 118 | + |
| 119 | + common_shutdown(); |
| 120 | + return 0; |
| 121 | +} |
0 commit comments