Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lispm
Last active August 29, 2015 14:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lispm/6066e1eeadf943910c47 to your computer and use it in GitHub Desktop.
Save lispm/6066e1eeadf943910c47 to your computer and use it in GitHub Desktop.
longest graph, version without a NODE structure
; https://github.com/logicchains/LPATHBench/blob/master/writeup.md
(eval-when (:load-toplevel :compile-toplevel :execute)
(defstruct route
(dest 0 :type fixnum)
(cost 0 :type fixnum)))
(defun parse-line (line &aux (pos 0) n)
(declare (ignorable n))
(loop repeat 3
collect (multiple-value-setq (n pos)
(parse-integer line :start pos :junk-allowed t))))
(defparameter *file* "agraph")
(defun read-places ()
(with-open-file (stream *file*)
(let ((num-lines (parse-integer (read-line stream nil))))
(values (loop for line = (read-line stream nil nil)
while line
collect (parse-line line))
num-lines))))
(defun parse-places ()
(multiple-value-bind (place-data num-nodes)
(read-places)
(let ((nodes (make-array num-nodes :initial-element nil)))
(loop for (node-id neighbour dist) in place-data
do (push (make-route :dest neighbour :cost dist)
(aref nodes node-id)))
nodes)))
(declaim (ftype (function (simple-vector fixnum simple-vector) fixnum)
get-longest-path))
(defun get-longest-path (nodes node-id visited &aux (max 0))
(declare (optimize (speed 3) (space 0) (debug 0) (safety 0) (compilation-speed 0)
#+lispworks (fixnum-safety 0))
(fixnum max))
(setf (svref visited node-id) t)
(setf max (loop for neighbour of-type route in (svref nodes node-id)
unless (svref visited (route-dest neighbour))
maximize (+ (the fixnum (route-cost neighbour))
(the fixnum (get-longest-path nodes
(route-dest neighbour)
visited)))
#+lispworks fixnum))
(setf (svref visited node-id) nil)
max)
(defun run ()
(let* ((nodes (parse-places))
(visited (make-array (length nodes) :element-type 'boolean :initial-element nil))
(start (get-internal-real-time))
(len (get-longest-path nodes 0 visited))
(end (get-internal-real-time))
(duration (truncate (* 1000 (- end start))
internal-time-units-per-second)))
(format t "~d LANGUAGE Lisp ~d ~%" len duration)))
(sb-ext:save-lisp-and-die "lisp" :toplevel #'run :executable t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment