;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname graph) (read-case-sensitive #t) (teachpacks ((lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;; a graph is represented as a list of lists, ;; each list contains a node and a list of its neighbors ;; (adjacency list) (define Graph '((A (B E)) (B (E F)) (C (D)) (D ()) (E (C F)) (F (D G)) (G ()))) Graph ;; neighbors: node graph ;; To find all neightbors of a node in a graph (define (neighbors node G) ;;; your code goes here ) (check-expect (neighbors 'E Graph) (list 'C 'F)) (check-expect (neighbors 'G Graph) empty) (check-error (neighbors 'X Graph) "neighbors: No such node") ;; find-route : node node graph -> (listof node) or false ;; to create a path from origin to destination in G ;; if there is no path, the function produces false (define (find-route origin destination G) (cond [(symbol=? origin destination) (list destination)] [else (local ((define possible-route (find-route/list (neighbors origin G) destination G))) (cond [(boolean? possible-route) false] [else (cons origin possible-route)]))])) ;; find-route/list : (listof node) node graph -> (listof node) or false ;; to create a path from some node on lo-Os to D ;; if there is no path, the function produces false (define (find-route/list lo-Os D G) (cond [(empty? lo-Os) false] [else (local ((define possible-route (find-route (first lo-Os) D G))) (cond [(boolean? possible-route) (find-route/list (rest lo-Os) D G)] [else possible-route]))])) ;; write results in check-expect for these cases: (find-route 'B 'G Graph) (find-route 'C 'A Graph)