0

How would we go about implementing get the shortest path between the start vertex and the end vertex? The program should return a list of edges (shortest path) using BFS.

(define (new-paths path node net)
   (map (lambda (n) (cons n path)) (cdr (assoc node net))))
(define (shortest-path start end net)
    (bfs end (list (list start)) net))

 ;; Breadth-first search
  (define (bfs end queue net)
    (display queue) (newline) (newline) ; entertainment
    (if (null? queue)
      '()
      (let ((path (car queue)))
       (let ((node (car path)))
         (if (equal? node end) ;; Graham used CL eql
             (reverse path)
             (bfs end 
                  (append (cdr queue)
                          (new-paths path node net))
                  net))))))

I came up with but this does not seem to work. Can someone provide an implementation in a purely functional way?

In the form (get-shortest-path vertices edges src dest) An example of the call would be

(get-shortest-path '(a b c d) (cons (cons a b) (cons b c) (cons c d)) 'a 'c) 
; Get the shortest path between a and c
  • Can you share the definition of `new-paths` as well, please? Also [edit] your question to include a [mcve] to demonstrate how it does not work. – Bergi Dec 16 '20 at 22:47
  • I have added a definition of the sample call. – coderforlife Dec 16 '20 at 23:01
  • I still might be missing something (never used Racket before) but the `cdr` in `new-paths` looks wrong (why skip the first neighbor?) and I'm missing the set/list/flag map of visited nodes. – Bergi Dec 17 '20 at 11:35

0 Answers0