Browse files

Mirror 'root' functionality for leaf nodes

Defines two new functions: graph-leafs and leafp. These are similar to
graph-roots and rootp but act on leaf nodes instead.
  • Loading branch information...
1 parent c3e4307 commit 176f8ffc279101a1e3093a2cf0718a83f89c0743 @hraban committed May 27, 2011
Showing with 21 additions and 0 deletions.
  1. +11 −0 dev/api.lisp
  2. +8 −0 dev/graph.lisp
  3. +2 −0 dev/package.lisp
@@ -550,11 +550,22 @@ as a source. [?? Could be a defun]."))
are out-going\). (cf. rootp) [?? could be a defun]"))
+(defgeneric graph-leafs (graph)
+ (:documentation "Returns a list of the leafs of graph. A leaf is
+ defined as a vertex with no target edges \(i.e., all of the edges
+ are incoming\). (cf. targetp) [?? could be a defun]"))
(defgeneric rootp (vertex)
(:documentation "Returns true if `vertex` is a root vertex \(i.e.,
it has no incoming \(source\) edges\)."))
+(defgeneric leafp (vertex)
+ (:documentation "Returns true if `vertex` is a leaf vertex \(i.e.,
+ it has no outgoing \(target\) edges\)."))
(defgeneric find-vertex-if (thing predicate &key key)
(:documentation "Returns the first vertex in `thing` for which the
`predicate` function returns non-nil. If the `key` is supplied, then
@@ -644,11 +644,19 @@ something is putting something on the vertexes plist's
(collect-elements (graph-vertexes graph) :filter #'rootp))
+(defmethod graph-leafs ((graph basic-graph))
+ (collect-elements (graph-vertexes graph) :filter #'leafp))
(defmethod rootp ((vertex basic-vertex))
;;?? this is inefficient in the same way that (zerop (length <list>)) is...
(zerop (target-edge-count vertex)))
+(defmethod leafp ((vertex basic-vertex))
+ (zerop (source-edge-count vertex)))
(defmethod find-vertex-if ((graph basic-graph) fn &key key)
(iterate-vertexes graph
(lambda (v)
@@ -46,7 +46,9 @@ DISCUSSION
#:target-edge-count ; vertex
#:rootp ; vertex
+ #:leafp ; vertex
#:graph-roots ; graph
+ #:graph-leafs ; graph
#:topological-sort ; graph
#:depth ; graph | vertex

0 comments on commit 176f8ff

Please sign in to comment.