Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-advanced-reader.ss" "lang")((modname |37.3|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 ;A node is a symbol.
5 ;
6 ;A pair is a list
7 ;(cons n1 (cons n2)), ie, (list n1 n2)
8 ;where n1 and n2 are symbols representing nodes.
9 ;
10 ;A simple-graph is a (listof pairs).
12 ;;State Variables
14 ;;seen-before : (listof nodes)
15 ;;Keeps track of all nodes that have been accessed in the past.
17 (define seen-before empty)
19 ;route-exists? : node node simple-graph -> boolean
20 ;Determines if there exists a route from orig to dest given the simple-graph sg.
22 ;route-exists?-aux : node (listof nodes) -> boolean
23 ;Determine if there exists a route from orig to dest given the simple-graph sg. accu-seen represents nodes that have been visited before. If a node is visited twice and a route is not determined, return false.
25 (define (route-exists? orig dest sg)
26 (local ((define (route-exists?-aux orig dest sg)
27 (cond
28 [(symbol=? orig dest) true]
29 [(contains orig seen-before) false]
30 [else (begin (set! seen-before (cons orig seen-before))
31 (route-exists?-aux (neighbor orig sg) dest sg))])))
32 (begin (set! seen-before empty)
33 (route-exists?-aux orig dest sg))))
35 ;neighbor : node simple-graph -> node
36 ;Given anode, find its neighbor in simple-graph.
38 (define (neighbor anode sg)
39 (cond
40 [(empty? sg) (error 'neighbor "node does not exist")]
41 [(symbol=? (first (first sg)) anode) (second (first sg))]
42 [else (neighbor anode (rest sg))]))
44 ;contains : X (listof X) -> boolean
45 ;Determines if alox contains x.
47 (define (contains x alox)
48 (ormap (lambda (item) (equal? x item)) alox))
49 #|
50 Old Body
51 (cond
52 [(empty? alox) false]
53 [(equal? x (first alox)) true]
54 [else (contains x (rest alox))]))
55 |#
56 (define SimpleG
57 '((A B)
58 (B C)
59 (C E)
60 (D E)
61 (E B)
62 (F F)))
64 (not (route-exists? 'A 'D SimpleG))
65 (route-exists? 'D 'B SimpleG)
66 (not (route-exists? 'F 'D SimpleG))
67 (route-exists? 'D 'C SimpleG)