Dijkstra in Lisp
The Dijkstra's algorithm was conceived by Edsger Dijkstra, a computer scientist who solved the single-source shortest path for a graph. The basic idea of the algorithm is to find the shortest paths from one node to others. It is usually used in network routing, air traffic, logistics and Artificial Intelligence.
Here below is my implementation of the Dijkstra's algorithm in the Lisp language:
(defun dijkstra (s c n o) (setq INF 9999999) ; Infinite (setq D (make-array s)) (setq m (list o)) ; Initialize D vector (do ((i 0)) ((= i s)) ; if adjacent (if (>= (aref c o i) 0) (setf (aref D i) (aref c o i)) (setf (aref D i) INF) ) (setq i (+ i 1)) ) ; Do Dijkstra for N - 1 items (do ((j 1)) ((= j s)) (setq exit nil) (do ((w 0)) ; select the first of D ((= w s)) ; not in candidate's list (when (and (eq (member w m) nil)(eq exit nil)) (setq minW w) (setq valW (aref D w)) (setq exit t) ) (setq w (+ w 1)) ) (do ((w 0)) ; select the minimum of D ((= w s)) ; not in candidate's list (when (eq (member w m) nil) (when (< (aref D w) valW) (setq minW w) (setq valW (aref D w)) ) ) (setq w (+ w 1)) ) ; Add to candidate's list (setq m (append m (list minW))) ; Iterate to update the items (do ((v 0)) ((= v s)) (if (and (> (aref c minW v) 0)(eq (member v m) nil)) (setf (aref D v) (min (aref D v)(+(aref D minW)(aref c minW v))))) (setq v (+ v 1)) ) (setq j (+ j 1)) ) D ; return D vector )

