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