[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/1g4wn6Ax    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Apr 1:
; minimum spanning tree: kruskal's algorithm

(define (make-hash hash eql? oops size)
  (let ((table (make-vector size '())))
    (lambda (message . args)
      (if (eq? message 'enlist)
          (let loop ((k 0) (result '()))
            (if (= size k)
                result
                (loop (+ k 1) (append (vector-ref table k) result))))
          (let* ((key (car args))
                 (index (modulo (hash key) size))
                 (bucket (vector-ref table index)))
            (case message
              ((lookup fetch get ref recall)
                (let loop ((bucket bucket))
                  (cond ((null? bucket) oops)
                        ((eql? (caar bucket) key) (cdar bucket))
                        (else (loop (cdr bucket))))))
              ((insert insert! ins ins! set set! store store! install install!)
                (vector-set! table index
                  (let loop ((bucket bucket))
                    (cond ((null? bucket)
                            (list (cons key (cadr args))))
                          ((eql? (caar bucket) key)
                            (cons (cons key (cadr args)) (cdr bucket)))
                          (else (cons (car bucket) (loop (cdr bucket))))))))
              ((delete delete! del del! remove remove!)
                (vector-set! table index
                  (let loop ((bucket bucket))
                    (cond ((null? bucket) '())
                          ((eql? (caar bucket) key)
                            (cdr bucket))
                          (else (cons (car bucket) (loop (cdr bucket))))))))
              ((update update!)
                (vector-set! table index
                  (let loop ((bucket bucket))
                    (cond ((null? bucket)
                            (list (cons key (caddr args))))
                          ((eql? (caar bucket) key)
                            (cons (cons key ((cadr args) key (cdar bucket))) (cdr bucket)))
                          (else (cons (car bucket) (loop (cdr bucket))))))))
              (else (error 'hash-table "unrecognized message")) ))))))

(define (string-hash str)
  (let loop ((cs (string->list str)) (s 0))
    (if (null? cs) s
      (loop (cdr cs) (+ (* s 31)
        (char->integer (car cs)))))))

(define sort #f)
(define merge #f)
(let ()
  (define dosort
    (lambda (pred? ls n)
      (if (= n 1)
          (list (car ls))
          (let ((i (quotient n 2)))
            (domerge pred?
                     (dosort pred? ls i)
                     (dosort pred? (list-tail ls i) (- n i)))))))
  (define domerge
    (lambda (pred? l1 l2)
      (cond
        ((null? l1) l2)
        ((null? l2) l1)
        ((pred? (car l2) (car l1))
         (cons (car l2) (domerge pred? l1 (cdr l2))))
        (else (cons (car l1) (domerge pred? (cdr l1) l2))))))
  (set! sort
    (lambda (pred? l)
      (if (null? l) l (dosort pred? l (length l)))))
  (set! merge
    (lambda (pred? l1 l2)
      (domerge pred? l1 l2))))

(define (make-disjoint-set hash eql? oops size)
  (let ((forest (make-hash hash eql? oops size)))
    (define (make-set item) ; car is parent, cdr is rank
      (forest 'insert item (cons item 0)))
    (define (find item)
      (let ((parent (car (forest 'lookup item))))
        (if (eql? item parent) item
          (let ((x (forest 'lookup (find parent))))
            (forest 'insert item x)
            (car x)))))
    (define (union item1 item2)
      (let* ((root1 (find item1)) (root2 (find item2))
             (rank1 (cdr (forest 'lookup root1)))
             (rank2 (cdr (forest 'lookup root2))))
        (cond ((< rank1 rank2)
                (forest 'insert root1 (cons root2 rank1)))
              ((< rank2 rank1)
                (forest 'insert root2 (cons root1 rank2)))
              ((not (eql? root1 root2))
                (forest 'insert root2 (cons root1 rank2))
                (forest 'insert root1 (cons root1 (+ rank1 1)))))))
    (lambda (message . args)
      (case message
        ((enlist) (forest 'enlist))
        ((make-set) (apply make-set args))
        ((find) (apply find args))
        ((union) (apply union args))))))

(define (kruskal vs es)
  (let ((f (make-disjoint-set string-hash string=? #f 19))
        (n (- (length vs) 1))
        (es (sort (lambda (x y) (< (car x) (car y))) es)))
    (do ((vs vs (cdr vs))) ((null? vs))
      (f 'make-set (car vs)))
    (let loop ((es es) (zs '()) (n n))
      (cond ((zero? n) zs)
            ((string=? (f 'find (cadar es)) (f 'find (caddar es)))
              (loop (cdr es) zs n))
            (else (f 'union (cadar es) (caddar es))
                  (loop (cdr es) (cons (car es) zs) (- n 1)))))))

(define vertices '("A" "B" "C" "D" "E" "F" "G"))
(define edges '((5 "A" "D") (7 "A" "D") (9 "B" "D")
  (8 "B" "C") (5 "C" "E") (7 "B" "E") (15 "D" "E")
  (6 "D" "F") (8 "E" "F") (9 "E" "G") (11 "F" "G")))
(display (kruskal vertices edges))


Output:
1
((9 E G) (8 E F) (7 B E) (6 D F) (5 C E) (5 A D))


Create a new paste based on this one


Comments: