Add dfs/generalized.
This commit is contained in:
parent
12fcd54f8d
commit
c9c640cdc8
48
graph.rkt
48
graph.rkt
|
@ -33,7 +33,7 @@
|
||||||
matrix-graph?
|
matrix-graph?
|
||||||
|
|
||||||
bfs fewest-vertices-path
|
bfs fewest-vertices-path
|
||||||
dfs
|
dfs dfs/generalized
|
||||||
|
|
||||||
graphviz)
|
graphviz)
|
||||||
|
|
||||||
|
@ -121,6 +121,34 @@
|
||||||
;; 4.2 Depth-first Search
|
;; 4.2 Depth-first Search
|
||||||
(define (dfs g)
|
(define (dfs g)
|
||||||
(g:dfs (gg 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
|
;; 10 Graphviz
|
||||||
(define (graphviz g #:output [output #f] #:colors [colors #f])
|
(define (graphviz g #:output [output #f] #:colors [colors #f])
|
||||||
|
@ -179,6 +207,19 @@
|
||||||
[dfs (-> Graph (Values (Mutable-HashTable Any Number)
|
[dfs (-> Graph (Values (Mutable-HashTable Any Number)
|
||||||
(Mutable-HashTable Any Any)
|
(Mutable-HashTable Any Any)
|
||||||
(Mutable-HashTable Any Number)))]
|
(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
|
;; 10 Graphviz
|
||||||
[graphviz (->* (Graph)
|
[graphviz (->* (Graph)
|
||||||
|
@ -271,7 +312,10 @@
|
||||||
(check-equal? (hash->ordered-list dfs-pred)
|
(check-equal? (hash->ordered-list dfs-pred)
|
||||||
'((a . #f) (b . a) (c . #f) (d . c)))
|
'((a . #f) (b . a) (c . #f) (d . c)))
|
||||||
(check-equal? (hash->ordered-list dfs-finish)
|
(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"
|
(test-case "10 Graphviz"
|
||||||
(define g (directed-graph '((a b) (b c))))
|
(define g (directed-graph '((a b) (b c))))
|
||||||
|
|
Loading…
Reference in New Issue