Debian Codegraph @

benchmarks/rnrs-benchmarks/graphs.ss

raw
1 ;;; GRAPHS -- Obtained from Andrew Wright.
2
3 (library (rnrs-benchmarks graphs)
4 (export main)
5 (import (rnrs) (rnrs-benchmarks))
6
7 ;;; ==== util.ss ====
8
9
10 ; Fold over list elements, associating to the left.
11 (define fold
12 (lambda (lst folder state)
13 ; (assert (list? lst)
14 ; lst)
15 ; (assert (procedure? folder)
16 ; folder)
17 (do ((lst lst
18 (cdr lst))
19 (state state
20 (folder (car lst)
21 state)))
22 ((null? lst)
23 state))))
24
25 ; Given the size of a vector and a procedure which
26 ; sends indicies to desired vector elements, create
27 ; and return the vector.
28 (define proc->vector
29 (lambda (size f)
30 ; (assert (and (integer? size)
31 ; (exact? size)
32 ; (>= size 0))
33 ; size)
34 ; (assert (procedure? f)
35 ; f)
36 (if (zero? size)
37 (vector)
38 (let ((x (make-vector size (f 0))))
39 (let loop ((i 1))
40 (if (< i size)
41 (begin
42 (vector-set! x i (f i))
43 (loop (+ i 1)))))
44 x))))
45
46 (define vector-fold
47 (lambda (vec folder state)
48 ; (assert (vector? vec)
49 ; vec)
50 ; (assert (procedure? folder)
51 ; folder)
52 (let ((len
53 (vector-length vec)))
54 (do ((i 0
55 (+ i 1))
56 (state state
57 (folder (vector-ref vec i)
58 state)))
59 ((= i len)
60 state)))))
61
62 ; AZIZ: rnrs has vector-map, this is not even used
63 ;(define vector-map
64 ; (lambda (vec proc)
65 ; (proc->vector (vector-length vec)
66 ; (lambda (i)
67 ; (proc (vector-ref vec i))))))
68
69 ; Given limit, return the list 0, 1, ..., limit-1.
70 (define giota
71 (lambda (limit)
72 ; (assert (and (integer? limit)
73 ; (exact? limit)
74 ; (>= limit 0))
75 ; limit)
76 (let _-*-
77 ((limit
78 limit)
79 (res
80 '()))
81 (if (zero? limit)
82 res
83 (let ((limit
84 (- limit 1)))
85 (_-*- limit
86 (cons limit res)))))))
87
88 ; Fold over the integers [0, limit).
89 (define gnatural-fold
90 (lambda (limit folder state)
91 ; (assert (and (integer? limit)
92 ; (exact? limit)
93 ; (>= limit 0))
94 ; limit)
95 ; (assert (procedure? folder)
96 ; folder)
97 (do ((i 0
98 (+ i 1))
99 (state state
100 (folder i state)))
101 ((= i limit)
102 state))))
103
104 ; Iterate over the integers [0, limit).
105 (define gnatural-for-each
106 (lambda (limit proc!)
107 ; (assert (and (integer? limit)
108 ; (exact? limit)
109 ; (>= limit 0))
110 ; limit)
111 ; (assert (procedure? proc!)
112 ; proc!)
113 (do ((i 0
114 (+ i 1)))
115 ((= i limit))
116 (proc! i))))
117
118 (define natural-for-all?
119 (lambda (limit ok?)
120 ; (assert (and (integer? limit)
121 ; (exact? limit)
122 ; (>= limit 0))
123 ; limit)
124 ; (assert (procedure? ok?)
125 ; ok?)
126 (let _-*-
127 ((i 0))
128 (or (= i limit)
129 (and (ok? i)
130 (_-*- (+ i 1)))))))
131
132 (define natural-there-exists?
133 (lambda (limit ok?)
134 ; (assert (and (integer? limit)
135 ; (exact? limit)
136 ; (>= limit 0))
137 ; limit)
138 ; (assert (procedure? ok?)
139 ; ok?)
140 (let _-*-
141 ((i 0))
142 (and (not (= i limit))
143 (or (ok? i)
144 (_-*- (+ i 1)))))))
145
146 (define there-exists?
147 (lambda (lst ok?)
148 ; (assert (list? lst)
149 ; lst)
150 ; (assert (procedure? ok?)
151 ; ok?)
152 (let _-*-
153 ((lst lst))
154 (and (not (null? lst))
155 (or (ok? (car lst))
156 (_-*- (cdr lst)))))))
157
158
159 ;;; ==== ptfold.ss ====
160
161
162 ; Fold over the tree of permutations of a universe.
163 ; Each branch (from the root) is a permutation of universe.
164 ; Each node at depth d corresponds to all permutations which pick the
165 ; elements spelled out on the branch from the root to that node as
166 ; the first d elements.
167 ; Their are two components to the state:
168 ; The b-state is only a function of the branch from the root.
169 ; The t-state is a function of all nodes seen so far.
170 ; At each node, b-folder is called via
171 ; (b-folder elem b-state t-state deeper accross)
172 ; where elem is the next element of the universe picked.
173 ; If b-folder can determine the result of the total tree fold at this stage,
174 ; it should simply return the result.
175 ; If b-folder can determine the result of folding over the sub-tree
176 ; rooted at the resulting node, it should call accross via
177 ; (accross new-t-state)
178 ; where new-t-state is that result.
179 ; Otherwise, b-folder should call deeper via
180 ; (deeper new-b-state new-t-state)
181 ; where new-b-state is the b-state for the new node and new-t-state is
182 ; the new folded t-state.
183 ; At the leaves of the tree, t-folder is called via
184 ; (t-folder b-state t-state accross)
185 ; If t-folder can determine the result of the total tree fold at this stage,
186 ; it should simply return that result.
187 ; If not, it should call accross via
188 ; (accross new-t-state)
189 ; Note, fold-over-perm-tree always calls b-folder in depth-first order.
190 ; I.e., when b-folder is called at depth d, the branch leading to that
191 ; node is the most recent calls to b-folder at all the depths less than d.
192 ; This is a gross efficiency hack so that b-folder can use mutation to
193 ; keep the current branch.
194 (define fold-over-perm-tree
195 (lambda (universe b-folder b-state t-folder t-state)
196 ; (assert (list? universe)
197 ; universe)
198 ; (assert (procedure? b-folder)
199 ; b-folder)
200 ; (assert (procedure? t-folder)
201 ; t-folder)
202 (let _-*-
203 ((universe
204 universe)
205 (b-state
206 b-state)
207 (t-state
208 t-state)
209 (accross
210 (lambda (final-t-state)
211 final-t-state)))
212 (if (null? universe)
213 (t-folder b-state t-state accross)
214 (let _-**-
215 ((in
216 universe)
217 (out
218 '())
219 (t-state
220 t-state))
221 (let* ((first
222 (car in))
223 (rest
224 (cdr in))
225 (accross
226 (if (null? rest)
227 accross
228 (lambda (new-t-state)
229 (_-**- rest
230 (cons first out)
231 new-t-state)))))
232 (b-folder first
233 b-state
234 t-state
235 (lambda (new-b-state new-t-state)
236 (_-*- (fold out cons rest)
237 new-b-state
238 new-t-state
239 accross))
240 accross)))))))
241
242
243 ;;; ==== minimal.ss ====
244
245
246 ; A directed graph is stored as a connection matrix (vector-of-vectors)
247 ; where the first index is the `from' vertex and the second is the `to'
248 ; vertex. Each entry is a bool indicating if the edge exists.
249 ; The diagonal of the matrix is never examined.
250 ; Make-minimal? returns a procedure which tests if a labelling
251 ; of the verticies is such that the matrix is minimal.
252 ; If it is, then the procedure returns the result of folding over
253 ; the elements of the automoriphism group. If not, it returns #f.
254 ; The folding is done by calling folder via
255 ; (folder perm state accross)
256 ; If the folder wants to continue, it should call accross via
257 ; (accross new-state)
258 ; If it just wants the entire minimal? procedure to return something,
259 ; it should return that.
260 ; The ordering used is lexicographic (with #t > #f) and entries
261 ; are examined in the following order:
262 ; 1->0, 0->1
263 ;
264 ; 2->0, 0->2
265 ; 2->1, 1->2
266 ;
267 ; 3->0, 0->3
268 ; 3->1, 1->3
269 ; 3->2, 2->3
270 ; ...
271 (define make-minimal?
272 (lambda (max-size)
273 ; (assert (and (integer? max-size)
274 ; (exact? max-size)
275 ; (>= max-size 0))
276 ; max-size)
277 (let ((iotas
278 (proc->vector (+ max-size 1)
279 giota))
280 (perm
281 (make-vector max-size 0)))
282 (lambda (size graph folder state)
283 ; (assert (and (integer? size)
284 ; (exact? size)
285 ; (<= 0 size max-size))
286 ; size
287 ; max-size)
288 ; (assert (vector? graph)
289 ; graph)
290 ; (assert (procedure? folder)
291 ; folder)
292 (fold-over-perm-tree (vector-ref iotas size)
293 (lambda (perm-x x state deeper accross)
294 (case (cmp-next-vertex graph perm x perm-x)
295 ((less)
296 #f)
297 ((equal)
298 (vector-set! perm x perm-x)
299 (deeper (+ x 1)
300 state))
301 ((more)
302 (accross state))
303 (else
304 ; (assert #f)
305 (fatal-error "???"))))
306 0
307 (lambda (leaf-depth state accross)
308 ; (assert (eqv? leaf-depth size)
309 ; leaf-depth
310 ; size)
311 (folder perm state accross))
312 state)))))
313
314 ; Given a graph, a partial permutation vector, the next input and the next
315 ; output, return 'less, 'equal or 'more depending on the lexicographic
316 ; comparison between the permuted and un-permuted graph.
317 (define cmp-next-vertex
318 (lambda (graph perm x perm-x)
319 (let ((from-x
320 (vector-ref graph x))
321 (from-perm-x
322 (vector-ref graph perm-x)))
323 (let _-*-
324 ((y
325 0))
326 (if (= x y)
327 'equal
328 (let ((x->y?
329 (vector-ref from-x y))
330 (perm-y
331 (vector-ref perm y)))
332 (cond ((eq? x->y?
333 (vector-ref from-perm-x perm-y))
334 (let ((y->x?
335 (vector-ref (vector-ref graph y)
336 x)))
337 (cond ((eq? y->x?
338 (vector-ref (vector-ref graph perm-y)
339 perm-x))
340 (_-*- (+ y 1)))
341 (y->x?
342 'less)
343 (else
344 'more))))
345 (x->y?
346 'less)
347 (else
348 'more))))))))
349
350
351 ;;; ==== rdg.ss ====
352
353
354 ; Fold over rooted directed graphs with bounded out-degree.
355 ; Size is the number of verticies (including the root). Max-out is the
356 ; maximum out-degree for any vertex. Folder is called via
357 ; (folder edges state)
358 ; where edges is a list of length size. The ith element of the list is
359 ; a list of the verticies j for which there is an edge from i to j.
360 ; The last vertex is the root.
361 (define fold-over-rdg
362 (lambda (size max-out folder state)
363 ; (assert (and (exact? size)
364 ; (integer? size)
365 ; (> size 0))
366 ; size)
367 ; (assert (and (exact? max-out)
368 ; (integer? max-out)
369 ; (>= max-out 0))
370 ; max-out)
371 ; (assert (procedure? folder)
372 ; folder)
373 (let* ((root
374 (- size 1))
375 (edge?
376 (proc->vector size
377 (lambda (from)
378 (make-vector size #f))))
379 (edges
380 (make-vector size '()))
381 (out-degrees
382 (make-vector size 0))
383 (minimal-folder
384 (make-minimal? root))
385 (non-root-minimal?
386 (let ((cont
387 (lambda (perm state accross)
388 ; (assert (eq? state #t)
389 ; state)
390 (accross #t))))
391 (lambda (size)
392 (minimal-folder size
393 edge?
394 cont
395 #t))))
396 (root-minimal?
397 (let ((cont
398 (lambda (perm state accross)
399 ; (assert (eq? state #t)
400 ; state)
401 (case (cmp-next-vertex edge? perm root root)
402 ((less)
403 #f)
404 ((equal more)
405 (accross #t))
406 (else
407 ; (assert #f)
408 (fatal-error "???"))))))
409 (lambda ()
410 (minimal-folder root
411 edge?
412 cont
413 #t)))))
414 (let _-*-
415 ((vertex
416 0)
417 (state
418 state))
419 (cond ((not (non-root-minimal? vertex))
420 state)
421 ((= vertex root)
422 ; (assert
423 ; (begin
424 ; (gnatural-for-each root
425 ; (lambda (v)
426 ; (assert (= (vector-ref out-degrees v)
427 ; (length (vector-ref edges v)))
428 ; v
429 ; (vector-ref out-degrees v)
430 ; (vector-ref edges v))))
431 ; #t))
432 (let ((reach?
433 (make-reach? root edges))
434 (from-root
435 (vector-ref edge? root)))
436 (let _-*-
437 ((v
438 0)
439 (outs
440 0)
441 (efr
442 '())
443 (efrr
444 '())
445 (state
446 state))
447 (cond ((not (or (= v root)
448 (= outs max-out)))
449 (vector-set! from-root v #t)
450 (let ((state
451 (_-*- (+ v 1)
452 (+ outs 1)
453 (cons v efr)
454 (cons (vector-ref reach? v)
455 efrr)
456 state)))
457 (vector-set! from-root v #f)
458 (_-*- (+ v 1)
459 outs
460 efr
461 efrr
462 state)))
463 ((and (natural-for-all? root
464 (lambda (v)
465 (there-exists? efrr
466 (lambda (r)
467 (vector-ref r v)))))
468 (root-minimal?))
469 (vector-set! edges root efr)
470 (folder
471 (proc->vector size
472 (lambda (i)
473 (vector-ref edges i)))
474 state))
475 (else
476 state)))))
477 (else
478 (let ((from-vertex
479 (vector-ref edge? vertex)))
480 (let _-**-
481 ((sv
482 0)
483 (outs
484 0)
485 (state
486 state))
487 (if (= sv vertex)
488 (begin
489 (vector-set! out-degrees vertex outs)
490 (_-*- (+ vertex 1)
491 state))
492 (let* ((state
493 ; no sv->vertex, no vertex->sv
494 (_-**- (+ sv 1)
495 outs
496 state))
497 (from-sv
498 (vector-ref edge? sv))
499 (sv-out
500 (vector-ref out-degrees sv))
501 (state
502 (if (= sv-out max-out)
503 state
504 (begin
505 (vector-set! edges
506 sv
507 (cons vertex
508 (vector-ref edges sv)))
509 (vector-set! from-sv vertex #t)
510 (vector-set! out-degrees sv (+ sv-out 1))
511 (let* ((state
512 ; sv->vertex, no vertex->sv
513 (_-**- (+ sv 1)
514 outs
515 state))
516 (state
517 (if (= outs max-out)
518 state
519 (begin
520 (vector-set! from-vertex sv #t)
521 (vector-set! edges
522 vertex
523 (cons sv
524 (vector-ref edges vertex)))
525 (let ((state
526 ; sv->vertex, vertex->sv
527 (_-**- (+ sv 1)
528 (+ outs 1)
529 state)))
530 (vector-set! edges
531 vertex
532 (cdr (vector-ref edges vertex)))
533 (vector-set! from-vertex sv #f)
534 state)))))
535 (vector-set! out-degrees sv sv-out)
536 (vector-set! from-sv vertex #f)
537 (vector-set! edges
538 sv
539 (cdr (vector-ref edges sv)))
540 state)))))
541 (if (= outs max-out)
542 state
543 (begin
544 (vector-set! edges
545 vertex
546 (cons sv
547 (vector-ref edges vertex)))
548 (vector-set! from-vertex sv #t)
549 (let ((state
550 ; no sv->vertex, vertex->sv
551 (_-**- (+ sv 1)
552 (+ outs 1)
553 state)))
554 (vector-set! from-vertex sv #f)
555 (vector-set! edges
556 vertex
557 (cdr (vector-ref edges vertex)))
558 state)))))))))))))
559
560 ; Given a vector which maps vertex to out-going-edge list,
561 ; return a vector which gives reachability.
562 (define make-reach?
563 (lambda (size vertex->out)
564 (let ((res
565 (proc->vector size
566 (lambda (v)
567 (let ((from-v
568 (make-vector size #f)))
569 (vector-set! from-v v #t)
570 (for-each
571 (lambda (x)
572 (vector-set! from-v x #t))
573 (vector-ref vertex->out v))
574 from-v)))))
575 (gnatural-for-each size
576 (lambda (m)
577 (let ((from-m
578 (vector-ref res m)))
579 (gnatural-for-each size
580 (lambda (f)
581 (let ((from-f
582 (vector-ref res f)))
583 (if (vector-ref from-f m)
584 (gnatural-for-each size
585 (lambda (t)
586 (if (vector-ref from-m t)
587 (vector-set! from-f t #t)))))))))))
588 res)))
589
590
591 ;;; ==== test input ====
592
593 ; Produces all directed graphs with N verticies, distinguished root,
594 ; and out-degree bounded by 2, upto isomorphism.
595
596 (define (run n)
597 (fold-over-rdg n
598 2
599 cons
600 '()))
601
602 (define (main)
603 (run-benchmark
604 "graphs"
605 graphs-iters
606 (lambda (result) (equal? (length result) 596))
607 (lambda (n) (lambda () (run n)))
608 5)))