Adjacency Matrix / Floyd / Warshall in lisp

Apparently, my teacher believes that even if we don’t have time to study the material (and there aren’t enough examples), we should move on, so now I need to know how to make the Floyd-Warshall and Warshall algorithms in clisp.

As in the case of the prologue, my task is to generate an adjacency matrix from the graph, in this case it will be a list of lists, for example:

((A B) (A C) (A D) (B C) (C D))

This should generate:

((0 1 1 1) (1 0 1 9) (1 1 0 1) (1 9 1 0))

I have it:

(defun floyd(graph)
    (setf l (length graph)) 
    (setf mat (matrix l graph))
)

(defun matrix(l graph)
    (setf matrix (make-array (list l l)))
    (dotimes (i l)
        (dotimes (j l)
            (if (= i j)
                (setf (aref matrix i j) 0)
                (setf (aref matrix i j) ???)
            )
        )
    )
    matrix
)

Any help is greatly appreciated.

In addition, and it seems to be off topic: if I could solve my own question, should I answer myself for the answer to the question?

+3
source share
1 answer

Wikipedia Common Lisp . , SBCL. , , , Lisp.

(defparameter *n* 5)
(defparameter *path*
  (make-array (list *n* *n*)
          :element-type '(unsigned-byte 64)))


(defun floyd-warshall (path)
  (declare (type (simple-array (unsigned-byte 64) 2) path)
       (values (simple-array (unsigned-byte 64) 2) &optional))
  (destructuring-bind (y x) (array-dimensions path)
    (unless (= y x)
      (break "I expect a square matrix, not ~ax~a." x y))
    (macrolet ((p (j i)
         `(aref path ,j ,i)))
      (dotimes (k x)
    (dotimes (i x)
      (dotimes (j x)
        (setf (p j i)
          (min (p j i) (+ (p k i)
                  (p j k)))))))))
  path)

1: , , (aref vol k j i) k z, j y - x. SBCL , , .

2: . : git://github.com/nikodemus/raylisp.git/objects/box.lisp

+1

All Articles