0
我有一個BFS的實現我得到了其他地方,並稍加修改,但我有其輸入問題。
它需要一個圖表,並將其視爲'((abc)(bc)(cd)) 但我給它的輸入是一個加權圖...我知道這對BFS沒有用,但是我稍後使用更靠後的權重。此輸入看起來像
'(
(a (b 3) (c 1))
(b (a 3) (d 1))
(c (a 1) (d2) (e 2))
)
依此類推。LISP - 廣度優先搜索
我的代碼:
(defun shortest-path (start end net)
(BFS end (list (list start)) net))
(defun BFS (end queue net)
(if (null queue)
nil
(expand-queue end (car queue) (cdr queue) net)))
(defun expand-queue (end path queue net)
(let ((node (car path)))
(if (eql node end)
(reverse path)
(BFS end
(append queue
(new-paths path node net))
net))))
(defun new-paths (path node net)
(mapcar #'(lambda (n)
(cons n path))
(cdr (assoc node net))))
我只是不知道在哪裏,我需要最有可能修改它以接受新的樣式列表,或者做一個幫助功能,以正確的格式呢?
這工作完美。這有點超出了我的Lisp技能,所以非常感謝您的幫助。 – snivek 2011-04-11 19:08:46