forked from jeapostrophe/exp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.rkt
111 lines (99 loc) · 1.97 KB
/
bfs.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#lang racket/base
(require racket/list
data/heap)
(define (heap-empty? h)
(zero? (heap-count h)))
(define (best-first-search node-data node-children node-score ? root)
(define (node-<= x y)
(<= (node-score x) (node-score y)))
(define q (make-heap node-<=))
(let visit ([i 0] [n root])
(cond
[(? (node-data n))
(eprintf "found after ~a\n" i)
n]
[else
(heap-add-all! q (node-children n))
(cond
[(heap-empty? q)
#f]
[else
(define next (heap-min q))
(heap-remove-min! q)
(visit (add1 i) next)])])))
(define (bfs node-data node-children ? root)
(best-first-search
node-data node-children
(λ (n) 0)
?
root))
(define (dfs node-data node-children ? root)
(cdr
(best-first-search
(λ (d*n) (node-data (cdr d*n)))
(λ (d*n) (map (λ (c) (cons (add1 (car d*n)) c))
(node-children (cdr d*n))))
(λ (d*n) (* -1 (car d*n)))
?
(cons 0 root))))
(module+ main
(define d
'(1
(2
(3
(4)
(5)
(6
(7)
(8))
(9)
(10)
(11
(12))
(13))
(14
(15))
(16
(17))
(18
(19))
(20))
(21)))
(define look-for-11
(λ (x)
(printf "Visited ~a\n" x)
(= x 11)))
(bfs first
rest
look-for-11
d)
(bfs (λ (x) x)
(λ (x) (list (+ (* 2 x) 0)
(+ (* 2 x) 1)))
look-for-11
1)
(dfs first
rest
look-for-11
d)
(dfs (λ (x) x)
(λ (x)
(if (> x 150)
empty
(list (+ (* 2 x) 0)
(+ (* 2 x) 1))))
look-for-11
1)
(best-first-search
first
rest
(λ (x) (abs (- (first x) 11)))
look-for-11
d)
(best-first-search
(λ (x) x)
(λ (x) (list (+ (* 2 x) 0)
(+ (* 2 x) 1)))
(λ (x) (abs (- x 11)))
look-for-11
1))