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-intermediate-lambda-reader.ss" "lang")((modname |28.1|) (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 #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
24 ;A path is a list of the form
26 ;where no is a node and lon is a (listof nodes).
31 ;where pa is a path and gr is a graph.
33 ;find-route : node node graph -> (listof nodes) or false
34 ;Given dest, ori, and G, find a route from dest to ori in G and return is as a (listof nodes). The destination and origin are included in the (listof nodes). If no route is available, return false.
36 (define (find-route ori dest G)
38 [(symbol=? ori dest) (list ori)]
39 [else (local ((define possible-route (find-route/list (neighbors ori G) dest G)))
41 [(boolean? possible-route) false]
42 [else (cons ori possible-route)]))]))
44 ;find-route/list : (listof nodes) node graph -> (listof nodes) or false
45 ;Given lo-ori (listof origins), dest, and G, produce a route from some node on lo-ori to dest in G. Return the route as a (listof nodes) or false if no route is available.
47 (define (find-route/list lo-ori dest G)
49 [(empty? lo-ori) false]
50 [else (local ((define possible-route (find-route (first lo-ori) dest G)))
51 (cond [(boolean? possible-route) (find-route/list (rest lo-ori) dest G)]
52 [else possible-route]))]))
54 ;neighbors : node graph -> (listof nodes)
55 ;Given anode and G, find all the neighboring nodes of anode in G. If there are no neighboring nodes, return empty.
57 (define (neighbors anode G)
58 (first (rest (assf (lambda (x) (equal? anode x)) G))))
60 ;; assf : (X -> boolean) (listof (list X Y)) -> (list X Y) or false
61 ;; to find the first item on alop for whose first item p? holds
63 (define (assf op aloxy)
65 [(empty? aloxy) false]
66 [(op (first (first aloxy))) (first aloxy)]
67 [else (assf op (rest aloxy))]))
69 ;(find-route 'A 'G Graph)
70 ;(find-route 'C 'G Graph)
72 ;A node-path is a list
74 ;where no1, no2 are nodes (representing the origin and destination, respectively), and lon is a (listof nodes) representing the route from the origin to the destination.
76 ;test-on-all-nodes : graph -> (listof (listof node-path))
77 ;Tests find-route for all possible pairs of nodes in G. We first generate all possible permutations of node pairs and we apply find-route to each node pair. We then return the resulting (listof node-paths), each node-path being a list containing the origin, destination, and the (listof nodes) taken to get from the origin to the destination.
79 ;find-route : node node graph -> (listof nodes) or false
81 (define (test-on-all-nodes G)
85 (find-route (first x) (second x) G)))
86 (generate-pairs (extract-nodes G))))
88 ;extract-nodes : graph -> (listof nodes)
89 ;Extracts the nodes from G and returns them as a (listof nodes).
91 (define (extract-nodes G)
92 (map (lambda (x) (first x)) G))
94 ;generate-pairs : (listof nodes) -> (listof (listof nodes))
95 ;Generates all possible pairs of nodes from alon and returns it as a (listof (listof nodes)), each element containing a pair of nodes.
97 ;generate-pairs : (listof nodes) (listof nodes) -> (listof (listof nodes))
98 ;Pair the first element of current-lon with the entire complete-lon, and repeat the process to return a (listof (listof nodes)), each element containing a pair of nodes, to give all possible pairings.
101 (define (generate-pairs alon)
102 (local ((define (generate-pairs current-lon complete-lon)
104 [(empty? current-lon) empty]
105 [else (append (pair (first current-lon)
106 (remove (first current-lon) complete-lon))
107 (generate-pairs (rest current-lon) complete-lon))])))
108 (generate-pairs alon alon)))
110 ;pair : node (listof nodes) -> (listof (listof nodes))
111 ;Given anode and alon, generate all possible pairs of anode with elements in alon.
113 (define (pair anode alon)
115 [(empty? alon) empty]
116 [else (cons (list anode (first alon))
117 (pair anode (rest alon)))]))
119 ;remove : X (listof X) -> (listof X)
120 ;Given x and alox, removes the first instance of x in alox and returns the remaining list. If x is not present in alox, simply returns alox.
122 (define (remove x alox)
124 [(empty? alox) empty]
125 [(equal? x (first alox)) (rest alox)]
126 [else (cons (first alox)
127 (remove x (rest alox)))]))
128 (equal? (find-route 'B 'C Graph2) '(B E C))