Anonymous avatar Anonymous committed e568b05

BREADTH-FIRST-SEARCH method for GRAPH class implemented.

TODO: Make DEPTH-FIRST-SEARCH return sequence.

Comments (0)

Files changed (2)

        ))
      (:module
       "graph"
-      :depends-on ("edge")
+      :depends-on ("utility" "edge")
       :components
       ((:file "graph")
        (:file "standard-graph"

source/graph/graph.lisp

 					predicate)
   (:documentation "Returns a path (if there is one) from SOURCE to
   TARGET in GRAPH and NIL of there is none. The path is found via
-  breadth first search."))
+  breadth first search.")
+  (:method ((graph graph)
+	    source target &optional (predicate (constantly t)))
+    (when (and (contains-node? graph source)
+	       (contains-node? graph target))
+      (let ((paths (sort (image (lambda (n)
+				  (seq n))
+				(neighbours graph source))
+			 predicate))
+	    (visited (set source)))
+	(if (equal? source target)
+	    (seq source)
+	    (loop do
+		 (if (empty? paths)
+		     (return-from breadth-first-search
+		       nil)
+		     (let* ((path (lookup paths 0))
+			    (end (last path)))
+		       (if (equal? end target)
+			   (return-from breadth-first-search
+			     (concat (seq source)
+				     path))
+			   (setf
+			    paths
+			    (concat (subseq paths 1)
+				    (image
+				     (lambda (n)
+				       (concat path (seq n)))
+				     (sort (set-difference (neighbours
+							    graph
+							    end)
+							   visited)
+					   predicate)))
+			    visited (with visited end)))))))))))
 
 (defgeneric normal-spanning-tree (graph root &optional predicate)
   (:documentation "Returns a normal spanning tree of GRAPH with root
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.