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