diff --git a/graph.rkt b/graph.rkt index c441966..65e1d24 100644 --- a/graph.rkt +++ b/graph.rkt @@ -33,7 +33,7 @@ matrix-graph? bfs fewest-vertices-path - dfs + dfs dfs/generalized graphviz) @@ -121,6 +121,34 @@ ;; 4.2 Depth-first Search (define (dfs g) (g:dfs (gg g))) + (define (dfs/generalized + g + #:order [order (λ (x) x)] + #:break [break (λ (g from to acc) #f)] + #:init [init void] + #:inner-init [inner-init (λ (acc) acc)] + #:visit? [custom-visit?-fn #f] + #:prologue [prologue (λ (G u v acc) acc)] + #:epilogue [epilogue (λ (G u v acc) acc)] + #:process-unvisited? [process-unvisited? + (λ (G u v) #f)] + #:process-unvisited [process-unvisited + (λ (G u v acc) acc)] + #:combine [combine (λ (x acc) x)] + #:return [finish (λ (G acc) acc)]) + (g:dfs/generalized + (gg g) + #:order order + #:break break + #:init init + #:inner-init inner-init + #:visit? custom-visit?-fn + #:prologue prologue + #:epilogue epilogue + #:process-unvisited? process-unvisited? + #:process-unvisited process-unvisited + #:combine combine + #:return finish)) ;; 10 Graphviz (define (graphviz g #:output [output #f] #:colors [colors #f]) @@ -179,6 +207,19 @@ [dfs (-> Graph (Values (Mutable-HashTable Any Number) (Mutable-HashTable Any Any) (Mutable-HashTable Any Number)))] + [dfs/generalized (->* (Graph) + (#:order (-> (Listof Any) (Listof Any)) + #:break (-> Graph Any Any Any Boolean) + #:init (-> Graph Void) + #:inner-init (-> Any Any) + #:visit? (-> Graph Any Any Boolean) + #:prologue (-> Graph Any Any Any Any) + #:epilogue (-> Graph Any Any Any Any) + #:process-unvisited? (-> Graph Any Any Boolean) + #:process-unvisited (-> Graph Any Any Any Any) + #:combine (-> Any Any Any) + #:return (-> Graph Any Any)) + Any)] ;; 10 Graphviz [graphviz (->* (Graph) @@ -271,7 +312,10 @@ (check-equal? (hash->ordered-list dfs-pred) '((a . #f) (b . a) (c . #f) (d . c))) (check-equal? (hash->ordered-list dfs-finish) - '((a . 7) (b . 6) (c . 3) (d . 2)))) + '((a . 7) (b . 6) (c . 3) (d . 2))) + + (check-equal? (dfs/generalized (directed-graph '((a b) (a c) (b d) (c d)))) + (void))) (test-case "10 Graphviz" (define g (directed-graph '((a b) (b c))))