The assignment I'm struggling with is based around a problem called the knight's tour and this Numberphile video: https://www.youtube.com/watch?v=G1m7goLCJDY
Basically, what I'm trying to do here is to write a helper function that recursively computes a Hamiltonian path in a given graph (V, E). It should return a list of the elements in V in the order of that path, or nil if no such path exists. But it only returns an empty list for the path P.
My attempt so far (further down): (and formatting is a bit weird)
(defn- H'
;;
;; "This is the helper function for computing the Hamiltonian path.
;; E is the relation, i.e. the graph, we are looking for a path in.
;; a is the current node.
;; S is the set of nodes we haven't visited yet.
;; P is the path we have traveled so far.
;;
;; H' should return a Hamiltonian path through E
;; that begins with P, then goes through a, and then visits every vertex
;; in the set S.
;; If no such path exists, it should return nil."
;;
[E a S P]
;;
{
:pre [
(not (contains? S a))
(not (contains? (set P) a))
(empty? (intersection S (set P)))
]
:post [
(or (empty? %) (= (set %) (union S (set P) #{a})))
(or (empty? %) (= (count %) (+ (count S) (count P) 1)))
]
}
;; (image-of E a) returns the set of edges leading away from the current vertex
;; MY ATTEMPT:
(if-not (empty? S)
(if (some #(H' E % (disj S %) P) (intersection (image-of E a) S))
(concat P [a])
)
)
)
(defn H
"compute a Hamiltonian path in the graph (V, E); returns a list of the elements in V in the
order of that path, or nil if no such path exists"
[V E]
(some #(H' E % (disj V %) '()) V)
)
I don't understand why I'm not getting any path P at all in return here from H, just an empty list? Am I terminating the recursion under the wrong conditions or something similar? Is the predicate to the some-function wrongly formulated?
Tell me if anything needs further clarification or more code is needed.