;; Node = symbol ;; Edges = (listof Node) ;; Graph = (listof Fob) ;; A Fob is a (make-fob Node Edges) (define-struct fob (node edges)) ;; Data examples (define L (list (make-fob 'A '(B)) (make-fob 'B '(A)) (make-fob 'C '()))) (define G (list (make-fob 'A '(B)) (make-fob 'B '(C D)) (make-fob 'C '()) (make-fob 'D '()))) ;; path?: Node Node Graph -> Boolean ;; whether there's a path from o to d in G (define (path? o d G ) (path-acc? o d G empty)) ;; path?: Node Node Graph -> Boolean ;; whether there's a path from o to d in G (define (path-acc? o d G acc) (cond [(cons? (memv o acc)) false] ;; new [(symbol=? o d) true] [(empty? (neighbors o G)) false] [else (local ((define (f next) (path-acc? next d G (cons o acc)))) ;; new (ormap f (neighbors o G)))])) ;; neighbors: Node Graph -> Edges ;; return list of nodes adjacent to n (define (neighbors n G) (cond [(symbol=? (fob-node (first G)) n) (fob-edges (first G))] [else (neighbors n (rest G))])) ;; Tests for neighbors (equal? (neighbors 'A L) '(B)) (equal? (neighbors 'B G) '(C D)) ;; Tests for path? (path? 'A 'D G) (not (path? 'B 'A G)) (not (path? 'A 'C L))