Add bfs.
This commit is contained in:
parent
9c9f9d921d
commit
d51b24a8c4
18
graph.rkt
18
graph.rkt
|
@ -32,6 +32,8 @@
|
||||||
undirected-graph directed-graph
|
undirected-graph directed-graph
|
||||||
matrix-graph?
|
matrix-graph?
|
||||||
|
|
||||||
|
bfs
|
||||||
|
|
||||||
graphviz)
|
graphviz)
|
||||||
|
|
||||||
;; Wrap the opaque graph structure coming from the generic
|
;; Wrap the opaque graph structure coming from the generic
|
||||||
|
@ -108,6 +110,11 @@
|
||||||
(define (matrix-graph? g)
|
(define (matrix-graph? g)
|
||||||
(g:matrix-graph? (gg 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
|
;; 10 Graphviz
|
||||||
(define (graphviz g #:output [output #f] #:colors [colors #f])
|
(define (graphviz g #:output [output #f] #:colors [colors #f])
|
||||||
(g:graphviz (gg g) #:output output #:colors colors)))
|
(g:graphviz (gg g) #:output output #:colors colors)))
|
||||||
|
@ -155,6 +162,11 @@
|
||||||
;; 2.3 Matrix Graphs
|
;; 2.3 Matrix Graphs
|
||||||
[matrix-graph? (-> Graph Boolean)]
|
[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
|
;; 10 Graphviz
|
||||||
[graphviz (->* (Graph)
|
[graphviz (->* (Graph)
|
||||||
(#:output Output-Port
|
(#:output Output-Port
|
||||||
|
@ -230,6 +242,12 @@
|
||||||
;; 2.3 Matrix Graphs
|
;; 2.3 Matrix Graphs
|
||||||
(check-false (matrix-graph? (directed-graph '((a b) (b c))))))
|
(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"
|
(test-case "10 Graphviz"
|
||||||
(define g (directed-graph '((a b) (b c))))
|
(define g (directed-graph '((a b) (b c))))
|
||||||
(check-equal? (graphviz g)
|
(check-equal? (graphviz g)
|
||||||
|
|
Loading…
Reference in New Issue