516585a909d0 v1.0.0

Add `emptyp`, export `roots` and `leafs`
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 20 Nov 2016 15:38:47 +0000
parents f5d201dead81
children 4dd91c341487
branches/tags v1.0.0
files docs/03-reference.markdown package.lisp src/directed-graph.lisp test/tests.lisp

Changes

--- a/docs/03-reference.markdown	Tue Nov 08 14:11:17 2016 +0000
+++ b/docs/03-reference.markdown	Sun Nov 20 15:38:47 2016 +0000
@@ -78,6 +78,12 @@
 
   
 
+### `EMPTYP` (function)
+
+    (EMPTYP DIGRAPH)
+
+Return `t` if `digraph` has no vertices or edges, `nil` otherwise.
+
 ### `INSERT-EDGE` (function)
 
     (INSERT-EDGE DIGRAPH PREDECESSOR SUCCESSOR)
@@ -102,6 +108,18 @@
 
   
 
+### `LEAFS` (function)
+
+    (LEAFS DIGRAPH)
+
+Return all leaf vertices in `digraph`.
+
+  This is currently O(vertices).
+
+  A root is a vertex with no outgoing edges (i.e. out-degree 0).
+
+  
+
 ### `MAKE-DIGRAPH` (function)
 
     (MAKE-DIGRAPH &KEY INITIAL-VERTICES (TEST #'EQL) (HASH-FUNCTION NIL))
@@ -278,6 +296,18 @@
 
   
 
+### `ROOTS` (function)
+
+    (ROOTS DIGRAPH)
+
+Return all root vertices in `digraph`.
+
+  This is currently O(vertices).
+
+  A root is a vertex with no incoming edges (i.e. in-degree 0).
+
+  
+
 ### `SUCCESSORS` (function)
 
     (SUCCESSORS DIGRAPH VERTEX)
--- a/package.lisp	Tue Nov 08 14:11:17 2016 +0000
+++ b/package.lisp	Sun Nov 20 15:38:47 2016 +0000
@@ -4,9 +4,14 @@
     :digraph
     :make-digraph
 
+    :emptyp
+
     :vertices
     :edges
 
+    :roots
+    :leafs
+
     :predecessors
     :successors
     :neighbors
--- a/src/directed-graph.lisp	Tue Nov 08 14:11:17 2016 +0000
+++ b/src/directed-graph.lisp	Sun Nov 20 15:38:47 2016 +0000
@@ -1,7 +1,5 @@
 (in-package :digraph)
 
-
-
 ;;;; Utils --------------------------------------------------------------------
 (defun make-hash-table-portably (&key (size 0) test hash-function)
   ;; Only try to pass :hash-function if we were given it, so we don't explode in
@@ -72,6 +70,11 @@
 
 
 ;;;; Basic API ----------------------------------------------------------------
+(defun emptyp (digraph)
+  "Return `t` if `digraph` has no vertices or edges, `nil` otherwise."
+  (zerop (hash-table-count (digraph-nodes digraph))))
+
+
 (defun vertices (digraph)
   "Return a fresh list of the vertices of `digraph`."
   (hash-table-keys (digraph-nodes digraph)))
@@ -372,10 +375,24 @@
 
 
 (defun roots (digraph)
+  "Return all root vertices in `digraph`.
+
+  This is currently O(vertices).
+
+  A root is a vertex with no incoming edges (i.e. in-degree 0).
+
+  "
   (remove-if-not (lambda (v) (null (pred digraph v)))
                  (vertices digraph)))
 
 (defun leafs (digraph)
+  "Return all leaf vertices in `digraph`.
+
+  This is currently O(vertices).
+
+  A root is a vertex with no outgoing edges (i.e. out-degree 0).
+
+  "
   (remove-if-not (lambda (v) (null (succ digraph v)))
                  (vertices digraph)))
 
--- a/test/tests.lisp	Tue Nov 08 14:11:17 2016 +0000
+++ b/test/tests.lisp	Sun Nov 20 15:38:47 2016 +0000
@@ -20,15 +20,34 @@
   (let ((g (make-digraph)))
     (is (zerop (count-vertices g)))
     (is (same () (vertices g)))
-    (is (same () (edges g))))
+    (is (same () (edges g)))
+    (is (emptyp g)))
   (let ((g (make-digraph :initial-vertices '(a b c))))
     (is (= 3 (count-vertices g)))
     (is (same '(a b c) (vertices g)))
-    (is (same () (edges g))))
+    (is (same () (edges g)))
+    (is (not (emptyp g))))
   (let ((g (make-digraph :initial-vertices '(a b a c a a))))
     (is (= 3 (count-vertices g)))
     (is (same '(a b c) (vertices g)))
-    (is (same () (edges g)))))
+    (is (same () (edges g)))
+    (is (not (emptyp g)))))
+
+
+(define-test roots-and-leafs
+  (let ((g (make-digraph)))
+    (is (same () (roots g)))
+    (is (same () (leafs g)))
+    (insert-vertex g 'a)
+    (insert-vertex g 'b)
+    (is (same '(a b) (roots g)))
+    (is (same '(a b) (leafs g)))
+    (insert-edge g 'a 'b)
+    (is (same '(a) (roots g)))
+    (is (same '(b) (leafs g)))
+    (insert-edge g 'b 'a)
+    (is (same () (roots g)))
+    (is (same () (leafs g)))))
 
 
 (define-test insert-vertex