Advertisement
Average-user

dfs

May 11th, 2018
2,694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (declare helper)
  2.  
  3. (defn- dfs [graph start end n acc]
  4.   (if (= n end) (conj acc end)
  5.     (let [ns (seq (disj (get graph n) (peek acc)))]
  6.       (if (empty? ns)
  7.         nil
  8.         (helper graph start end ns (conj acc n))))))
  9.  
  10. (defn- helper [graph start end ns acc]
  11.   (if (empty? ns)
  12.     nil
  13.     (let [p (dfs graph start end (first ns) acc)]
  14.       (if p
  15.         p
  16.         (recur graph start end (rest ns) acc)))))
  17.  
  18. (defn findpath [graph start goal]
  19.   (dfs graph start goal start []))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement