Finding path between 2 points in Racket

∥☆過路亽.° 提交于 2019-11-29 16:54:22

Here is a very simple solution:

(define (mainpath routelist start end)
  (define (neighbors node)
    (map second (filter (lambda (x) (eq? (first x) node)) routelist)))
  (define (visit node visited)
    (when (not (member node visited))
      (when (eq? node end)
        (println (reverse (cons node visited))))
      (let ((new-visited (cons node visited)))
        (map (lambda (x) (visit x new-visited)) (neighbors node)))))
  (visit start '())
  "No more paths")

This recursive function, that can manage also graphs with loops, keeps a list of nodes already visited along the current path and stops when it has visited all the nodes reachable from the start node. When the current node is the end node, the current path is printed.

Use DFS algorithm will be ok.

(define (mainpath routelist start end)
  (letrec ([next-nodes (λ (node)
                        (for/list ([al routelist]
                                   #:when (eq? node (first al)))
                                  (second al)))]
           [path (λ (node vlist)
                   (let ([new-list (cons node vlist)])
                     (when (eq? node end)
                       (println (reverse new-list)))
                     (for ([next (next-nodes node)]
                           #:unless (memq next vlist))
                          (path next new-list))))])
    (path start '())))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!