Add floyd-warshall.

This commit is contained in:
Sergiu Ivanov 2021-10-31 22:41:49 +01:00
parent 80be5c48e5
commit caa684d8a9

View file

@ -40,6 +40,7 @@
min-st-kruskal max-st-kruskal min-st-prim max-st-prim
bellman-ford dijkstra dag-shortest-paths
floyd-warshall
graphviz)
@ -204,6 +205,10 @@
(define (dag-shortest-paths g source)
(g:dag-shortest-paths (gg g) source))
;; 7 All-pairs Shortest Paths
(define (floyd-warshall g)
(g:floyd-warshall (gg g)))
;; 10 Graphviz
(define (graphviz g #:output [output #f] #:colors [colors #f])
(g:graphviz (gg g) #:output output #:colors colors)))
@ -303,6 +308,9 @@
[dag-shortest-paths (-> Graph Any (Values (Mutable-HashTable Any Number)
(Mutable-HashTable Any Any)))]
;; 7 All-pairs Shortest Paths
[floyd-warshall (-> Graph (Mutable-HashTable (List Any Any) Number))]
;; 10 Graphviz
[graphviz (->* (Graph)
(#:output Output-Port
@ -447,6 +455,27 @@
(check-equal? (hash->ordered-list dsp-pred)
'((a . #f) (b . a) (c . a) (d . b))))
(test-case "7 All-pairs Shortest Paths"
(define g0 (weighted-graph/directed '((1 a b) (2 a c) (3 b d) (3 c d))))
(check-equal? (hash->ordered-list (floyd-warshall g0))
'(((a d) . 4.0)
((c c) . 0.0)
((b a) . +inf.0)
((a a) . 0.0)
((d c) . +inf.0)
((a c) . 2.0)
((c b) . +inf.0)
((d d) . 0.0)
((c a) . +inf.0)
((b c) . +inf.0)
((d a) . +inf.0)
((c d) . 3.0)
((b d) . 3.0)
((b b) . 0)
((d b) . +inf.0)
((a b) . 1.0))))
(test-case "10 Graphviz"
(define g (directed-graph '((a b) (b c))))
(check-equal? (graphviz g)