From bbd1d8f317095a435711b8ef001b20c4aab0e61f Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Fri, 27 May 2011 15:57:57 +0000 Subject: [PATCH 1/2] 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. --- dev/api.lisp | 11 +++++++++++ dev/graph.lisp | 8 ++++++++ dev/package.lisp | 2 ++ 3 files changed, 21 insertions(+) diff --git a/dev/api.lisp b/dev/api.lisp index 32231b3..fc92992 100644 --- a/dev/api.lisp +++ b/dev/api.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 diff --git a/dev/graph.lisp b/dev/graph.lisp index 8114715..f2d6671 100644 --- a/dev/graph.lisp +++ b/dev/graph.lisp @@ -677,11 +677,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 )) 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) diff --git a/dev/package.lisp b/dev/package.lisp index 83e707f..5181ee1 100644 --- a/dev/package.lisp +++ b/dev/package.lisp @@ -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 From c6e58815988deb8933dc644de0e3b1bbc703a393 Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Mon, 14 Aug 2023 18:27:58 -0400 Subject: [PATCH 2/2] spelling leafs -> leaves MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I think I originally wanted to be consistent with some weird thing? But obviously it’s better to follow English grammar. --- dev/api.lisp | 4 ++-- dev/graph.lisp | 2 +- dev/package.lisp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dev/api.lisp b/dev/api.lisp index fc92992..6c31235 100644 --- a/dev/api.lisp +++ b/dev/api.lisp @@ -550,8 +550,8 @@ 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 +(defgeneric graph-leaves (graph) + (:documentation "Returns a list of the leaves 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]")) diff --git a/dev/graph.lisp b/dev/graph.lisp index f2d6671..b27daa6 100644 --- a/dev/graph.lisp +++ b/dev/graph.lisp @@ -677,7 +677,7 @@ something is putting something on the vertexes plist's (collect-elements (graph-vertexes graph) :filter #'rootp)) -(defmethod graph-leafs ((graph basic-graph)) +(defmethod graph-leaves ((graph basic-graph)) (collect-elements (graph-vertexes graph) :filter #'leafp)) diff --git a/dev/package.lisp b/dev/package.lisp index 5181ee1..429d3f0 100644 --- a/dev/package.lisp +++ b/dev/package.lisp @@ -48,7 +48,7 @@ DISCUSSION #:rootp ; vertex #:leafp ; vertex #:graph-roots ; graph - #:graph-leafs ; graph + #:graph-leaves ; graph #:topological-sort ; graph #:depth ; graph | vertex