From d51b24a8c4812fb4378d8d1a4e19db55ab3ffd47 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 13 May 2021 21:48:43 +0200 Subject: [PATCH] Add bfs. --- graph.rkt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/graph.rkt b/graph.rkt index 2cf9d2f..dbd81f4 100644 --- a/graph.rkt +++ b/graph.rkt @@ -32,6 +32,8 @@ undirected-graph directed-graph matrix-graph? + bfs + graphviz) ;; Wrap the opaque graph structure coming from the generic @@ -108,6 +110,11 @@ (define (matrix-graph? g) (g:matrix-graph? (gg g))) + ;; 4 Basic Graph Functions + ;; 4.1 Breadth-first Search + (define (bfs g source) + (g:bfs (gg g) source)) + ;; 10 Graphviz (define (graphviz g #:output [output #f] #:colors [colors #f]) (g:graphviz (gg g) #:output output #:colors colors))) @@ -155,6 +162,11 @@ ;; 2.3 Matrix Graphs [matrix-graph? (-> Graph Boolean)] + ;; 4 Basic Graph Functions + ;; 4.1 Breadth-first Search + [bfs (-> Graph Any (Values (Mutable-HashTable Any Number) + (Mutable-HashTable Any Any)))] + ;; 10 Graphviz [graphviz (->* (Graph) (#:output Output-Port @@ -230,6 +242,12 @@ ;; 2.3 Matrix Graphs (check-false (matrix-graph? (directed-graph '((a b) (b c)))))) + (test-case "4 Basic Graph Functions" + ;; 4.1 Breadth-first Search + (define-values (bfs-lens bfs-tree) (bfs (directed-graph '((a b) (b c))) 'a)) + (check-equal? (hash->ordered-list bfs-lens) '((c . 2) (a . 0) (b . 1))) + (check-equal? (hash->ordered-list bfs-tree) '((a . #f) (b . a) (c . b)))) + (test-case "10 Graphviz" (define g (directed-graph '((a b) (b c)))) (check-equal? (graphviz g)