我有一个 BFS 的实现,我在别处得到并稍作修改,但我的输入有问题。
它需要一个图表,并将其视为 '((abc) (bc) (cd)) 但我给它的输入是一个加权图......我知道它对 BFS 没有用,但我使用权重以后再往下走。这个输入看起来像
等等。
'(
(a (b 3) (c 1))
(b (a 3) (d 1))
(c (a 1) (d2) (e 2))
)
我的代码:
(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))))
我只是不确定我最有可能在哪里修改它以接受新的样式列表,或者制作一个帮助功能来正确格式化它?