Created
May 30, 2020 07:26
-
-
Save kmicinski/a8300d84871706ed19e1b823d71dde5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Render a graph in dot format. Graph is a hash from nodes to sets of | |
;; nodes. Terminal nodes need not be included in the domain of graph. | |
(define (graph->dot graph) | |
(define nodes (foldl (lambda (k s) (set-add (set-union (hash-ref graph k) s) k)) (set) (hash-keys graph))) | |
(define nodesl (set->list nodes)) | |
(define node->num | |
(foldl (lambda (i h) (hash-set h (list-ref nodesl i) i)) (hash) (range (length nodesl)))) | |
(displayln "digraph sloggraph {") | |
(for ([i (range (length nodesl))]) | |
(displayln (format "\tn~a [label=\"~a\"];" i (string-replace | |
(string-replace (pretty-format (list-ref nodesl i)) "\n" "\\n") "\"" "\\\"")))) | |
(for ([key (hash-keys graph)]) | |
(for ([value (set->list (hash-ref graph key))]) | |
(displayln (format "\tn~a -> n~a [color=blue];" (hash-ref node->num key) (hash-ref node->num value))))) | |
(displayln "}")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment