Skip to content

Instantly share code, notes, and snippets.

@kmicinski
Created May 30, 2020 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmicinski/a8300d84871706ed19e1b823d71dde5e to your computer and use it in GitHub Desktop.
Save kmicinski/a8300d84871706ed19e1b823d71dde5e to your computer and use it in GitHub Desktop.
;; 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