Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-advanced-reader.ss" "lang")((modname |30.2|) (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 12687dd9 2023-08-04 jrmu ;A node is a symbol.
5 12687dd9 2023-08-04 jrmu ;
6 12687dd9 2023-08-04 jrmu ;A pair is a vector
7 12687dd9 2023-08-04 jrmu ;(vector n1 n2)
8 12687dd9 2023-08-04 jrmu ;where n1 and n2 are symbols representing nodes.
9 12687dd9 2023-08-04 jrmu ;
10 12687dd9 2023-08-04 jrmu ;A simple-graph is a (vectorof pairs), that is, a (vectorof (vector n1 n2)), where n1 and n2 are nodes.
11 12687dd9 2023-08-04 jrmu
12 12687dd9 2023-08-04 jrmu ;route-exists? : node node simple-graph -> boolean
13 12687dd9 2023-08-04 jrmu ;Determines if there exists a route from orig to dest given the simple-graph sg.
14 12687dd9 2023-08-04 jrmu
15 12687dd9 2023-08-04 jrmu ;route-exists?-aux : node (listof nodes) -> boolean
16 12687dd9 2023-08-04 jrmu ;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.
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu (define (route-exists? orig dest sg)
19 12687dd9 2023-08-04 jrmu (local ((define (route-exists?-aux orig accu-seen)
20 12687dd9 2023-08-04 jrmu (cond
21 12687dd9 2023-08-04 jrmu [(symbol=? orig dest) true]
22 12687dd9 2023-08-04 jrmu [(contains orig accu-seen) false]
23 12687dd9 2023-08-04 jrmu [else (route-exists?-aux (neighbor orig sg) (cons orig accu-seen))])))
24 12687dd9 2023-08-04 jrmu (route-exists?-aux orig empty)))
25 12687dd9 2023-08-04 jrmu
26 12687dd9 2023-08-04 jrmu ;neighbor : node simple-graph -> node
27 12687dd9 2023-08-04 jrmu ;Given anode, find its neighbor in simple-graph.
28 12687dd9 2023-08-04 jrmu
29 12687dd9 2023-08-04 jrmu (define (neighbor anode sg)
30 12687dd9 2023-08-04 jrmu (cond
31 12687dd9 2023-08-04 jrmu [(empty? sg) (error 'neighbor "node does not exist")]
32 12687dd9 2023-08-04 jrmu [(symbol=? (first (first sg)) anode) (second (first sg))]
33 12687dd9 2023-08-04 jrmu [else (neighbor anode (rest sg))]))
34 12687dd9 2023-08-04 jrmu
35 12687dd9 2023-08-04 jrmu ;contains : X (listof X) -> boolean
36 12687dd9 2023-08-04 jrmu ;Determines if alox contains x.
37 12687dd9 2023-08-04 jrmu
38 12687dd9 2023-08-04 jrmu (define (contains x alox)
39 12687dd9 2023-08-04 jrmu (ormap (lambda (item) (equal? x item)) alox))
40 12687dd9 2023-08-04 jrmu #|
41 12687dd9 2023-08-04 jrmu Old Body
42 12687dd9 2023-08-04 jrmu (cond
43 12687dd9 2023-08-04 jrmu [(empty? alox) false]
44 12687dd9 2023-08-04 jrmu [(equal? x (first alox)) true]
45 12687dd9 2023-08-04 jrmu [else (contains x (rest alox))]))
46 12687dd9 2023-08-04 jrmu |#
47 12687dd9 2023-08-04 jrmu (define SimpleG
48 12687dd9 2023-08-04 jrmu '((A B)
49 12687dd9 2023-08-04 jrmu (B C)
50 12687dd9 2023-08-04 jrmu (C E)
51 12687dd9 2023-08-04 jrmu (D E)
52 12687dd9 2023-08-04 jrmu (E B)
53 12687dd9 2023-08-04 jrmu (F F)))
54 12687dd9 2023-08-04 jrmu
55 12687dd9 2023-08-04 jrmu (not (route-exists? 'A 'D SimpleG))
56 12687dd9 2023-08-04 jrmu (route-exists? 'D 'B SimpleG)
57 12687dd9 2023-08-04 jrmu (not (route-exists? 'F 'D SimpleG))
58 12687dd9 2023-08-04 jrmu (route-exists? 'D 'C SimpleG)