Skip to content

Instantly share code, notes, and snippets.

@zeptometer
Created December 5, 2016 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeptometer/142340eca75d86074ec0f923c7f594af to your computer and use it in GitHub Desktop.
Save zeptometer/142340eca75d86074ec0f923c7f594af to your computer and use it in GitHub Desktop.
(defun warshall-floyd (n edges)
(let ((table (make-array (list n n) :initial-element 0)))
(dolist (x edges)
(destructuring-bind (from to weight) x
(setf (aref table from to) weight)))
(dotimes (k n)
(dotimes (i n)
(dotimes (j n)
(when (> (aref table i j)
(+ (aref table i k) (aref table k j)))
(setf (aref table i j)
(+ (aref table i k) (aref table k j)))))))))
(defun warshall-floyd-optimized (n edges)
(declare (optimize (speed 3) (debug 0) (safety 0))
(fixnum n))
(let ((table (make-array (list n n) :element-type 'fixnum
:initial-element 0)))
(dolist (x edges)
(destructuring-bind (from to weight) x
(setf (aref table from to) weight)))
(dotimes (k n)
(dotimes (i n)
(dotimes (j n)
(let ((cand (the fixnum (+ (the fixnum (aref table i k))
(the fixnum (aref table k j))))))
(when (> (the fixnum (aref table i j)) cand)
(setf (aref table i j)
cand))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment