Add transitive-closure.

This commit is contained in:
Sergiu Ivanov 2021-11-01 09:07:52 +01:00
parent caa684d8a9
commit 8a4d087a73
1 changed files with 23 additions and 2 deletions

View File

@ -40,7 +40,7 @@
min-st-kruskal max-st-kruskal min-st-prim max-st-prim min-st-kruskal max-st-kruskal min-st-prim max-st-prim
bellman-ford dijkstra dag-shortest-paths bellman-ford dijkstra dag-shortest-paths
floyd-warshall floyd-warshall transitive-closure
graphviz) graphviz)
@ -208,6 +208,8 @@
;; 7 All-pairs Shortest Paths ;; 7 All-pairs Shortest Paths
(define (floyd-warshall g) (define (floyd-warshall g)
(g:floyd-warshall (gg g))) (g:floyd-warshall (gg g)))
(define (transitive-closure g)
(g:transitive-closure (gg g)))
;; 10 Graphviz ;; 10 Graphviz
(define (graphviz g #:output [output #f] #:colors [colors #f]) (define (graphviz g #:output [output #f] #:colors [colors #f])
@ -310,6 +312,7 @@
;; 7 All-pairs Shortest Paths ;; 7 All-pairs Shortest Paths
[floyd-warshall (-> Graph (Mutable-HashTable (List Any Any) Number))] [floyd-warshall (-> Graph (Mutable-HashTable (List Any Any) Number))]
[transitive-closure (-> Graph (Mutable-HashTable (List Any Any) Boolean))]
;; 10 Graphviz ;; 10 Graphviz
[graphviz (->* (Graph) [graphviz (->* (Graph)
@ -474,7 +477,25 @@
((b d) . 3.0) ((b d) . 3.0)
((b b) . 0) ((b b) . 0)
((d b) . +inf.0) ((d b) . +inf.0)
((a b) . 1.0)))) ((a b) . 1.0)))
(check-equal? (hash->ordered-list (transitive-closure g0))
'(((a d) . #t)
((c c) . #t)
((b a) . #f)
((a a) . #t)
((d c) . #f)
((a c) . #t)
((c b) . #f)
((d d) . #t)
((c a) . #f)
((b c) . #f)
((d a) . #f)
((c d) . #t)
((b d) . #t)
((b b) . #t)
((d b) . #f)
((a b) . #t))))
(test-case "10 Graphviz" (test-case "10 Graphviz"
(define g (directed-graph '((a b) (b c)))) (define g (directed-graph '((a b) (b c))))