fe5020c4c3f1cb9f3fe852bbb17765c45819fbdc
[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-key
268                        edge-labeler 
269                        edge-formatter)
270   (:documentation 
271    "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. 
272
273 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*.
274
275 Here is an example;
276
277     (let ((g (make-container 'graph-container :default-edge-type :directed)))
278       (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
279             (add-edge-between-vertexes g a b))
280       (graph->dot g nil))
281
282     \"digraph G {
283     E []
284     C []
285     B []
286     A []
287     D []
288     F []
289     E->F []
290     B->C []
291     B->D []
292     A->B []
293     D->E []
294     D->F []
295     }\"
296
297 For more information about DOT file format, search the web for 'DOTTY' and 
298 'GRAPHVIZ'."))
299
300 ;;; ---------------------------------------------------------------------------
301
302 (defgeneric graph->dot-properties (g stream)
303   (: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."))
304
305 ;;; ---------------------------------------------------------------------------
306
307 (defgeneric vertex->dot (vertex stream)
308   (: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."))
309
310 ;;; ---------------------------------------------------------------------------
311
312 (defgeneric edge->dot (edge stream)
313   (: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."))
314
315 ;;; ---------------------------------------------------------------------------
316
317 (defgeneric generate-gnm (generator graph n m &key)
318   (:documentation "Generate a 'classic' random graph G(n, m) with n vertexes and m edges."))
319
320 ;;; ---------------------------------------------------------------------------
321
322 (defgeneric generate-gnp (generator graph n p &key)
323   (:documentation  "Generate the Erd\"os-R\'enyi random graph G\(n, p\). I.e., a graph with n vertexes where
324 each possible edge appears with probability p. This implementation is from Efficient Generation
325 of Large Random Networks \(see batagelj-generation-2005 in doab\)."))
326
327 ;;; ---------------------------------------------------------------------------
328
329 (defgeneric generate-undirected-graph-via-assortativity-matrix 
330   (generator graph-class size edge-count kind-matrix assortativity-matrix
331              vertex-labeler &key)
332   (:documentation "This generates a random graph with 'size' vertexes. 
333 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.
334
335 The assortativity-matrix specifies the number of edges between vertexes of different kinds.
336
337 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."))
338
339 ;;; ---------------------------------------------------------------------------
340
341 (defgeneric generate-undirected-graph-via-vertex-probabilities 
342   (generator graph-class size kind-matrix probability-matrix vertex-labeler)
343   (:documentation   "Generate an Erd\"os-R/'enyi like random graph having multiple vertex kinds. See the function
344 Gnp for the simple one vertex kind method.
345
346 Generator and graph-class specify the random number generator used and the class of the graph produced; Size
347 the number of vertexes. Kind matrix is a vector of ratios specifying the distribution of vertex kinds {0 ... \(1- k\)}.
348 The probability-matrix is a k x k matrix specifying the probability that two vertexes of the row-kind and the
349 column-kind will have an edge between them. vertex-labeler is a function of two arguments \(the kind and the vertex number\)
350 called to create values for vertexes. It will be called only once for each vertex created.
351
352 The clever sequential sampling technique in this implementation is from Efficient Generation
353 of Large Random Networks \(see batagelj-generation-2005 in moab\)."))
354
355 ;;; ---------------------------------------------------------------------------
356
357 (defgeneric generate-scale-free-graph 
358   (generator graph size kind-matrix add-edge-count
359              other-vertex-kind-samplers vertex-labeler &key)
360   (:documentation "Generates a 'scale-free' graph using preferential attachment -- too damn slow.
361
362 Size is the number of vertexes; 
363 kind-matrix is as in generate-undirected-graph-via-assortativity-matrix, etc.;
364 add-edge-count is the number of edges to add for each vertex;
365 other-vertex-kind-samplers are confusing...; and
366 vertex-labeler is used to create vertex elements \(as in other generators\)."))
367
368 ;;; ---------------------------------------------------------------------------
369
370 (defgeneric generate-assortative-graph-with-degree-distributions
371   (generator graph
372              edge-count assortativity-matrix
373              average-degrees
374              degree-distributions
375              vertex-labeler
376              &key)
377   (:documentation ""))
378
379 ;;; ---------------------------------------------------------------------------
380
381 (defgeneric generate-simple-preferential-attachment-graph (generator graph size minimum-degree)
382   (:documentation "Generate a simple scale-free graph using the preferential attachment
383 mechanism of Barabasi and Albert. The implementation is from Efficient Generation
384 of Large Random Networks \(see batagelj-generation-2005 in moab\). Self-edges are possible."))
385
386 ;;; ---------------------------------------------------------------------------
387
388 (defgeneric generate-preferential-attachment-graph 
389   (generator graph size kind-matrix minimum-degree 
390              assortativity-matrix 
391              &key)
392   (:documentation "Generate a Barabasi-Albert type scale free graph with multiple vertex kinds.
393
394 The idea behind this implementation is from Efficient Generation
395 of Large Random Networks \(see batagelj-generation-2005 in moab\)."))
396
397
398 ;;; ---------------------------------------------------------------------------
399 ;;; more
400 ;;; ---------------------------------------------------------------------------
401
402 (defgeneric make-vertex-for-graph (graph &key &allow-other-keys)
403   (:documentation "Creates a new vertex for graph `graph`. The keyword arguments include:
404
405 * vertex-class : specify the class of the vertex
406 * element      : specify the `element` of the vertex
407
408 and any other initialization arguments that make sense for the vertex class."))
409
410 ;;; ---------------------------------------------------------------------------
411
412 (defgeneric tag-all-edges (thing)
413   (:documentation "Sets the `tag` of all the edges of `thing` to true. [?? why does this exist?\]"))
414
415 ;;; ---------------------------------------------------------------------------
416
417 (defgeneric untag-all-edges (thing)
418   (:documentation "Sets the `tag` of all the edges of `thing` to nil.  [?? why does this exist?\]"))
419
420 ;;; ---------------------------------------------------------------------------
421
422 (defgeneric untag-edges (edges)
423   (:documentation "Sets the `tag` of all the edges of `thing` to true. [?? why does this exist?\]"))
424
425 ;;; ---------------------------------------------------------------------------
426
427 (defgeneric tag-edges (edges)
428   (:documentation "Sets the `tag` of all the edges of `thing` to true. [?? why does this exist?\]"))
429
430 ;;; ---------------------------------------------------------------------------
431
432 (defgeneric replace-vertex (graph old new)
433   (:documentation "Replace vertex `old` in graph `graph` with vertex `new`. The edge structure of the graph is maintained."))
434
435 ;;; ---------------------------------------------------------------------------
436
437 (defgeneric add-edge-to-vertex (edge vertex)
438   (:documentation "Attaches the edge `edge` to the vertex `vertex`."))
439
440 ;;; ---------------------------------------------------------------------------
441
442 (defgeneric source-edges (vertex &optional filter)
443   (:documentation "Returns a list of the source edges of `vertex`. [?? Could be a defun]."))
444
445 ;;; ---------------------------------------------------------------------------
446
447 (defgeneric target-edges (vertex &optional filter)
448   (:documentation "Returns a list of the target edges of `vertex`. [?? Could be a defun]."))
449
450 ;;; ---------------------------------------------------------------------------
451
452 (defgeneric child-vertexes (vertex &optional filter)
453   (:documentation "Returns a list of the vertexes to which `vertex` is connected by an edge and for which `vertex` is the source vertex. If the connecting edge is undirected, then the vertex is always counted as a source. [?? Could be a defun]."))
454
455 ;;; ---------------------------------------------------------------------------
456
457 (defgeneric parent-vertexes (vertex &optional filter)
458   (: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]."))
459
460 ;;; ---------------------------------------------------------------------------
461
462 (defgeneric neighbor-vertexes (vertex &optional filter)
463   (: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]."))
464
465 ;;; ---------------------------------------------------------------------------
466
467 (defgeneric number-of-neighbors (vertex)
468   (:documentation "Returns the number of neighbors of `vertex` (cf. `neighbor-vertexes`). [?? could be a defun]"))
469
470 ;;; ---------------------------------------------------------------------------
471
472 (defgeneric in-cycle-p (graph start-vertex)
473   (:documentation "Returns true if `start-vertex` is in some cycle in `graph`. This uses child-vertexes to generate the vertexes adjacent to a vertex."))
474
475 ;;; ---------------------------------------------------------------------------
476
477 (defgeneric iterate-vertexes (thing fn)
478   (:documentation "Calls `fn` on each of the vertexes of `thing`."))
479
480 ;;; ---------------------------------------------------------------------------
481
482 (defgeneric edges (thing)
483   (:documentation "Returns a list of the edges of `thing`."))
484
485 ;;; ---------------------------------------------------------------------------
486
487 (defgeneric vertex-count (graph)
488   (:documentation "Returns the number of vertexes in `graph`. [?? could be a defun]"))
489
490 ;;; ---------------------------------------------------------------------------
491
492 (defgeneric vertexes (thing)
493   (:documentation "Returns a list of the vertexes of `thing`."))
494
495 ;;; ---------------------------------------------------------------------------
496
497 (defgeneric source-edge-count (vertex)
498   (:documentation "Returns the number of source edges of vertex (cf. source-edges). [?? could be a defun]"))
499
500 ;;; ---------------------------------------------------------------------------
501
502 (defgeneric target-edge-count (vertex)
503   (:documentation "Returns the number of target edges of vertex (cf. target-edges). [?? could be a defun]"))
504
505 ;;; ---------------------------------------------------------------------------
506
507 (defgeneric graph-roots (graph)
508   (: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]"))
509
510 ;;; ---------------------------------------------------------------------------
511
512 (defgeneric rootp (vertex)
513   (:documentation "Returns true if `vertex` is a root vertex \(i.e., it has no incoming \(source\) edges\)."))
514
515 ;;; ---------------------------------------------------------------------------
516
517 (defgeneric find-vertex-if (thing predicate &key key)
518   (: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."))
519
520 ;;; ---------------------------------------------------------------------------
521
522 (defgeneric find-edge-if (graph fn &key key)
523   (: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."))
524
525 ;;; ---------------------------------------------------------------------------
526
527 (defgeneric find-edges-if (thing predicate)
528   (:documentation "Returns a list of edges in `thing` for which the `predicate` returns non-nil. [?? why no key function?]"))
529
530 ;;; ---------------------------------------------------------------------------
531
532 (defgeneric find-vertexes-if (thing predicate)
533   (:documentation "Returns a list of vertexes in `thing` for which the `predicate` returns non-nil. [?? why no key function?]"))
534
535 ;;; ---------------------------------------------------------------------------
536
537 (defgeneric force-undirected (graph)
538   (:documentation "Ensures that the graph is undirected (possibly by calling change-class on the edges)."))
539
540 ;;; ---------------------------------------------------------------------------
541
542 (defgeneric traverse-elements (thing style fn)
543   (:documentation "WIP"))
544
545 (defgeneric traverse-elements-helper (thing style marker fn)
546   (:documentation "WIP"))
547
548 (defgeneric any-undirected-cycle-p (graph)
549   (:documentation "Returns true if there are any undirected cycles in `graph`."))
550
551 (defgeneric edge-count (vertex)
552   (:documentation "Returns the number of edges attached to `vertex`. Compare with the more flexible `vertex-degree`."))
553
554 (defgeneric topological-sort (graph)
555   (:documentation "Returns a list of vertexes sorted by the depth from the roots of the graph. See also assign-level and graph-roots."))
556
557 (defgeneric assign-level (vertex level)
558   (:documentation "Sets the depth of `vertex` to level and then recursively sets the depth of all of the children of `vertex` to \(1+ level\)."))
559
560 (defgeneric depth (graph)
561   (: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."))
562
563 (defgeneric make-vertex-edges-container (vertex container-class &rest args)
564   (: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."))
565
566 (defgeneric other-vertex (edge value-or-vertex)
567   (: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]"))
568
569 (defgeneric find-edge-between-vertexes-if 
570   (graph value-or-vertex-1 value-or-vertex-2 fn &key error-if-not-found?)
571   (: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.]"))
572
573 (defgeneric vertices-share-edge-p (vertex-1 vertex-2)
574   (:documentation "Return true if vertex-1 and vertex-2 are connected by an edge. [?? Compare adjacentp]"))
575
576 (defgeneric graph-edge-mixture-matrix (graph vertex-classifier &key edge-weight)
577   (: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.]"))
578
579 (defgeneric graph-mixing-matrix (graph vertex-classifier &key edge-weight)
580   (: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.]"))
581
582 (defgeneric connected-components (graph)
583   (:documentation "Returns a union-find-container representing the connected-components of `graph`."))
584
585 (defgeneric connected-component-count (graph)
586   (:documentation "Returns the number of connected-components of graph."))
587
588 (defgeneric find-connected-components (graph)
589   (: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."))
590
591 (defgeneric initialize-vertex-data (graph)
592   (:documentation ""))
593
594 (defgeneric breadth-first-visitor (graph source fn)
595   (:documentation ""))
596
597 (defgeneric breadth-first-search-graph (graph source)
598   (:documentation ""))
599
600 (defgeneric mst-find-set (vertex)
601   (:documentation ""))
602
603 (defgeneric mst-make-set (vertex)
604   (:documentation ""))
605
606 (defgeneric mst-tree-union (v1 v2)
607   (:documentation ""))
608
609 (defgeneric mst-link (v1 v2)
610   (:documentation ""))
611
612 (defgeneric add-edges-to-graph (graph edges &key if-duplicate-do)
613   (:documentation ""))
614
615 (defgeneric make-graph-from-vertexes (vertex-list)
616   (: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."))
617
618 (defgeneric edge-lessp-by-weight (edge-1 edge-2)
619   (:documentation "Returns true if the weight of edge-1 is strictly less than the weight of edge-2."))
620
621 (defgeneric minimum-spanning-tree (graph &key edge-sorter)
622   (:documentation "Returns a minimum spanning tree of graph if one exists and nil otherwise."))
623
624 (defgeneric connected-graph-p (graph &key edge-sorter)
625   (:documentation "Returns true if graph is a connected graph and nil otherwise."))
626
627 (defgeneric edge-lessp-by-direction (edge-1 edge-2)
628   (:documentation "Returns true if and only if edge-1 is undirected and edge-2 is directed."))
629
630 (defgeneric out-edge-for-vertex-p (edge vertex)
631   (: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."))
632
633 (defgeneric dfs (graph root fn &key out-edge-sorter)
634   (:documentation ""))
635
636 (defgeneric dfs-visit (graph u fn sorter)
637   (:documentation ""))
638
639 (defgeneric dfs-tree-edge-p (edge)
640   (:documentation ""))
641
642 (defgeneric dfs-back-edge-p (edge)
643   (:documentation ""))
644
645 (defgeneric dfs-forward-edge-p (edge)
646   (:documentation ""))
647
648 (defgeneric dfs-cross-edge-p (edge)
649   (:documentation ""))
650
651 (defgeneric dfs-edge-type (edge)
652   (:documentation ""))
653
654 (defgeneric map-over-all-combinations-of-k-vertexes (graph k fn)
655   (:documentation ""))
656
657 (defgeneric map-over-all-combinations-of-k-edges (vertex k fn)
658   (:documentation ""))
659
660 (defgeneric complete-links (new-graph old-graph)
661   (: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`."))
662
663 (defgeneric subgraph-containing (graph vertex &key)
664   (: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.]"))
665
666 ;;; ---------------------------------------------------------------------------
667
668 (defgeneric weight (edge)
669   (: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`."))