1 /* Licensed under LGPLv2+ - see LICENSE file for details */
5 * Abstract Graph Algorithms
7 * This module implements several standard algorithms on "abstract"
8 * (directed) graphs. That is to say rather than requiring a specific
9 * concrete representation of the graph, user-supplied callbacks allow
10 * the graph to be constructed as it is explored.
16 * Graph nodes are represented by 'struct aga_node'
18 * - These will often be embedded in a caller-specific structure
19 * (calling code can then locate its own structures using
22 * - Nodes are semi-persistent - they MAY be constructed on the fly by
23 * the edge_info callback (see below), but MUST then remain in place
24 * at least as long as the completion of the current graph
27 * - Nodes must be initialized with aga_node_init(), either up front,
28 * or as they are constructed on the fly.
30 * - The contents of the structure should be treated as opaque by the
37 * Graph edges are reported by three caller supplied functions,
38 * 'first_edge', 'next_edge' and 'edge_info'.
40 * - Edges are identified by a (void *)
41 * - The combination of a graph, a node and this (void *) MUST
42 * uniquely identify an edge
43 * - Different edges leading from different nodes MAY have the same
45 * - NULL has a special meaning (indicating there are no more edges
47 * - Otherwise, edge identifiers are treated as opaque by aga
49 * - Calling first_edge, followed by next_edge repeatedly must iterate
50 * through all the edges leading from node n.
52 * - Either first_edge or next_edge may return NULL indicating there
53 * are no further edges from this node
55 * - edge_info MAY return a negative value in case of error. This
56 * will generally abort any aga algorithm which encounters it.
58 * - Otherwise edge_info must return 0. Any other return value will
61 * - edge_info MAY set ei->to to NULL, indicating a "missing" edge,
62 * thus there MAY be more edge identifiers than actual edges from a
63 * given node. Otherwise, edge_info MUST fill in the ei->to field
64 * with a pointer to the destination node of the given edge
66 * - The ei->to field for a returned edge MAY point to an existing
67 * struct aga_node, or it MAY have just been allocated by the edge
68 * callback itself. If the latter, it MUST have been initialized
69 * with aga_node_init() before the edge callback returns.
71 * - If a node is contructed by the edge callback, any subsequent
72 * reference to that node by the edge callback for *any* node and
73 * index MUST use the same pointer.
75 * - The ei->icost field MAY be set by the edge_info callback to
76 * indicate the edge's cost / length represented as an integer (of
77 * type aga_icost_t), Otherwise the cost defaults to 1.
82 * - Because the algorithms implemented here keep state in the
83 * aga_node structures, only one algorithm can run at a time.
84 * Global state for algorithms is stored in the aga_graph structure.
86 * - When you start an algorithm (aga_*_start()) the graph is marked
89 * - Subsequent attempts to start an algorithm will fail;
90 * aga_*_start() will return -1.
92 * - To release the graph for another algorithm, use aga_finish().
94 * - Calling functions associated with one algorithm while another is
95 * running has undefined results.
100 * - Errors may be reported by the edge_info callback, or may be
101 * detected internally by algorithms.
103 * - Algorithms will generally stop running when they encounter an
104 * error; the call which detects the error and subsequent calls will
105 * return a "safe", but otherwise meaningless value.
107 * - After an error is encountered aga_error() will return a non-zero
108 * value. Negative values are reserved for errors reported by the
109 * user supplied edge callback. Positive values are reserved for
110 * errors detected interally by aga.
112 * - Errors are cleared on aga_finish().
117 #include <ccan/build_assert/build_assert.h>
118 #include <ccan/check_type/check_type.h>
119 #include <ccan/lstack/lstack.h>
120 #include <ccan/lqueue/lqueue.h>
128 typedef const void *(*aga_first_edge_fn)(const struct aga_graph *g,
129 const struct aga_node *n);
130 typedef const void *(*aga_next_edge_fn)(const struct aga_graph *g,
131 const struct aga_node *n,
134 typedef long aga_icost_t;
136 struct aga_edge_info {
138 aga_icost_t icost; /* integer edge cost */
141 typedef int (*aga_edge_info_fn)(const struct aga_graph *g,
142 const struct aga_node *n,
143 const void *e, struct aga_edge_info *ei);
146 * Internal data structures
153 struct lstack_link parent;
157 struct lqueue_link next;
167 aga_first_edge_fn first_edge;
168 aga_next_edge_fn next_edge;
169 aga_edge_info_fn edge_info;
177 * aga_init_graph - Initialize a new abstract graph
178 * @g: graph structure to initialize
179 * @first_edge: first edge callback
180 * @next_edge: next edge callback
181 * @edge_into: edge info callback
183 * Initialize @g to represent an abstract graph defined by the
184 * supplied edge callbacks
186 void aga_init_graph_(struct aga_graph *g,
187 aga_first_edge_fn first_edge,
188 aga_next_edge_fn next_edge,
189 aga_edge_info_fn edge_info);
190 #define aga_init_graph(g_, fefn_, nefn_, eifn_) \
192 struct aga_node *n_; \
193 struct aga_edge_info *ei_; \
194 BUILD_ASSERT(check_types_match((fefn_)((g_), n_), \
196 (fefn_)((g_), n_))) \
198 BUILD_ASSERT(check_type((eifn_)((g_), n_, \
199 (fefn_)((g_), n_), ei_), \
201 aga_init_graph_((g_), (aga_first_edge_fn)(fefn_), \
202 (aga_next_edge_fn)(nefn_), \
203 (aga_edge_info_fn)(eifn_)); \
207 * enum aga_error - Error codes for aga routines
209 * These error codes are returned by aga_error() for errors detected
210 * within aga itself (rather than errors reported by supplied
211 * callbacks, which should be negative
219 * aga_error - Determine error state of a graph
222 * Returns 0 if the graph is not in an error state, negative values
223 * for error states reported by one of the edge callbacks and
224 * postitive values for errors detected by aga itself.
226 int aga_error(const struct aga_graph *g);
229 * aga_node_init - Initialize a graph node
230 * @node: a graph node
232 * Initialize @node as a new graph node. This must be called before
233 * @node is passed to any aga function, or returned from an edge_info
234 * callback (in the ei->to field)
236 static inline void aga_node_init(struct aga_node *node)
238 memset(node, 0, sizeof(*node));
242 * aga_finish - Finish an aga algorithm
245 * Wraps up the aga algorithm currently running on @g. This will
246 * clear any error conditions. After this is called it is an error to
247 * call aga functions on @g apart from aga_*_start() and aga_error.
249 void aga_finish(struct aga_graph *g);
251 const void *aga_first_edge(const struct aga_graph *g, const struct aga_node *n);
252 const void *aga_next_edge(const struct aga_graph *g, const struct aga_node *n,
254 int aga_edge_info(const struct aga_graph *g, const struct aga_node *n,
255 const void *e, struct aga_edge_info *ei);
257 #define aga_for_each_edge(_e, _g, _n) \
258 for ((_e) = aga_first_edge((_g), (_n)); (_e); \
259 (_e) = aga_next_edge((_g), (_n), (_e)))
261 #define aga_for_each_edge_info(_e, _ei, _err, _g, _n) \
262 for ((_err) = 0, (_e) = aga_first_edge((_g), (_n)); \
263 (_e) && ((((_err) = aga_edge_info((_g), (_n), (_e), &(_ei)))) == 0); \
264 (_e) = aga_next_edge((_g), (_n), (_e))) \
272 * aga_dfs_start - Start a depth-first search
273 * @g: graph to search
275 * Begins the depth-first search algorithm on @g
277 int aga_dfs_start(struct aga_graph *g);
280 * aga_dfs_explore - One step of depth-first search
281 * @g: graph to search
282 * @n: node to start exploration from
284 * If @n has not yet been explored since aga_dfs_start(), returns @n.
285 * Otherwise returns the next node after @n in depth-first search
286 * order. Marks the returned node as explored.
288 struct aga_node *aga_dfs_explore(struct aga_graph *g, struct aga_node *n);
291 * aga_dfs - Depth-first search
292 * @_n: pointer to current node (output)
293 * @_g: graph to search
294 * @_start: node to start from
296 * Performs a depth first search. The block following this macro is
297 * executed with @_n set first to @_start, then to each node reachable
298 * from @_start in depth first search order.
300 * aga_dfs_start() must be called before this macro is used.
302 #define aga_dfs(_n, _g, _start) \
303 for ((_n) = (_start); ((_n) = aga_dfs_explore((_g), (_n))) != NULL; )
307 * Breadth first search
311 * aga_bfs_start - Start a breadth-first search
312 * @g: graph to search
314 * Begins the breadth-first search algorithm on @g
316 int aga_bfs_start(struct aga_graph *g);
319 * aga_bfs_explore - One step of breadth-first search
320 * @g: graph to search
321 * @n: node to start exploration from
323 * If @n has not yet been explored since aga_bfs_start(), returns @n.
324 * Otherwise returns the next node after @n in breadth-first search
325 * order. Marks the returned node as explored.
327 struct aga_node *aga_bfs_explore(struct aga_graph *g, struct aga_node *n);
330 * aga_bfs - Breadth-first search
331 * @_n: pointer to current node (output)
332 * @_g: graph to search
333 * @_start: node to start from
335 * Performs a breadth first search. The block following this macro is
336 * executed with @_n set first to @_start, then to each node reachable
337 * from @_start in depth first search order.
339 * aga_bfs_start() must be called before this macro is used.
341 #define aga_bfs(_n, _g, _start) \
342 for ((_n) = (_start) ; ((_n) = aga_bfs_explore((_g), (_n))) != NULL; )
344 #endif /* CCAN_AGA_H */