| 28 | } |
| 29 | |
| 30 | int main(int argc, char *argv[]) |
| 31 | { |
| 32 | common_setup(argv[0]); |
| 33 | printf("Allocating a memory context\n"); |
| 34 | tal_t *ctx = tal(NULL, tal_t); |
| 35 | assert(ctx); |
| 36 | |
| 37 | printf("Allocating a graph\n"); |
| 38 | struct graph *graph = graph_new(ctx, MAX_NODES, MAX_ARCS, DUAL_BIT); |
| 39 | assert(graph); |
| 40 | |
| 41 | s64 *capacity = tal_arrz(ctx, s64, MAX_ARCS); |
| 42 | struct arc *prev = tal_arr(ctx, struct arc, MAX_NODES); |
| 43 | |
| 44 | graph_add_arc(graph, arc_obj(0), node_obj(1), node_obj(2)); |
| 45 | capacity[0] = 1; |
| 46 | graph_add_arc(graph, arc_obj(1), node_obj(1), node_obj(3)); |
| 47 | capacity[1] = 1; |
| 48 | graph_add_arc(graph, arc_obj(2), node_obj(1), node_obj(6)); |
| 49 | capacity[2] = 1; |
| 50 | graph_add_arc(graph, arc_obj(3), node_obj(2), node_obj(3)); |
| 51 | capacity[3] = 1; |
| 52 | graph_add_arc(graph, arc_obj(4), node_obj(2), node_obj(4)); |
| 53 | capacity[4] = 0; /* disable this arc */ |
| 54 | graph_add_arc(graph, arc_obj(5), node_obj(3), node_obj(4)); |
| 55 | capacity[5] = 1; |
| 56 | graph_add_arc(graph, arc_obj(6), node_obj(3), node_obj(6)); |
| 57 | capacity[6] = 1; |
| 58 | graph_add_arc(graph, arc_obj(7), node_obj(4), node_obj(5)); |
| 59 | capacity[7] = 1; |
| 60 | graph_add_arc(graph, arc_obj(8), node_obj(5), node_obj(6)); |
| 61 | capacity[8] = 1; |
| 62 | |
| 63 | show(graph, node_obj(1)); |
| 64 | show(graph, node_obj(2)); |
| 65 | show(graph, node_obj(3)); |
| 66 | show(graph, node_obj(4)); |
| 67 | show(graph, node_obj(5)); |
| 68 | show(graph, node_obj(6)); |
| 69 | |
| 70 | struct node src = {.idx = 1}; |
| 71 | struct node dst = {.idx = 5}; |
| 72 | |
| 73 | bool result = BFS_path(ctx, graph, src, dst, capacity, 1, prev); |
| 74 | assert(result); |
| 75 | |
| 76 | int pathlen = 0; |
| 77 | int arc_sequence[] = {7, 5, 1}; |
| 78 | int node_sequence[] = {4, 3, 1}; |
| 79 | |
| 80 | printf("path: "); |
| 81 | for (struct node cur = dst; cur.idx != src.idx;) { |
| 82 | struct arc arc = prev[cur.idx]; |
| 83 | printf("node(%" PRIu32 ") arc(%" PRIu32 ") - ", cur.idx, |
| 84 | arc.idx); |
| 85 | cur = arc_tail(graph, arc); |
| 86 | CHECK(pathlen < 3); |
| 87 | CHECK(cur.idx == node_sequence[pathlen]); |
nothing calls this directly
no test coverage detected