docstring tweaks to source-edges, target-edges, and friends
[cl-graph.git] / dev / api.lisp
1 (in-package #:cl-graph)
2
3 ;;; ---------------------------------------------------------------------------
4
5 (defgeneric make-vertex-container (graph initial-size)
6   (:documentation "Make-vertex-container is called during graph creation and can be used to create specialized containers to hold graph vertexes."))
7
8 ;;; ---------------------------------------------------------------------------
9
10 (defgeneric make-edge-container (graph initial-size)
11   (:documentation "Make-edge-container is called during graph creation and can be used to create specialized containers to hold graph edges."))
12
13 ;;; ---------------------------------------------------------------------------
14 ;;; API
15 ;;; ---------------------------------------------------------------------------
16
17 (defgeneric make-graph (graph-type &key &allow-other-keys)
18   (:documentation "Create a new graph of type `graph-type'. Graph type can be 
19 a symbol naming a sub-class of basic-graph or a list. If it is a list of symbols naming
20 different classes. If graph-type is a list, then a class which has all of the listed 
21 classes as superclasses will be found (or created). In either case, the new graph will
22 be created as if with a call to make-instance."))
23
24 ;;; ---------------------------------------------------------------------------
25
26 (defgeneric make-edge-for-graph (graph vertex-1 vertex-2 
27                                        &key edge-type edge-class &allow-other-keys)
28   (:documentation "It should not usually necessary to call this in user code. Creates a new edge between vertex-1 and vertex-2 for the graph. If the edge-type and edge-class are not specified, they will be determined from the defaults of the graph."))
29
30 ;;; ---------------------------------------------------------------------------
31
32 (defgeneric add-edge (graph edge &rest args &key force-new?)
33   (:documentation "Add-edge adds an existing edge to a graph. As add-edge-between-vertexes is generally more natural to use, this method is rarely called."))
34
35 ;;; ---------------------------------------------------------------------------
36
37 (defgeneric add-edge-between-vertexes (graph value-or-vertex-1 value-or-vertex-2
38                                               &rest args &key if-duplicate-do
39                                               edge-type &allow-other-keys)
40   (:documentation "Adds an edge between two vertexes and returns it.  
41 If force-new? is true, 
42 the edge is added even if one already exists. 
43 If the vertexes are not 
44 found in the graph, they will be added \(unless :error-if-not-found? is
45 true\). The class of the edge can be specified using :edge-class or
46 :edge-type. If :edge-type is used, it can be either :directed or 
47 :undirected; the actual class of the edge will be determined by using
48 the edge-types of the graph. If neither :edge-type nor :edge-class is
49 specified, then a directed edge will be created.
50
51 If-duplicate-do is used when the 'same' edge is added more than once. It can be
52 either a function on one variable or :ignore or :force. If it is :ignore, then
53 the previously added edge is returned; if it is :force, then another edge is
54 added between the two vertexes; if it is a function, then this function will
55 be called with the previous edge."))
56
57 ;;; ---------------------------------------------------------------------------
58
59 (defgeneric delete-edge (graph edge)
60   (:documentation "Delete the `edge' from the `graph' and returns it."))
61
62 ;;; ---------------------------------------------------------------------------
63
64 (defgeneric delete-edge-between-vertexes (graph value-or-vertex-1
65                                                 value-or-vertex-2 &rest args)
66   (:documentation "Finds an edge in the graph between the two specified vertexes. If values (i.e., non-vertexes) are passed in, then the graph will be searched for matching vertexes."))
67
68 ;;; ---------------------------------------------------------------------------
69
70 (defgeneric add-vertex (graph value-or-vertex &key if-duplicate-do &allow-other-keys)
71   (:documentation  "Adds a vertex to a graph. If called with a vertex, then this vertex is added. If called with a value, then a new vertex is created to hold the value. If-duplicate-do can be one of :ignore, :force, :replace, :replace-value or a function. The default is :ignore."))
72
73 ;;; ---------------------------------------------------------------------------
74
75 (defgeneric delete-vertex (graph value-or-vertex)
76   (:documentation "Remove a vertex from a graph. The 'vertex-or-value' argument can be
77 a vertex of the graph or a 'value' that will find a vertex via a call to find-vertex. A
78 graph-vertex-not-found-error will be raised if the vertex is not found or is not part of
79 the graph."))
80
81 ;;; ---------------------------------------------------------------------------
82
83 (defgeneric find-vertex (graph value &optional error-if-not-found?)
84   (:documentation "Search 'graph' for a vertex with element 'value'. The search is fast but inflexible because it uses an associative-container. If you need more flexibity, see search-for-vertex."))
85
86 ;;; ---------------------------------------------------------------------------
87
88 (defgeneric search-for-vertex (graph value &key key test error-if-not-found?)
89   (:documentation "Search 'graph' for a vertex with element 'value'. The 'key' function is applied to each element before that element is compared with the value. The comparison is done using the function 'test'. If you don't need to use key or test, then consider using find-vertex instead."))
90
91 ;;; ---------------------------------------------------------------------------
92
93 (defgeneric find-edge (graph edge &optional error-if-not-found?)
94   (:documentation "Search `graph` for an edge whose vertexes match `edge`. This means that `vertex-1` of the edge in the graph must match `vertex-1` of `edge` and so forth. Wil signal an error of type `graph-edge-not-found-error` unless `error-if-not-found?` is nil. [?? Unused. Remove?]"))
95
96 ;;; ---------------------------------------------------------------------------
97
98 (defgeneric find-edge-between-vertexes (graph value-or-vertex-1 value-or-vertex-2
99                                               &key error-if-not-found?)
100   (:documentation "Searches `graph` for an edge that connects vertex-1 and vertex-2.  [?? Ignores error-if-not-found? Does directedness matter? need test]"))
101
102 ;;; ---------------------------------------------------------------------------
103
104 (defgeneric source-vertex (edge)
105   (:documentation "Returns the source-vertex of a directed edge. Compare with `vertex-1`."))
106
107 ;;; ---------------------------------------------------------------------------
108
109 (defgeneric target-vertex (edge)
110   (:documentation "Returns the target-vertex of a directed edge. Compare with `vertex-2`."))
111
112 ;;; ---------------------------------------------------------------------------
113
114 (defgeneric iterate-edges (graph-or-vertex fn)
115   (:documentation "Calls `fn` on each edge of graph or vertex."))
116
117 ;;; ---------------------------------------------------------------------------
118
119 (defgeneric iterate-source-edges (vertex fn)
120   (:documentation "In a directed graph, calls `fn` on each edge of a vertex that begins at vertex. In an undirected graph, this is equivalent to `iterate-edges`."))
121
122 ;;; ---------------------------------------------------------------------------
123
124 (defgeneric iterate-target-edges (vertex fn)
125   (:documentation "In a directed graph, calls `fn` on each edge of a vertex that ends at vertex. In an undirected graph, this is equivalent to `iterate-edges`."))
126
127 ;;; ---------------------------------------------------------------------------
128
129 (defgeneric iterate-children (vertex fn)
130   (:documentation "Calls fn on every vertex that is either connected to vertex by an undirected edge or is at the target end of a directed edge."))
131
132 ;;; ---------------------------------------------------------------------------
133
134 (defgeneric has-children-p (vertex)
135   (:documentation "In a directed graph, returns true if vertex has any edges that point from vertex to some other vertex (cf. iterate-target-edges). In an undirected graph, `has-children-p` is testing only whether or not the vertex has any edges."))
136
137 ;;; ---------------------------------------------------------------------------
138
139 (defgeneric has-parent-p (vertex)
140   (:documentation "In a directed graph, returns true if vertex has any edges that point from some other vertex to this vertex (cf. iterate-source-edges). In an undirected graph, `has-parent-p` is testing only whether or not the vertex has any edges."))
141
142 ;;; ---------------------------------------------------------------------------
143
144 (defgeneric iterate-parents (vertex fn)
145   (:documentation "Calls fn on every vertex that is either connected to vertex by an undirected edge or is at the source end of a directed edge."))
146
147 ;;; ---------------------------------------------------------------------------
148
149 (defgeneric iterate-neighbors (vertex fn)
150   (:documentation "Calls fn on every vertex adjecent to vertex See also iterate-children and iterate-parents."))
151
152 ;;; ---------------------------------------------------------------------------
153
154 (defgeneric renumber-vertexes (graph)
155   (:documentation "Assign a number to each vertex in a graph in some unspecified order. [?? internal]"))
156
157 ;;; ---------------------------------------------------------------------------
158
159 (defgeneric renumber-edges (graph)
160   (:documentation "Assign a number to each edge in a graph in some unspecified order. [?? internal]"))
161
162 ;;; ---------------------------------------------------------------------------
163
164 (defgeneric generate-directed-free-tree (graph root)
165   (:documentation "Returns a version of graph which is a directed free tree
166 rooted at root."))
167
168 ;;; ---------------------------------------------------------------------------
169
170 (defgeneric in-undirected-cycle-p (graph start-vertex &optional marked previous)
171   (:documentation "Return true if-and-only-if an undirected cycle in graph is reachable from start-vertex."))
172
173 ;;; ---------------------------------------------------------------------------
174
175 (defgeneric undirected-edge-p (edge)
176   (:documentation "Returns true if-and-only-if edge is undirected"))
177
178 ;;; ---------------------------------------------------------------------------
179
180 (defgeneric directed-edge-p (edge)
181   (:documentation "Returns true if-and-only-if edge is directed"))
182
183 ;;; ---------------------------------------------------------------------------
184
185 (defgeneric tagged-edge-p (edge)
186   (:documentation "Returns true if-and-only-if edge's tag slot is t"))
187
188 ;;; ---------------------------------------------------------------------------
189
190 (defgeneric untagged-edge-p (edge)
191   (:documentation "Returns true if-and-only-if edge's tage slot is nil"))
192           
193 ;;; ---------------------------------------------------------------------------
194
195 (defgeneric adjacentp (graph vertex-1 vertex-2)
196   (:documentation "Return true if vertex-1 and vertex-2 are connected by an edge. [?? compare with vertices-share-edge-p and remove one or maybe call one directed-adjacentp]"))
197
198 ;;; ---------------------------------------------------------------------------
199
200 (defgeneric make-filtered-graph (old-graph test-fn &key
201                                            graph-completion-method depth
202                                            new-graph)
203   (:documentation "Takes a GRAPH and a TEST-FN (a single argument function
204 returning NIL or non-NIL), and filters the graph nodes according to 
205 the test-fn (those that return non-NIL are accepted), returning 
206 a new graph with only nodes corresponding to those in the 
207 original graph that satisfy the test (the nodes in the new graph 
208 are new, but their values and name point to the same contents of 
209 the original graph).  There are four options for how the new 
210 graph is filled-out, depending on the following keywords passed 
211 to the optional GRAPH-COMPLETION-METHOD argument:
212
213 *  NIL (default)    
214
215      New graph has only nodes that correspond to those in
216        the original graph that pass the test.  NO LINKS are
217        reproduced.
218
219 *  :COMPLETE-LINKS  
220
221      New graph has only nodes that pass, but reproduces 
222        corresponding links between passing nodes in the
223        original graph.
224
225 *  :COMPLETE-CLOSURE-NODES-ONLY
226
227      New graph also includes nodes corresponding to the 
228        transitive closure(s) that include the passign nodes 
229        in the original graph.  NO LINKS are reproduced.
230
231 *  :COMPLETE-CLOSURE-WITH-LINKS
232
233      Same as above, except corresponding links are reproduced.
234
235 For both transitive closure options, an additional optional argument,
236 DEPTH, specifies how many links away from a source vertex to travel 
237 in gathering vertexes of the closure.  E.g., a depth of 1 returns the 
238 source vertex and the parents and children of that vertex (all vertexes
239 one link away from the source).  The default value is NIL, indicating
240 that all vertexes are to be included, no matter their depth.  This
241 value is ignored in non closure options."))
242
243 ;;; ---------------------------------------------------------------------------
244
245 (defgeneric project-bipartite-graph  
246   (new-graph existing-graph vertex-class vertex-classifier)
247   (:documentation "Creates the unimodal bipartite projects of existing-graph with
248 vertexes for each vertex of existing graph whose `vertex-classifier` is eq to `vertex-class` and where an edge existing between two vertexes of the graph if and only if they are connected to a shared vertex in the existing-graph."))
249
250 ;;; ---------------------------------------------------------------------------
251
252 (defgeneric assortativity-coefficient (mixing-matrix)
253   (:documentation "An assortative graph is one where vertexes of the same type are more likely to 
254 have edges between them. The \(discrete\) assortativity-coefficient measures how
255 assortative a graph is based on its mixing matrix. The definition we use is from 
256 Mixing Patterns in Networks by Mark Newman. See the citation 'newman200-mixing' in moab 
257 or the URL 'http://arxiv.org/abs/cond-mat/0209450'."))
258
259 ;;; ---------------------------------------------------------------------------
260
261 (defgeneric graph->dot (graph output
262                        &key 
263                        graph-formatter
264                        vertex-key
265                        vertex-labeler
266                        vertex-formatter
267                        edge-labeler 
268                        edge-formatter)
269   (:documentation 
270    "Generates a description of `graph` in DOT file format. The formatting can be altered using `graph->dot-properties,` `vertex->dot,` and `edge->dot` as well as `edge-formatter,` `vertex-formatter,` `vertex-labeler,` and `edge-labeler`. These can be specified directly in the call to `graph->dot` or by creating subclasses of basic-graph, basic-vertex and basic-edge. 
271
272 The output can be a stream or pathname or one of the values `nil` or `t`. If output is `nil`, then graph->dot returns a string containing the DOT description. If it is `t`, then the DOT description is written to *standard-output*.
273
274 Here is an example;
275
276     (let ((g (make-container 'graph-container :default-edge-type :directed)))
277       (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
278             (add-edge-between-vertexes g a b))
279       (graph->dot g nil))
280
281     \"digraph G {
282     E []
283     C []
284     B []
285     A []
286     D []
287     F []
288     E->F []
289     B->C []
290     B->D []
291     A->B []
292     D->E []
293     D->F []
294     }\"
295
296 For more information about DOT file format, search the web for 'DOTTY' and 
297 'GRAPHVIZ'."))
298
299 ;;; ---------------------------------------------------------------------------
300
301 (defgeneric graph->dot-properties (g stream)
302   (:documentation "Unless a different graph-formatter is specified, this method is called by graph->dot to output graph-properties onto a stream. The function can assume that the openning and closing brackets will be taken care of by the graph->dot."))
303
304 ;;; ---------------------------------------------------------------------------
305
306 (defgeneric vertex->dot (vertex stream)
307   (:documentation "Unless a different vertex-formatter is specified with a keyword argument, this is used by graph->dot to output vertex formatting for `vertex` onto the `stream`. The function can assume that openning and closing square brackets and label have already been taken care of."))
308
309 ;;; ---------------------------------------------------------------------------
310
311 (defgeneric edge->dot (edge stream)
312   (:documentation "Used by graph->dot to output edge formatting for `edge` onto the `stream`. The function can assume that openning and closing square brackets and label have already been taken care of."))
313
314 ;;; ---------------------------------------------------------------------------
315
316 (defgeneric generate-gnm (generator graph n m &key)
317   (:documentation "Generate a 'classic' random graph G(n, m) with n vertexes and m edges."))
318
319 ;;; ---------------------------------------------------------------------------
320
321 (defgeneric generate-gnp (generator graph n p &key)
322   (:documentation  "Generate the Erd\"os-R\'enyi random graph G\(n, p\). I.e., a graph with n vertexes where
323 each possible edge appears with probability p. This implementation is from Efficient Generation
324 of Large Random Networks \(see batagelj-generation-2005 in doab\)."))
325
326 ;;; ---------------------------------------------------------------------------
327
328 (defgeneric generate-undirected-graph-via-assortativity-matrix 
329   (generator graph-class size edge-count kind-matrix assortativity-matrix
330              vertex-labeler &key)
331   (:documentation "This generates a random graph with 'size' vertexes. 
332 The vertexes can be in multiple different classes and the number of vertexes in each class is specified in the 'kind-matrix'. If the matrix has all fixnums, then it specifies the counts and these should add up to the size. If the kind-matrix contains non-fixnums then the values are treated as ratios.
333
334 The assortativity-matrix specifies the number of edges between vertexes of different kinds.
335
336 The vertex-labeler is a function of two parameters: the vertex kind and the index. It should return whatever the 'value' of the vertex ought to be."))
337
338 ;;; ---------------------------------------------------------------------------
339
340 (defgeneric generate-undirected-graph-via-vertex-probabilities 
341   (generator graph-class size kind-matrix probability-matrix vertex-labeler)
342   (:documentation   "Generate an Erd\"os-R/'enyi like random graph having multiple vertex kinds. See the function
343 Gnp for the simple one vertex kind method.
344
345 Generator and graph-class specify the random number generator used and the class of the graph produced; Size
346 the number of vertexes. Kind matrix is a vector of ratios specifying the distribution of vertex kinds {0 ... \(1- k\)}.
347 The probability-matrix is a k x k matrix specifying the probability that two vertexes of the row-kind and the
348 column-kind will have an edge between them. vertex-labeler is a function of two arguments \(the kind and the vertex number\)
349 called to create values for vertexes. It will be called only once for each vertex created.
350
351 The clever sequential sampling technique in this implementation is from Efficient Generation
352 of Large Random Networks \(see batagelj-generation-2005 in moab\)."))
353
354 ;;; ---------------------------------------------------------------------------
355
356 (defgeneric generate-scale-free-graph 
357   (generator graph size kind-matrix add-edge-count
358              other-vertex-kind-samplers vertex-labeler &key)
359   (:documentation "Generates a 'scale-free' graph using preferential attachment -- too damn slow.
360
361 Size is the number of vertexes; 
362 kind-matrix is as in generate-undirected-graph-via-assortativity-matrix, etc.;
363 add-edge-count is the number of edges to add for each vertex;
364 other-vertex-kind-samplers are confusing...; and
365 vertex-labeler is used to create vertex elements \(as in other generators\)."))
366
367 ;;; ---------------------------------------------------------------------------
368
369 (defgeneric generate-assortative-graph-with-degree-distributions
370   (generator graph
371              edge-count assortativity-matrix
372              average-degrees
373              degree-distributions
374              vertex-labeler
375              &key)
376   (:documentation ""))
377
378 ;;; ---------------------------------------------------------------------------
379
380 (defgeneric generate-simple-preferential-attachment-graph (generator graph size minimum-degree)
381   (:documentation "Generate a simple scale-free graph using the preferential attachment
382 mechanism of Barabasi and Albert. The implementation is from Efficient Generation
383 of Large Random Networks \(see batagelj-generation-2005 in moab\). Self-edges are possible."))
384
385 ;;; ---------------------------------------------------------------------------
386
387 (defgeneric generate-preferential-attachment-graph 
388   (generator graph size kind-matrix minimum-degree 
389              assortativity-matrix 
390              &key)
391   (:documentation "Generate a Barabasi-Albert type scale free graph with multiple vertex kinds.
392
393 The idea behind this implementation is from Efficient Generation
394 of Large Random Networks \(see batagelj-generation-2005 in moab\)."))
395
396
397 ;;; ---------------------------------------------------------------------------
398 ;;; more
399 ;;; ---------------------------------------------------------------------------
400
401 (defgeneric make-vertex-for-graph (graph &key &allow-other-keys)
402   (:documentation "Creates a new vertex for graph `graph`. The keyword arguments include:
403
404 * vertex-class : specify the class of the vertex
405 * element      : specify the `element` of the vertex
406
407 and any other initialization arguments that make sense for the vertex class."))
408
409 ;;; ---------------------------------------------------------------------------
410
411 (defgeneric tag-all-edges (thing)
412   (:documentation "Sets the `tag` of all the edges of `thing` to true. [?? why does this exist?\]"))
413
414 ;;; ---------------------------------------------------------------------------
415
416 (defgeneric untag-all-edges (thing)
417   (:documentation "Sets the `tag` of all the edges of `thing` to nil.  [?? why does this exist?\]"))
418
419 ;;; ---------------------------------------------------------------------------
420
421 (defgeneric untag-edges (edges)
422   (:documentation "Sets the `tag` of all the edges of `thing` to true. [?? why does this exist?\]"))
423
424 ;;; ---------------------------------------------------------------------------
425
426 (defgeneric tag-edges (edges)
427   (:documentation "Sets the `tag` of all the edges of `thing` to true. [?? why does this exist?\]"))
428
429 ;;; ---------------------------------------------------------------------------
430
431 (defgeneric replace-vertex (graph old new)
432   (:documentation "Replace vertex `old` in graph `graph` with vertex `new`. The edge structure of the graph is maintained."))
433
434 ;;; ---------------------------------------------------------------------------
435
436 (defgeneric add-edge-to-vertex (edge vertex)
437   (:documentation "Attaches the edge `edge` to the vertex `vertex`."))
438
439 ;;; ---------------------------------------------------------------------------
440
441 (defgeneric source-edges (vertex &optional filter)
442   (:documentation "Returns a list of the source edges of `vertex`. I.e., 
443 the edges that begin at `vertex`."))
444
445 ;;; ---------------------------------------------------------------------------
446
447 (defgeneric target-edges (vertex &optional filter)
448   (:documentation "Returns a list of the target edges of `vertex`. 
449 I.e., the edges that end at `vertex`."))
450
451 ;;; ---------------------------------------------------------------------------
452
453 (defgeneric child-vertexes (vertex &optional filter)
454   (:documentation "Returns a list of the vertexes to which `vertex` 
455 is connected by an edge and for which `vertex` is the source vertex. 
456 If the connecting edge is undirected, then the vertex is always 
457 counted as a source. [?? Could be a defun]."))
458
459 ;;; ---------------------------------------------------------------------------
460
461 (defgeneric parent-vertexes (vertex &optional filter)
462   (:documentation "Returns a list of the vertexes to which `vertex` is connected by an edge and for which `vertex` is the target vertex. If the connecting edge is undirected, then the vertex is always counted as a target. [?? Could be a defun]."))
463
464 ;;; ---------------------------------------------------------------------------
465
466 (defgeneric neighbor-vertexes (vertex &optional filter)
467   (:documentation "Returns a list of the vertexes to which `vertex` is connected by an edge disregarding edge direction. In a directed graph, neighbor-vertexes is the union of parent-vertexes and child-vertexes. [?? Could be a defun]."))
468
469 ;;; ---------------------------------------------------------------------------
470
471 (defgeneric number-of-neighbors (vertex)
472   (:documentation "Returns the number of neighbors of `vertex` (cf. `neighbor-vertexes`). [?? could be a defun]"))
473
474 ;;; ---------------------------------------------------------------------------
475
476 (defgeneric in-cycle-p (graph start-vertex)
477   (:documentation "Returns true if `start-vertex` is in some cycle in `graph`. This uses child-vertexes to generate the vertexes adjacent to a vertex."))
478
479 ;;; ---------------------------------------------------------------------------
480
481 (defgeneric iterate-vertexes (thing fn)
482   (:documentation "Calls `fn` on each of the vertexes of `thing`."))
483
484 ;;; ---------------------------------------------------------------------------
485
486 (defgeneric edges (thing)
487   (:documentation "Returns a list of the edges of `thing`."))
488
489 ;;; ---------------------------------------------------------------------------
490
491 (defgeneric vertex-count (graph)
492   (:documentation "Returns the number of vertexes in `graph`. [?? could be a defun]"))
493
494 ;;; ---------------------------------------------------------------------------
495
496 (defgeneric vertexes (thing)
497   (:documentation "Returns a list of the vertexes of `thing`."))
498
499 ;;; ---------------------------------------------------------------------------
500
501 (defgeneric source-edge-count (vertex)
502   (:documentation "Returns the number of source edges of vertex (cf. source-edges). [?? could be a defun]"))
503
504 ;;; ---------------------------------------------------------------------------
505
506 (defgeneric target-edge-count (vertex)
507   (:documentation "Returns the number of target edges of vertex (cf. target-edges). [?? could be a defun]"))
508
509 ;;; ---------------------------------------------------------------------------
510
511 (defgeneric graph-roots (graph)
512   (:documentation "Returns a list of the roots of graph. A root is defined as a vertex with no source edges \(i.e., all of the edges are out-going\). (cf. rootp) [?? could be a defun]"))
513
514 ;;; ---------------------------------------------------------------------------
515
516 (defgeneric rootp (vertex)
517   (:documentation "Returns true if `vertex` is a root vertex \(i.e., it has no incoming \(source\) edges\)."))
518
519 ;;; ---------------------------------------------------------------------------
520
521 (defgeneric find-vertex-if (thing predicate &key key)
522   (:documentation "Returns the first vertex in `thing` for which the `predicate` function returns non-nil. If the `key` is supplied, then it is applied to the vertex before the predicate is."))
523
524 ;;; ---------------------------------------------------------------------------
525
526 (defgeneric find-edge-if (graph fn &key key)
527   (:documentation "Returns the first edge in `thing` for which the `predicate` function returns non-nil. If the `key` is supplied, then it is applied to the edge before the predicate is."))
528
529 ;;; ---------------------------------------------------------------------------
530
531 (defgeneric find-edges-if (thing predicate)
532   (:documentation "Returns a list of edges in `thing` for which the `predicate` returns non-nil. [?? why no key function?]"))
533
534 ;;; ---------------------------------------------------------------------------
535
536 (defgeneric find-vertexes-if (thing predicate)
537   (:documentation "Returns a list of vertexes in `thing` for which the `predicate` returns non-nil. [?? why no key function?]"))
538
539 ;;; ---------------------------------------------------------------------------
540
541 (defgeneric force-undirected (graph)
542   (:documentation "Ensures that the graph is undirected (possibly by calling change-class on the edges)."))
543
544 ;;; ---------------------------------------------------------------------------
545
546 (defgeneric traverse-elements (thing style fn)
547   (:documentation "WIP"))
548
549 (defgeneric traverse-elements-helper (thing style marker fn)
550   (:documentation "WIP"))
551
552 (defgeneric any-undirected-cycle-p (graph)
553   (:documentation "Returns true if there are any undirected cycles in `graph`."))
554
555 (defgeneric edge-count (vertex)
556   (:documentation "Returns the number of edges attached to `vertex`. Compare with the more flexible `vertex-degree`."))
557
558 (defgeneric topological-sort (graph)
559   (:documentation "Returns a list of vertexes sorted by the depth from the roots of the graph. See also assign-level and graph-roots."))
560
561 (defgeneric assign-level (vertex level)
562   (:documentation "Sets the depth of `vertex` to level and then recursively sets the depth of all of the children of `vertex` to \(1+ level\)."))
563
564 (defgeneric depth (graph)
565   (:documentation "Returns the maximum depth of the vertexes in graph assuming that the roots are of depth 0 and that each edge distance from the roots increments the depth by one."))
566
567 (defgeneric make-vertex-edges-container (vertex container-class &rest args)
568   (:documentation "Called during the initialization of a vertex to create the create the container used to store the edges incident to the vertex. The initarg :vertex-edges-container-class can be used to alter the default container class."))
569
570 (defgeneric other-vertex (edge value-or-vertex)
571   (:documentation "Assuming that the value-or-vertex corresponds to one of the vertexes for `edge`, this method returns the other vertex of `edge`. If the value-or-vertex is not part of edge, then an error is signaled. [?? Should create a new condition for this]"))
572
573 (defgeneric find-edge-between-vertexes-if 
574   (graph value-or-vertex-1 value-or-vertex-2 fn &key error-if-not-found?)
575   (:documentation "Finds and returns an edge between value-or-vertex-1 and value-or-vertex-2 if one exists. Unless error-if-not-found? is nil, then a error will be signaled. [?? Error not signal, need test.]"))
576
577 (defgeneric vertices-share-edge-p (vertex-1 vertex-2)
578   (:documentation "Return true if vertex-1 and vertex-2 are connected by an edge. [?? Compare adjacentp]"))
579
580 (defgeneric graph-edge-mixture-matrix (graph vertex-classifier &key edge-weight)
581   (:documentation "Return the `mixing-matrix` of graph based on the classifier and the edge-weight function. The mixing matrix is a matrix whose runs and columns represent each class of vertex in the graph. The entries of the matrix show the total number of edges between vertexes of the two kinds. [?? Edge-weight is not used; also compare with graph-mixture-matrix.]"))
582
583 (defgeneric graph-mixing-matrix (graph vertex-classifier &key edge-weight)
584   (:documentation "Return the `mixing-matrix` of graph based on the classifier and the edge-weight function. The mixing matrix is a matrix whose runs and columns represent each class of vertex in the graph. The entries of the matrix show the total number of edges between vertexes of the two kinds. [?? Edge-weight is not used; also compare with graph-edge-mixture-matrix.]"))
585
586 (defgeneric connected-components (graph)
587   (:documentation "Returns a union-find-container representing the connected-components of `graph`."))
588
589 (defgeneric connected-component-count (graph)
590   (:documentation "Returns the number of connected-components of graph."))
591
592 (defgeneric find-connected-components (graph)
593   (:documentation "Returns a list of sub-graphs of `graph` where each sub-graph is a different connected component of graph. Compare with connected-components and connected-component-count."))
594
595 (defgeneric initialize-vertex-data (graph)
596   (:documentation ""))
597
598 (defgeneric breadth-first-visitor (graph source fn)
599   (:documentation ""))
600
601 (defgeneric breadth-first-search-graph (graph source)
602   (:documentation ""))
603
604 (defgeneric mst-find-set (vertex)
605   (:documentation ""))
606
607 (defgeneric mst-make-set (vertex)
608   (:documentation ""))
609
610 (defgeneric mst-tree-union (v1 v2)
611   (:documentation ""))
612
613 (defgeneric mst-link (v1 v2)
614   (:documentation ""))
615
616 (defgeneric add-edges-to-graph (graph edges &key if-duplicate-do)
617   (:documentation ""))
618
619 (defgeneric make-graph-from-vertexes (vertex-list)
620   (:documentation "Create a new graph given a list of vertexes \(which are assumed to be from the same graph\). The new graph contains all of the vertexes in the list and all of the edges between any vertexes on the list."))
621
622 (defgeneric edge-lessp-by-weight (edge-1 edge-2)
623   (:documentation "Returns true if the weight of edge-1 is strictly less than the weight of edge-2."))
624
625 (defgeneric minimum-spanning-tree (graph &key edge-sorter)
626   (:documentation "Returns a minimum spanning tree of graph if one exists and nil otherwise."))
627
628 (defgeneric connected-graph-p (graph &key edge-sorter)
629   (:documentation "Returns true if graph is a connected graph and nil otherwise."))
630
631 (defgeneric edge-lessp-by-direction (edge-1 edge-2)
632   (:documentation "Returns true if and only if edge-1 is undirected and edge-2 is directed."))
633
634 (defgeneric out-edge-for-vertex-p (edge vertex)
635   (:documentation "Returns true if the edge is connected to vertex and is either an undirected edge or a directed edge for which vertex is the source vertex of the edge."))
636
637 (defgeneric dfs (graph root fn &key out-edge-sorter)
638   (:documentation ""))
639
640 (defgeneric dfs-visit (graph u fn sorter)
641   (:documentation ""))
642
643 (defgeneric dfs-tree-edge-p (edge)
644   (:documentation ""))
645
646 (defgeneric dfs-back-edge-p (edge)
647   (:documentation ""))
648
649 (defgeneric dfs-forward-edge-p (edge)
650   (:documentation ""))
651
652 (defgeneric dfs-cross-edge-p (edge)
653   (:documentation ""))
654
655 (defgeneric dfs-edge-type (edge)
656   (:documentation ""))
657
658 (defgeneric map-over-all-combinations-of-k-vertexes (graph k fn)
659   (:documentation ""))
660
661 (defgeneric map-over-all-combinations-of-k-edges (vertex k fn)
662   (:documentation ""))
663
664 (defgeneric complete-links (new-graph old-graph)
665   (:documentation "Add edges between vertexes in the new-graph for which the matching  vertexes in the old-graph have edges. The vertex matching is done using `find-vertex`."))
666
667 (defgeneric subgraph-containing (graph vertex &key)
668   (:documentation "Returns a new graph that is a subset of `graph` that contains `vertex` and all of the other vertexes that can be reached from vertex by paths of less than or equal of length `depth`. If depth is not specified, then the entire sub-graph reachable from vertex will be returned. [?? Edge weights are always assumed to be one.]"))
669
670 ;;; ---------------------------------------------------------------------------
671
672 (defgeneric weight (edge)
673   (:documentation "Returns the weight of an edge. This defaults to 1.0 and can only be altered if the edge is a sub-class of `weighted-edge-mixin`."))