Add bfs.
This commit is contained in:
parent
9c9f9d921d
commit
d51b24a8c4
1 changed files with 18 additions and 0 deletions
18
graph.rkt
18
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)
|
||||
|
|
Loading…
Reference in a new issue