;; GRAPHS ;; ------------------------------------------------------- (define-struct node (name callees)) #| The call graph: --------------- A Graph is a [List-of Node] A Node is (make-node Name [List-of Name]) A Name is a Symbol. interpretation: The Graph (list (make-node 'a (list 'b ...)) (make-node 'b (list 'a ...)) ...) specifies people (by name) and who they call (by name). NAME CONSTRAINT: all names mentioned have a node |# (define a-graph (list (make-node 'amy '(billy claire)) (make-node 'billy '(dan)) (make-node 'claire '()) (make-node 'dan '(eli)) (make-node 'eli '(claire)))) (define b-graph (list (make-node 'amy '(billy claire)) (make-node 'billy '(dan)) (make-node 'claire '(amal)) ; not a good graph! (make-node 'dan '(amy)) (make-node 'eli '(claire)))) ;; good-graph? : [Listof Node] -> Boolean ;; is g a good graph? (define (good-graph? g) (local ((define names (map node-name g))) (andmap (lambda (n) (good-callees? (node-callees n) names)) g))) ;; good-callees? : [Listof Name] [Listof Name] -> Boolean ;; given a list of callees, checks if each callee ;; has a node (i.e., is in names) (define (good-callees? callees names) (andmap (lambda (callee) (member? callee names)) callees)) (check-expect (good-graph? a-graph) true) (check-expect (good-graph? b-graph) false) (check-expect (good-graph? empty) true) ;------------------------------------------------------------ ;; DESIGN path? : ;; path? : Graph Name Name -> Boolean ;; is there a path from node a to b in graph g? (define (path? g a b) ...) #| (check-expect (path? a-graph 'amy 'dan) true) (check-expect (path? a-graph 'amy 'claire) true) (check-expect (path? a-graph 'claire 'dan) false) (check-expect (path? a-graph 'billy 'claire) true) |#