Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4573828
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:50:22+00:00 2026-05-21T19:50:22+00:00

Ok I have a Lisp implementation of BFS that I am trying to convert

  • 0

Ok I have a Lisp implementation of BFS that I am trying to convert to do a hill climbing search.

Here is what my BFS code looks like:

; The list of lists is the queue that we pass BFS.  the first entry and 
; every other entry in the queue is a list.  BFS uses each of these lists and 
; the path to search.  

(defun shortest-path (start end net)   
  (BFS end (list (list start)) net))

;We pass BFS the end node, a queue containing the starting node and the graph 
;being searched(net)   

(defun BFS (end queue net)
  (if (null queue) ;if the queue is empty BFS has not found a path so exit
      nil
      (expand-queue end (car queue) (cdr queue) net)))

(defun expand-queue (end path queue net)
  (let ((node (car path)))
    (if (eql node end)   ; If the current node is the goal node then flip the path
                         ; and return that as the answer
        (reverse path)
        ; otherwise preform a new BFS search by appending the rest of 
        ; the current queue to the result of the new-paths function
        (BFS end (append queue (new-paths path node net)) net))))

; mapcar is called once for each member of the list new-paths is passed
; the results of this are collected into a list
(defun new-paths (path node net)
  (mapcar #'(lambda (n) (cons n path))
         (cdr (assoc node net))))

Now, I know that instead of always expanding the left node as I do in the BFS I need to expand the node that seems closest to the goal state.
The graph I am using looks like this:

(a (b 3) (c 1))  
(b (a 3) (d 1))

I have a conversion function to make this work with the above BFS implementation, but now I need to turn this into Hill climbing using this graph format.

I’m just not sure where to begin, and have been trying things to no avail. I know I mainly need to change the expand-queue function to expand the closest node, but I can’t seem to make a function to determine which node is closest.

Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:50:22+00:00Added an answer on May 21, 2026 at 7:50 pm

    Appending things to the end of a list is wrong. This is the most costly operation with a list.
    The whole list gets copied and then the other list is appended. You use it in a recursive procedure, which means that it will done each time you expand a node to create new paths.

    If you put items on a queue, you need to look in which order you are doing this. With breadth first, one visits each node in a level, before moving to the next level. Hill-climbing would require that you sort the candidates for the ‘best’ by using a weighting function. So you need some kind of function computing a numeric value from a current node and the next node candidate. Then you need to sort the candidates and expand first the ‘best’ node. For a LIFO (last in, first out) queue this means that the most promising node needs to be pushed last, so that it will be the first to be expanded. Note that LIFO queues are a good fit for singly linked lists. FIFO (first in, first out) not so.

    A typical idea of computer science is data abstraction. If a LIFO QUEUE is such a data structure, you need to define functions like MAKE-LIFO-QUEUE, EMPTY-QUEUE-P, … Theses functions you would then use instead of LIST, NULL and others and they make the purpose of the data structure clearer. This makes your code longer, but since Lisp lists are generic and can be (ab-)used for all kinds of usage scenarios, looking at list operations alone does not make their intention clear. Especially important is this for more complicated graph algorithms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that most languages are becoming more and more like lisp, adopting
Still working on lisp recipes and idioms. I have a list like this: ((a
I have the following LISP code (defun l (x y) (list x y)) when
I have some lisp initialisation code: (eval-when (:compile-toplevel :load-toplevel :execute) (require 'asdf)) (eval-when (:compile-toplevel
Say, if I have a Lisp program, which uses (eval 'sym) and looks it
I have this association-list in Common Lisp: (defvar base-list (list (cons 'a 0) (cons
Does clojure have a powerful 'loop' like common lisp. for example: get two elements
I note that Scheme and Lisp (I guess) support circular lists, and I have
I have a fairly involved LispWorks Common Lisp module that sits atop some .NET
In Common Lisp I can conditionally exclude or include code for different implementations like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.