From 124c9f51b5c39b188956f4dad7fedafdfd3bcefd Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 31 Oct 2021 22:19:53 +0100 Subject: [PATCH] Add bellman-ford. --- graph.rkt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/graph.rkt b/graph.rkt index 231753a..9c96332 100644 --- a/graph.rkt +++ b/graph.rkt @@ -39,6 +39,8 @@ min-st-kruskal max-st-kruskal min-st-prim max-st-prim + bellman-ford + graphviz) ;; Wrap the opaque graph structure coming from the generic @@ -194,6 +196,10 @@ (define (max-st-prim g source) (g:max-st-prim (gg g) source)) + ;; 6 Single-source Shortest Paths + (define (bellman-ford g source) + (g:bellman-ford (gg g) source)) + ;; 10 Graphviz (define (graphviz g #:output [output #f] #:colors [colors #f]) (g:graphviz (gg g) #:output output #:colors colors))) @@ -285,6 +291,10 @@ [min-st-prim (-> Graph Any (Listof (List Any Any)))] [max-st-prim (-> Graph Any (Listof (List Any Any)))] + ;; Single-source Shortest Paths + [bellman-ford (-> Graph Any (Values (Mutable-HashTable Any Number) + (Mutable-HashTable Any Any)))] + ;; 10 Graphviz [graphviz (->* (Graph) (#:output Output-Port @@ -408,6 +418,15 @@ (check-equal? (max-st-prim g0 'e) '((a c) (e a) (c d) (c b)))) + (test-case "6 Single-source Shortest Paths" + (define g0 (weighted-graph/directed '((1 a b) (2 a c) (3 b d) (3 c d)))) + + (define-values (bf-dists bf-pred) (bellman-ford g0 'a)) + (check-equal? (hash->ordered-list bf-dists) + '((a . 0) (b . 1) (c . 2) (d . 4))) + (check-equal? (hash->ordered-list bf-pred) + '((a . #f) (b . a) (c . a) (d . b)))) + (test-case "10 Graphviz" (define g (directed-graph '((a b) (b c)))) (check-equal? (graphviz g)