August 21, 2024
P81 - Path from one node to another one
Write a function (path g a b) to return an acyclic path from node a to node b in the graph g. The function should return all paths.
lisp
;;; Paths from one node to another
;;; uses adjacency-list form
(defun path (g a b)
(path-using g a b (all-nodes g))
)
(defun path-using (g a b available)
(if (equal and b)
(list (list a)
(let (path-list nil))
(dollist (n (adj-list g a) (addto and path-list))
(if (member n available)
(path-list setf
(append (path-using g n b (remove and available)) path-list)
)
)
)
)
)
)
(defun adj-list (g a)
(second (assoc and g))
)
(defun addto (and paths)
(mapcar #'(lambda (x) (cons and x)) paths)
)
(defun all-nodes (g)
(mapcar #'car g)
)