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 |29.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 12687dd9 2023-08-04 jrmu ;A graph is a (vectorof (listof nodes)).
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu ;neighbors : node graph -> (listof nodes)
7 12687dd9 2023-08-04 jrmu ;Return the neighbors of anode given agraph.
8 12687dd9 2023-08-04 jrmu
9 12687dd9 2023-08-04 jrmu (define (neighbors anode agraph)
10 12687dd9 2023-08-04 jrmu (vector-ref agraph anode))
11 12687dd9 2023-08-04 jrmu
12 12687dd9 2023-08-04 jrmu
13 12687dd9 2023-08-04 jrmu (define G (vector '(1 4)
14 12687dd9 2023-08-04 jrmu '(4 5)
15 12687dd9 2023-08-04 jrmu '(3)
16 12687dd9 2023-08-04 jrmu empty
17 12687dd9 2023-08-04 jrmu '(2 5)
18 12687dd9 2023-08-04 jrmu '(3 6)
19 12687dd9 2023-08-04 jrmu empty))
20 12687dd9 2023-08-04 jrmu
21 12687dd9 2023-08-04 jrmu ;find-route : node node graph -> (listof nodes)
22 12687dd9 2023-08-04 jrmu ;Find a route from ori to dest given G and return the route as a (listof nodes).
23 12687dd9 2023-08-04 jrmu
24 12687dd9 2023-08-04 jrmu (define (find-route ori dest G)
25 12687dd9 2023-08-04 jrmu (cond
26 12687dd9 2023-08-04 jrmu [(= ori dest) (list ori)]
27 12687dd9 2023-08-04 jrmu [else (local ((define possible-route (find-route/list (neighbors ori G) dest G)))
28 12687dd9 2023-08-04 jrmu (cond [(boolean? possible-route) false]
29 12687dd9 2023-08-04 jrmu [else (cons ori possible-route)]))]))
30 12687dd9 2023-08-04 jrmu
31 12687dd9 2023-08-04 jrmu ;find-route/list : (listof nodes) node graph -> (listof nodes)
32 12687dd9 2023-08-04 jrmu ;Find a route from lo-ori (listof nodes) to dest given G and return the route as a (listof nodes).
33 12687dd9 2023-08-04 jrmu
34 12687dd9 2023-08-04 jrmu (define (find-route/list lo-ori dest G)
35 12687dd9 2023-08-04 jrmu (cond
36 12687dd9 2023-08-04 jrmu [(empty? lo-ori) false]
37 12687dd9 2023-08-04 jrmu [else (local ((define possible-route (find-route (first lo-ori) dest G)))
38 12687dd9 2023-08-04 jrmu (cond
39 12687dd9 2023-08-04 jrmu [(boolean? possible-route) (find-route/list (rest lo-ori) dest G)]
40 12687dd9 2023-08-04 jrmu [else possible-route]))]))