Skip to content

Instantly share code, notes, and snippets.

@vseloved
Last active August 29, 2024 09:07
Show Gist options
  • Select an option

  • Save vseloved/6275d131d27fb873667a95a681168ca8 to your computer and use it in GitHub Desktop.

Select an option

Save vseloved/6275d131d27fb873667a95a681168ca8 to your computer and use it in GitHub Desktop.
Simple generation of graph images using dot (and cl-dot)
(defclass dwim-graph () ())
(defmethod cl-dot:graph-object-node ((graph (eql 'dwim-graph)) object)
(make-instance 'cl-dot:node
:attributes (list :label (format nil "~A" object)
:shape :circle)))
(defmacro dwim-graph ((file &key (directed t)) &body edges)
`(progn
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmethod cl-dot:graph-object-edges ((graph (eql 'dwim-graph)))
,(coerce (mapcar (lambda (edge)
(destructuring-bind (beg end &rest args) edge
(append (list beg end)
(unless directed `((:arrowhead :none)))
args)))))
edges)
'vector)))
(cl-dot:dot-graph
(cl-dot:generate-graph-from-roots 'dwim-graph () '(:rankdir "LR"))
,file
:format (intern (or (etypecase ,file
(pathname (pathname-type ,file))
(string (let ((dot-pos (position #\. ,file
:from-end t)))
(when dot-pos
(subseq ,file (1+ dot-pos))))))
"jpg")
:keyword))
,file))
#+directed-weighted-graph
(dwim-graph ("/tmp/g.jpg")
(0 1 4)
(0 2 4)
(1 3 4)
(1 4 1)
(2 3 2)
(2 4 2)
(3 5 3)
(4 5 5))
#+undirected-unweighted-graph
(dwim-graph ("/tmp/g.jpg" :directed nil)
(0 1)
(0 2)
(1 3)
(1 4)
(2 3)
(2 4)
(3 5)
(4 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment