(defconstant +graph1+ '((a (b c)) (b (d)) (c (b d)) (d (c e)) (e (f)) (f (g))) "A directed graph, represented as an association list between nodes (A-F) and, for each node, a set of the states you can get to from that node." ) (defun next-nodes (node graph) "Returns a set of the nodes you can get to in the graph directly from the given node, i.e. in one step. " (second (assoc node graph))) (defun next-nodes-set (nodeset graph) "Nodeset is a set of nodes. This function returns a set of the nodes you can get to in the graph directly (i.e. in one step) from any node in nodeset. " (if nodeset (reduce #'union (mapcar #'(lambda (n) (next-nodes n graph)) nodeset)))) (defun exactly-reachable-nodes (source graph numsteps) "Returns the nodes in graph that you can get to from source in exactly numsteps steps. " (let ((nodeset (list source))) (dotimes (i numsteps) (setf nodeset (next-nodes-set nodeset graph))) nodeset)) (defun exactly-reachablep (source target graph maxsteps) "Returns t if you can get from source to target in graph in exactly maxsteps steps, nil otherwise. " (and (member target (exactly-reachable-nodes source graph maxsteps)) t))