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 |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 (define G1
5 '((A (B E))
6 (B (E F))
7 (C (D))
8 (D ())
9 (E (C F))
10 (F (D G))
11 (G ())))
13 (define G2 (vector '(1 4)
14 '(4 5)
15 '(3)
16 empty
17 '(2 5)
18 '(3 6)
19 empty))
21 (define (find-route1 ori dest G)
22 (local ((define (find-route ori dest G)
23 (cond
24 [(symbol=? ori dest) (list ori)]
25 [else (local ((define possible-route (find-route/list (neighbors ori G) dest G)))
26 (cond
27 [(boolean? possible-route) false]
28 [else (cons ori possible-route)]))]))
29 (define (find-route/list lo-ori dest G)
30 (cond
31 [(empty? lo-ori) false]
32 [else (local ((define possible-route (find-route (first lo-ori) dest G)))
33 (cond [(boolean? possible-route) (find-route/list (rest lo-ori) dest G)]
34 [else possible-route]))]))
35 (define (assf op aloxy)
36 (cond
37 [(empty? aloxy) false]
38 [(op (first (first aloxy))) (first aloxy)]
39 [else (assf op (rest aloxy))]))
40 (define (neighbors anode G)
41 (first (rest (assf (lambda (x) (equal? anode x)) G)))))
42 (find-route ori dest G)))
44 (define (find-route2 ori dest G)
45 (local ((define (find-route ori dest G)
46 (cond
47 [(= ori dest) (list ori)]
48 [else (local ((define possible-route (find-route/list (neighbors ori G) dest G)))
49 (cond [(boolean? possible-route) false]
50 [else (cons ori possible-route)]))]))
51 (define (find-route/list lo-ori dest G)
52 (cond
53 [(empty? lo-ori) false]
54 [else (local ((define possible-route (find-route (first lo-ori) dest G)))
55 (cond
56 [(boolean? possible-route) (find-route/list (rest lo-ori) dest G)]
57 [else possible-route]))]))
58 (define (neighbors anode agraph)
59 (vector-ref agraph anode)))
60 (find-route ori dest G)))
62 (define n 100000)
63 (time (andmap (lambda (x) (equal? (find-route1 'A 'G G1)
64 (list 'A 'B 'E 'F 'G))) (build-list n identity)))
65 (time (andmap (lambda (x) (equal? (find-route2 0 6 G2)
66 (list 0 1 4 5 6))) (build-list n identity)))