Use lexicographic comparisons for heap algorithms
This commit is contained in:
parent
bcdb95211b
commit
d0967f0580
15
graphgif.scm
15
graphgif.scm
@ -179,6 +179,15 @@
|
||||
;; ALGORITHMS!!! ;;
|
||||
;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(define (lex< a b)
|
||||
(cond
|
||||
[(and (number? a) (number? b)) (< a b)]
|
||||
[(and (null? a) (null? b)) #f]
|
||||
[(and (pair? a) (null? b)) #f]
|
||||
[(and (null? a) (pair? b)) #t]
|
||||
[(= (car a) (car b)) (lex< (cdr a) (cdr b))]
|
||||
[else (< (car a) (car b))]))
|
||||
|
||||
;; Heap algorithms
|
||||
(define-public (heap-insert priority item heap)
|
||||
(define (fix-up! i heap)
|
||||
@ -187,7 +196,7 @@
|
||||
(let* ([parent (quotient (1- i) 2)]
|
||||
[i-priority (car (vector-ref heap i))]
|
||||
[parent-priority (car (vector-ref heap parent))])
|
||||
(when (<= i-priority parent-priority)
|
||||
(when (lex< i-priority parent-priority)
|
||||
(vector-swap! heap i parent))
|
||||
(fix-up! parent heap))))
|
||||
(fix-up! (vector-length heap) (vector-append heap (vector (cons priority item)))))
|
||||
@ -202,8 +211,8 @@
|
||||
[right (1+ left)])
|
||||
(if (< left len)
|
||||
(let ([min (if (< right len)
|
||||
(argmin (compose car (partial vector-ref heap)) i left right)
|
||||
(argmin (compose car (partial vector-ref heap)) i left))])
|
||||
(argmin (compose car (partial vector-ref heap)) lex< i left right)
|
||||
(argmin (compose car (partial vector-ref heap)) lex< i left))])
|
||||
(if (= min i)
|
||||
heap
|
||||
(begin
|
||||
|
Loading…
Reference in New Issue
Block a user