diff --git a/plugins/askrene/algorithm.c b/plugins/askrene/algorithm.c index 50a4f2565..4b3acc7a8 100644 --- a/plugins/askrene/algorithm.c +++ b/plugins/askrene/algorithm.c @@ -77,3 +77,95 @@ finish: tal_free(this_ctx); return target_found; } + +bool dijkstra_path(const tal_t *ctx, const struct graph *graph, + const struct node source, const struct node destination, + bool prune, const s64 *capacity, const s64 cap_threshold, + const s64 *cost, const s64 *potential, struct arc *prev, + s64 *distance) +{ + bool target_found = false; + const size_t max_num_arcs = graph_max_num_arcs(graph); + const size_t max_num_nodes = graph_max_num_nodes(graph); + tal_t *this_ctx = tal(ctx, tal_t); + + /* check preconditions */ + if (!graph || source.idx >=max_num_nodes || !cost || !capacity || + !prev || !distance) + goto finish; + + /* if prune is true then the destination cannot be invalid */ + if (destination.idx >=max_num_nodes && prune) + goto finish; + + if (tal_count(cost) != max_num_arcs || + tal_count(capacity) != max_num_arcs || + tal_count(prev) != max_num_nodes || + tal_count(distance) != max_num_nodes) + goto finish; + + /* FIXME: maybe this is unnecessary */ + bitmap *visited = tal_arrz(this_ctx, bitmap, + BITMAP_NWORDS(max_num_nodes)); + + if (!visited) + /* bad allocation */ + goto finish; + + for (size_t i = 0; i < max_num_nodes; ++i) + prev[i].idx = INVALID_INDEX; + + struct priorityqueue *q; + q = priorityqueue_new(this_ctx, max_num_nodes); + const s64 *const dijkstra_distance = priorityqueue_value(q); + + priorityqueue_init(q); + priorityqueue_update(q, source.idx, 0); + + while (!priorityqueue_empty(q)) { + const u32 cur = priorityqueue_top(q); + priorityqueue_pop(q); + + /* FIXME: maybe this is unnecessary */ + if (bitmap_test_bit(visited, cur)) + continue; + bitmap_set_bit(visited, cur); + + if (cur == destination.idx) { + target_found = true; + if (prune) + break; + } + + for (struct arc arc = + node_adjacency_begin(graph, node_obj(cur)); + !node_adjacency_end(arc); + arc = node_adjacency_next(graph, arc)) { + /* check if this arc is traversable */ + if (capacity[arc.idx] < cap_threshold) + continue; + + const struct node next = arc_head(graph, arc); + + const s64 cij = cost[arc.idx] - potential[cur] + + potential[next.idx]; + + /* Dijkstra only works with non-negative weights */ + assert(cij >= 0); + + if (dijkstra_distance[next.idx] <= + dijkstra_distance[cur] + cij) + continue; + + priorityqueue_update(q, next.idx, + dijkstra_distance[cur] + cij); + prev[next.idx] = arc; + } + } + for (size_t i = 0; i < max_num_nodes; i++) + distance[i] = dijkstra_distance[i]; + +finish: + tal_free(this_ctx); + return target_found; +} diff --git a/plugins/askrene/algorithm.h b/plugins/askrene/algorithm.h index 750e69163..ec571314c 100644 --- a/plugins/askrene/algorithm.h +++ b/plugins/askrene/algorithm.h @@ -33,4 +33,40 @@ bool BFS_path(const tal_t *ctx, const struct graph *graph, const struct node source, const struct node destination, const s64 *capacity, const s64 cap_threshold, struct arc *prev); + +/* Computes the distance from the source to every other node in the network + * using Dijkstra's algorithm. + * + * input: + * @ctx: tal context for internal allocation + * @graph: topological information of the graph + * @source: source node + * @destination: destination node + * @prune: if prune is true the algorithm stops when the optimal path is found + * for the destination node + * @capacity: arcs capacity + * @cap_threshold: an arc i is traversable if capacity[i]>=cap_threshold + * @cost: arc's cost + * @potential: nodes' potential, ie. reduced cost for an arc + * c_ij = cost_ij - potential[i] + potential[j] + * + * output: + * @prev: for each node, this is the arc that was used to arrive to it, this can + * be used to reconstruct the path from the destination to the source, + * @distance: node's best distance + * returns true if an optimal path is found for the destination, false otherwise + * + * precondition: + * |capacity|=|cost|=graph_max_num_arcs + * |prev|=|distance|=graph_max_num_nodes + * cost[i]>=0 + * if prune is true the destination must be valid + * */ +bool dijkstra_path(const tal_t *ctx, const struct graph *graph, + const struct node source, const struct node destination, + bool prune, const s64 *capacity, const s64 cap_threshold, + const s64 *cost, const s64 *potential, struct arc *prev, + s64 *distance); + + #endif /* LIGHTNING_PLUGINS_ASKRENE_ALGORITHM_H */ diff --git a/plugins/askrene/test/Makefile b/plugins/askrene/test/Makefile index ced945fca..1e1231949 100644 --- a/plugins/askrene/test/Makefile +++ b/plugins/askrene/test/Makefile @@ -10,7 +10,7 @@ $(PLUGIN_RENEPAY_TEST_OBJS): $(PLUGIN_ASKRENE_SRC) PLUGIN_ASKRENE_TEST_COMMON_OBJS := -plugins/askrene/test/run-bfs: \ +plugins/askrene/test/run-bfs plugins/askrene/test/run-dijkstra: \ plugins/askrene/priorityqueue.o \ plugins/askrene/graph.o diff --git a/plugins/askrene/test/run-dijkstra.c b/plugins/askrene/test/run-dijkstra.c new file mode 100644 index 000000000..4c2e9cd39 --- /dev/null +++ b/plugins/askrene/test/run-dijkstra.c @@ -0,0 +1,121 @@ +#include "config.h" +#include +#include +#include +#include +#include +#include + +#include "../algorithm.c" + +// 1->2 7 +// 1->3 9 +// 1->6 14 +// 2->3 10 +// 2->4 15 +// 3->6 2 +// 3->4 11 +// 4->5 6 +// 5->6 9 + +#define MAX_NODES 256 +#define MAX_ARCS 256 +#define DUAL_BIT 7 + +#define CHECK(arg) if(!(arg)){fprintf(stderr, "failed CHECK at line %d: %s\n", __LINE__, #arg); abort();} + +static void show(struct graph *graph, struct node node) +{ + printf("Showing node %" PRIu32 "\n", node.idx); + for (struct arc arc = node_adjacency_begin(graph, node); + !node_adjacency_end(arc); arc = node_adjacency_next(graph, arc)) { + printf("arc id: %" PRIu32 ", (%" PRIu32 " -> %" PRIu32 ")\n", + arc.idx, arc_tail(graph, arc).idx, + arc_head(graph, arc).idx); + } + printf("\n"); +} + +int main(int argc, char *argv[]) +{ + common_setup(argv[0]); + printf("Allocating a memory context\n"); + tal_t *ctx = tal(NULL, tal_t); + assert(ctx); + + printf("Allocating a graph\n"); + struct graph *graph = graph_new(ctx, MAX_NODES, MAX_ARCS, DUAL_BIT); + assert(graph); + + s64 *capacity = tal_arrz(ctx, s64, MAX_ARCS); + s64 *cost = tal_arrz(ctx, s64, MAX_ARCS); + s64 *potential = tal_arrz(ctx, s64, MAX_NODES); + s64 *distance = tal_arr(ctx, s64, MAX_NODES); + struct arc *prev = tal_arr(ctx, struct arc, MAX_NODES); + + graph_add_arc(graph, arc_obj(0), node_obj(1), node_obj(2)); + cost[0] = 7, capacity[0] = 1; + graph_add_arc(graph, arc_obj(1), node_obj(1), node_obj(3)); + cost[1] = 9, capacity[1] = 1; + graph_add_arc(graph, arc_obj(2), node_obj(1), node_obj(6)); + cost[2] = 14, capacity[2] = 1; + graph_add_arc(graph, arc_obj(3), node_obj(2), node_obj(3)); + cost[3] = 10, capacity[3] = 1; + graph_add_arc(graph, arc_obj(4), node_obj(2), node_obj(4)); + cost[4] = 15, capacity[4] = 1; + graph_add_arc(graph, arc_obj(5), node_obj(3), node_obj(4)); + cost[5] = 11, capacity[5] = 1; + graph_add_arc(graph, arc_obj(6), node_obj(3), node_obj(6)); + cost[6] = 2, capacity[6] = 1; + graph_add_arc(graph, arc_obj(7), node_obj(4), node_obj(5)); + cost[7] = 6, capacity[7] = 1; + graph_add_arc(graph, arc_obj(8), node_obj(5), node_obj(6)); + cost[8] = 9, capacity[8] = 1; + + show(graph, node_obj(1)); + show(graph, node_obj(2)); + show(graph, node_obj(3)); + show(graph, node_obj(4)); + show(graph, node_obj(5)); + show(graph, node_obj(6)); + + struct node src = {.idx = 1}; + struct node dst = {.idx = 6}; + + bool result = dijkstra_path(ctx, graph, src, dst, false, capacity, 1, + cost, potential, prev, distance); + CHECK(result); + + int pathlen = 0; + int arc_sequence[] = {6, 1}; + int node_sequence[] = {3, 1}; + + for (struct node cur = dst; cur.idx != src.idx;) { + struct arc arc = prev[cur.idx]; + printf("node(%" PRIu32 ") arc(%" PRIu32 ") - ", cur.idx, + arc.idx); + cur = arc_tail(graph, arc); + CHECK(pathlen < 2); + CHECK(cur.idx == node_sequence[pathlen]); + CHECK(arc.idx == arc_sequence[pathlen]); + pathlen++; + } + CHECK(pathlen == 2); + + for (size_t i = 1; i <= 6; i++) { + printf("node: %zu, distance: %" PRIi64 "\n", i, distance[i]); + } + + CHECK(distance[1] == 0); + CHECK(distance[2] == 7); + CHECK(distance[3] == 9); + CHECK(distance[4] == 20); + CHECK(distance[5] == 26); + CHECK(distance[6] == 11); + + printf("Freeing memory\n"); + ctx = tal_free(ctx); + + common_shutdown(); + return 0; +}