(defun branch-and-bound (graph start goal) (branch-and-bound-aux graph start goal (list (list start)))) (defun branch-and-bound-aux (graph start goal path-queue) (let ((path (first path-queue))) (cond ((null path-queue) nil) ((node-equal goal (first path)) (reverse path)) (t (branch-and-bound-aux graph start goal (sort (append (new-paths path graph) (rest path-queue)) #'shorterp))))))
> (branch-and-bound *graph1* 's 'f) (s d e f)sortを使っていますが,実はすべてを並べ替える必要はない.もっとも 評価の高いもののみを一つ選ぶという処理でも十分である. 各パスのコストがもっとも短くなる解を必ず見つけるのがこの分岐限定法である. ちなみに,ならべ替えを行う手続きsortの実行例を示しておく.
> (sort '(1 3 2 5 4) #'<) (1 2 3 4 5) > (sort '(1 3 2 5 4) #'>) (5 4 3 2 1)