1 /* Licensed under LGPLv2+ - see LICENSE file for details */
8 #include <ccan/aga/aga.h>
15 static bool dfs_push(struct aga_graph *g, struct lstack *stack,
18 if (!aga_update_node(g, n))
21 lstack_push(stack, n, u.dfs.parent);
22 n->u.dfs.edge = aga_first_edge(g, n);
26 static void dfs_pop(struct lstack *stack)
28 lstack_pop(stack, struct aga_node, u.dfs.parent);
31 static struct aga_node *dfs_top(struct lstack *stack)
33 return lstack_top(stack, struct aga_node, u.dfs.parent);
36 int aga_dfs_start(struct aga_graph *g)
47 struct aga_node *aga_dfs_explore(struct aga_graph *g, struct aga_node *n)
51 if (!aga_check_state(g))
57 if (dfs_push(g, &stack, n))
60 lstack_init_from_top(&stack, n, u.dfs.parent);
62 while ((n = dfs_top(&stack))) {
63 const void *e = n->u.dfs.edge;
65 struct aga_edge_info ei;
68 /* out of edges, back up */
73 n->u.dfs.edge = aga_next_edge(g, n, e);
75 err = aga_edge_info(g, n, e, &ei);
85 if (!dfs_push(g, &stack, ei.to)) {
86 /* already visited node */