Pictures for the Shelf

Drawing graphs

The graph-drawing phase of the discrete package, shared with logic for automata and Kripke models: layouts, colourings and highlighted paths, and Tutte's exact planar drawing with its check.

What can be drawn

drawGraph(g, options) takes a Graph from @ratmath/discrete (anything with vertices() and edges()) or plain { vertices, edges }. Directed edges get arrowheads, a pair of opposite edges is drawn as two curves, loops sit above their vertex, and weights or labels are written on the edges. colours fills vertices by class (a colouring) and highlight draws edges and vertices bold (a matching, a shortest path, a cut).

Layouts

  • circle, grid and bipartite (with left) place the vertices directly.
  • layered puts each vertex at its distance from the roots and orders each layer by the average place of its neighbours above, in a few sweeps, to cut crossings.
  • tree is a tidy tree: leaves one unit apart, each parent centred over its children.
  • spring is Fruchterman and Reingold's force model from the circle layout, with no random start: reproducible, but floating point, so the figure is marked estimated.

Tutte's drawing, exactly

Fix a face of a planar graph on a convex polygon and put every other vertex at the average of its neighbours:

pv=1deg⁡v∑w∼vpw. p_v = \frac{1}{\deg v} \sum_{w \sim v} p_w.

That is a linear system with a unique solution (its matrix is a Laplacian with some rows fixed), and Tutte proved that for a 3-connected planar graph the drawing has no crossings and every face is convex. tutteLayout(g, { outer }) places the outer face at rational points on the unit circle, (1−t21+t2,2t1+t2)\big(\tfrac{1 - t^2}{1 + t^2}, \tfrac{2t}{1 + t^2}\big), solves the system over the rationals, and then checks every pair of edges for a crossing with exact orientation tests. For K5K_5 and K3,3K_{3,3}, which have no plane drawing, the check fails, as it must. Pass faces from discrete's planarity(g) to use its longest face as the outer one.

Automata and Kripke models

automatonFigure(a) lays out states in layers from the start state, joins the symbols of parallel transitions (a,b), doubles accepting states and draws an entry arrow. kripkeFigure(k) writes under each world the atoms true there.

Try itEdit the code and run it with @ratmath/graphics and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math, and SHOW(svg) draws a picture.
Ctrl/⌘ + Enter runs