Paste number 52132: graph cycles

Paste number 52132: graph cycles
Pasted by: faxathisia
8 months, 2 weeks ago
#lispcafe | Context in IRC logs
Paste contents:
Raw Source | XML | Display As
(defun contains-duplicates-p (list)
  (/= (length list)
      (length (remove-duplicates list))
)
)


(defparameter *graph* '((a d e)
                        (b b c e f)
                        (c)
                        (d c f)
                        (e a)
                        (f b)
)
)


(defun next-paths (path)
  (mapcar #'(lambda (node) (append path (list node)))
          (cdr (assoc (car (last path)) *graph*))
)
)


(defun next-paths-for-paths (paths)
  (mapcan #'next-paths paths)
)


(defun cyclep (path)
  (some #'(lambda (path) (equal (first path) (car (last path))))
        (next-paths path)
)
)


(defun all-cycles ()
  (loop for cycles := nil then (append cycles (remove-if-not #'cyclep paths))
        for paths := (mapcar #'list (mapcar #'first *graph*))
        then (remove-if #'contains-duplicates-p (next-paths-for-paths paths))
        while paths
        finally (return cycles)
)
)



This paste has no annotations.

Colorize as:
Show Line Numbers

Ads absolutely not by Google

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.