New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. ·...

69
Algorithmic Graph Drawing in TikZ with Lua Till Tantau FOSDEM 2015

Transcript of New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. ·...

Page 1: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Algorithmic Graph Drawing in TikZ with LuaTill Tantau

FOSDEM 2015

Page 2: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Which drawing of the graph would you choose?

a

b cd

e

f gh

ij

a

b

cd

ef

g

h

i

j a

b c

d e

f g

h

i

j

Till Tantau FOSDEM 2015 2 / 47

Page 3: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Typography and graph drawing do not mix easily.

INTEGRAL

exp dx

.

2pi +

x r

∫exp

·

2π +

x r

dx

Till Tantau FOSDEM 2015 3 / 47

Page 4: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Typography and graph drawing do not mix easily.

148 M. Chimani et al.

h

f1 f2

f3

f4f5

f6p

1

6

2 34

5

(a) The insertion path p cross a hypernode h.

f1 f2

f3

f4f5

f6

h

p

1 6 5

42 3

(b) A realization of p.

Fig. 2. Splitting a hypernode by introducing an additional arc can reduce the crossings

small downward pieces of arcs, due to port constraints, are restricted to thedirect neighborhood of the corresponding node.

Note that, after the full upward-planarization approach is completed, we canmerge multiple such dummy nodes that correspond to the same hyperarc, whenthey lie in a common face.

Inserting reversed arcs, i.e., a ∈ Rev. To ensure confluency in the hyperarcs, wehave to take special care for the arcs that where reversed in the preprocessingstep to remove cycles. After the drawing is computed, we will have to reset theiroriginal direction. To avoid notational complexity, we will add such arcs onlyafter all other arcs of the hyperarc are already inserted.

Assume the arc a = (x, y) originally connected a source vertex to the hypern-ode of the star-based underlying graph, but got reversed and hence connects thehypernode to a target vertex. Nonetheless we have to ensure that it connects tothe aforementioned subtree Ts, instead of Tt. Let S be the set of original sourcesin Ts before inserting a. Then a confluency-feasible upward insertion path fora can be found by selecting the minimal insertion path from any node in S toy. Thereby it may cross over A◦

φ \ Tt and next to crossing dummies of A◦φ \ Tt

at no cost. The analogous holds, if a originally connected a target vertex to thestar-based hypernode.

Putting it together. So overall, after first computing a special feasible upwardsubgraph, our upward-planarization approach inserts one hyperarc after another.Each hyperarc is inserted by incrementally inserting the arcs of the star-basedunderlying graph, reusing the already established tree-based sub-drawing of thehyperarc as far as possible. By specially considering original arc directions, wethereby guarantee that all hyperarcs are drawn as confluent trees. The finalresult of the planarization is an upward-planar representation (planar, upward-feasible sT -graph) R of G, and hence of H , together with an embedding Γ ofR. Within R, hyperarcs of H are represented as confluent trees, and crossingsare represented as dummy nodes.

Crossing Minimization and Layouts of Directed Hypergraphs 149

1 20

6 8 975

3 4

h2

h4

h3

h1

c2

c1

(a)

1 20

6 8 975

3 4

h2

h4

h3

h1

c2

c1c’1 c’’1

c’2 c’’2

(b)

Fig. 3. Steps towards a final layout: (a) the subgraph between consecutive layers ofan UPR R, (b) fine-layering of the subgraph with included dummy nodes to split longarcs

3 Layout

Using the embedding Γ of the upward-planar planarization (UPR) R computedin the previous step, our layout procedure works in three steps:

1. A layering L of R is computed.2. An initial orthogonal drawing of R is computed.3. Optional step: Orthogonal compaction is applied to remove unnecessary bend

points and improve layout quality.

We finally obtain an upward drawing of R, which induces a drawing of H in astraight-forward way. The number of hyperarc crossings in this drawing equalsthe number of crossing dummies in R, thus it is minimal with respect to thecomputed embedding. Our initial method for orthogonal upward drawing mayproduce an unnecessarily high number of bend points, but it reveals that sucha drawing can be computed efficiently. We discuss the individual steps in moredetail.

Layering. We compute a layering of R in two phases using the layering algorithmby Chimani et al. [8], which also induces a node ordering for each layer. In thefirst phase we compute a layering L′ of the nodes of H , and in the secondphase a layering L′′ of the subgraph between each two consecutive layers of L′.Notice that the nodes of L′′ are either crossing dummies or hypernodes. For eachcrossing dummy c of L′′, we split c by adding new dummy nodes c′ and c′′ suchthat c′ is the immediate left and c′′ is the immediate right neighbor of c. Thesetwo nodes will be bend points in the later drawing and ensure the orthogonalityof the arcs incident to c. We redirect the left incoming and the right outgoing arcof c such that c′ is its new target and c′′ is its new source node, respectively. Wethen merge L′ and L′′ into a complete layering L of R and create dummy nodesto split long edges that span multiple layers. An example is shown in Fig. 3.

Till Tantau FOSDEM 2015 4 / 47

Page 5: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Outline

Graph DrawingAimsSolutions: Force-Based MethodsSolutions: The Sugiyama Method

Graph Drawing in TikZWhat is TikZ?How to Draw a Graph with TikZ

Graph Drawing in TikZ with LuaProgramming in TEXProgramming in LuaHow to Implement a Graph Drawing Algorithm

Till Tantau FOSDEM 2015 5 / 47

Page 6: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Why is the right drawing better than the left one?

a

b cd

e

f gh

ij

a

b

cd

ef

g

h

i

j

Some observations:

1. On the right, there are less crossings.

2. On the right, there are less overlaps.

3. On the right, there are more symmetries.

4. On the right, the edge lengths are similar.

5. On the right, the angles between edges are similar.

Till Tantau FOSDEM 2015 6 / 47

Page 7: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Why is the right drawing better than the left one?

a

b cd

e

f gh

ij

a

b

cd

ef

g

h

i

j

This leads to an optimzation problem: ‘‘Draw the graph such that

1. edge crossings are minimized,

2. node overlaps are minimized,

3. symmetries are maximized,

4. deviations in edge lengths are minimized

5. angular variance is minimized.’’

Till Tantau FOSDEM 2015 6 / 47

Page 8: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

There are many other possible objectives:Important stuff near the center, clustered clusters, . . .

Creative Commons Licence, Author User Calvinius

Till Tantau FOSDEM 2015 7 / 47

Page 9: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Outline

Graph DrawingAimsSolutions: Force-Based MethodsSolutions: The Sugiyama Method

Graph Drawing in TikZWhat is TikZ?How to Draw a Graph with TikZ

Graph Drawing in TikZ with LuaProgramming in TEXProgramming in LuaHow to Implement a Graph Drawing Algorithm

Till Tantau FOSDEM 2015 8 / 47

Page 10: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Mother nature draws graphs beautifully.

Creative Commons Licence, Author IDS.photos from Tiverton, UK

Till Tantau FOSDEM 2015 9 / 47

Page 11: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The force-based approach: Apply forces to nodes iteratively.

Nodes aremovable.

Edges cause forces between nodes.

We simulate the resulting node movements until an equilibrium isreached.

a

b

c

d

ef

g

h

i

j

Till Tantau FOSDEM 2015 10 / 47

Page 12: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The force-based approach: Apply forces to nodes iteratively.

Nodes aremovable.

Edges cause forces between nodes.

We simulate the resulting node movements until an equilibrium isreached.

a

b

cd

e f

g

h

ij

Till Tantau FOSDEM 2015 10 / 47

Page 13: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The force-based approach: Apply forces to nodes iteratively.

Nodes aremovable.

Edges cause forces between nodes.

We simulate the resulting node movements until an equilibrium isreached.

a

b

cd

e f

g

h

i

j

Till Tantau FOSDEM 2015 10 / 47

Page 14: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The force-based approach: Apply forces to nodes iteratively.

Nodes aremovable.

Edges cause forces between nodes.

We simulate the resulting node movements until an equilibrium isreached.

a

b

c

d

e f

g

h

ij

Till Tantau FOSDEM 2015 10 / 47

Page 15: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The force-based approach: Apply forces to nodes iteratively.

Nodes aremovable.

Edges cause forces between nodes.

We simulate the resulting node movements until an equilibrium isreached.

a

b

cd

ef

g

h

i

j

a

b

cd

e

fg

h

i

j

a

b

cd

ef

g

h

i

j

a

b

cd

ef

g

h

i

j

a

b

cd

ef

g

h

i

j

a

b

cd

ef

g

h

i

j

a

b

cd

ef

g

h

i

j

a

b

cd

ef

g

h

i

j

a

b

cd

ef

g

h

i

j

a

b

cd

e

fg

h

i

j

a

b

cd

ef

g

h

i

j

a

b

cd

ef

g

h

i

j

a

b

cd

e

fg

h

i

j

a

b

cd

e

fg

h

i

j

a

b

cd

e

fg

h

i

j

a

b

cd

e

fg

h

i

j

Till Tantau FOSDEM 2015 10 / 47

Page 16: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The force-based approach: Apply forces to nodes iteratively.

Nodes aremovable.

Edges cause forces between nodes.

We simulate the resulting node movements until an equilibrium isreached.

a

b

cd

ef

g

h

i

j

Till Tantau FOSDEM 2015 10 / 47

Page 17: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Forces 1 (Tutte): Spring forces

Public Domain

Edges are springs.

Springs have a naturallength.

If an edge is too short,‘‘it pushes the nodes apart.’’

If an edge is too long,‘‘it pulls the nodestogether.’’

Till Tantau FOSDEM 2015 11 / 47

Page 18: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Forces 1 (Tutte): Spring forces

ab

cda

b

c

d

Till Tantau FOSDEM 2015 11 / 47

Page 19: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Forces 2 (Eades): Electrical forces

Creative Commons License

There are additionalrepulsive forces betweennodes.

Nodes hence tend to formcircles and lines.

Angles tend to be equal.

Till Tantau FOSDEM 2015 12 / 47

Page 20: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Forces 2 (Eades): Electrical forces

a

b

c

d

a

b

c d

Till Tantau FOSDEM 2015 12 / 47

Page 21: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Forces 3: Gravitational forces

Public Domain

Nodes are additionallypulled to the center.

The ‘‘heavy, important’’nodes tend to be in thecenter.

Till Tantau FOSDEM 2015 13 / 47

Page 22: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Forces 3: Gravitational forces

Till Tantau FOSDEM 2015 13 / 47

Page 23: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Forces 4: Magnetic fields

GNU Free Documentation License, Author Gregory F. Maxwell

Edges try to align with thedirection of a force field.

For instance, we can causeedges to becomehorizontal or vertical.

Till Tantau FOSDEM 2015 14 / 47

Page 24: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Summary of force-based algorithms.

Advantages

+ ‘‘There is a force for every aesthetic objective.’’

+ Easy iterative implementation.

+ Edge routing is easily incorporated.

Disadvantages

– Iterative algorithms are slow.

– Difficult to implement well.

– Difficult to reproduce and predict drawings.

Till Tantau FOSDEM 2015 15 / 47

Page 25: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Outline

Graph DrawingAimsSolutions: Force-Based MethodsSolutions: The Sugiyama Method

Graph Drawing in TikZWhat is TikZ?How to Draw a Graph with TikZ

Graph Drawing in TikZ with LuaProgramming in TEXProgramming in LuaHow to Implement a Graph Drawing Algorithm

Till Tantau FOSDEM 2015 16 / 47

Page 26: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Many graphs are layered.

Public Domain

Till Tantau FOSDEM 2015 17 / 47

Page 27: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Many graphs are layered.

Creative Commons License

Till Tantau FOSDEM 2015 17 / 47

Page 28: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Many graphs are layered.

Lesser GNU Public License

Till Tantau FOSDEM 2015 17 / 47

Page 29: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Many graphs are layered.

Creative Commons License

Till Tantau FOSDEM 2015 17 / 47

Page 30: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The Sugiyama method.

On input of a directed graph, do:

1. Make the graph acyclic, if necessary.

2. Assign a layer to each node, so that edges are onlybetween nodes of adjacent layers.

3. Minimize the number of edge crossings.

4. Position the nodes on each layer nicely.

(Unfortunately, all but the last step are NP-complete. . . )

Till Tantau FOSDEM 2015 18 / 47

Page 31: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Sugiyama step 1: Make the graph acyclic

Input

a

b

c

d

e

f

Redirecting edges makes it acyclic

a

b

c

d

e

f

Till Tantau FOSDEM 2015 19 / 47

Page 32: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Sugiyama step 2: Assign layers

a

b f

e c

d

Till Tantau FOSDEM 2015 20 / 47

Page 33: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Sugiyama step 3: Minimize crossings

a

b f

c e

d

Till Tantau FOSDEM 2015 21 / 47

Page 34: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Sugiyama step 4: Position nodes nicely

a

b f

c e

d

Till Tantau FOSDEM 2015 22 / 47

Page 35: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Summary of Sugiyama’s method

Advantages

+ Produces nice drawings of layered graphs.

+ Can handle edges crossing several layers.

+ Extremely fast when good heuristics are used.

Disadvantages

– Difficult implementation.

– Works only for inherently layered graphs.

Till Tantau FOSDEM 2015 23 / 47

Page 36: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Outline

Graph DrawingAimsSolutions: Force-Based MethodsSolutions: The Sugiyama Method

Graph Drawing in TikZWhat is TikZ?How to Draw a Graph with TikZ

Graph Drawing in TikZ with LuaProgramming in TEXProgramming in LuaHow to Implement a Graph Drawing Algorithm

Till Tantau FOSDEM 2015 24 / 47

Page 37: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

TikZ ist kein Zeichenprogramm

Let $\int_0^1 \sqrt{x}\, dx$be the integral\dots

Let∫ 10

√x dx be the

integral. . .

Let \tikz \fill[red](0,0) circle[radius=1mm];

be the circle\dots

Let be thecircle. . .

TikZ is a library of TEXmacros for specifying graphics.

I developed it about 10 years ago in order to produce the 10figures of my PhD thesis.

Today, the manual has over a 1000 pages.

Till Tantau FOSDEM 2015 25 / 47

Page 38: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

How does it work?

The triangle \tikz \draw (0,0)-- (30:10pt) -- (60:10pt) -- cycle; The triangle

TikZ first transforms the code into a series of graphics commands:

\pgfpathmoveto{\pgfpointxy{0}{0}}\pgfpathlineto{\pgfpointpolar{30}{10pt}}\pgfpathlineto{\pgfpointpolar{60}{10pt}}\pgfpathclose\pgfusepath{draw}

Till Tantau FOSDEM 2015 26 / 47

Page 39: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

How does it work?

The triangle \tikz \draw (0,0)-- (30:10pt) -- (60:10pt) -- cycle; The triangle

These, in turn, get transformed into abstract graphics primitives:

\pgfsys@moveto{0pt}{0pt}\pgfsys@lineto{8.660254pt}{5pt}\pgfsys@lineto{5pt}{8.660254pt}\pgfsys@closepath\pgfsys@stroke

Till Tantau FOSDEM 2015 27 / 47

Page 40: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

How does it work?

The triangle \tikz \draw (0,0)-- (30:10pt) -- (60:10pt) -- cycle; The triangle

Finally, these are translated into concrete graphics primitives:

\special{ps:: 0 0 moveto}\special{ps:: 8.627899 4.98132 lineto}\special{ps:: 4.98132 8.627899 lineto}\special{ps:: closepath}\special{ps:: stroke}

(for PostScript output)

Till Tantau FOSDEM 2015 28 / 47

Page 41: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

How does it work?

The triangle \tikz \draw (0,0)-- (30:10pt) -- (60:10pt) -- cycle; The triangle

Finally, these are translated into concrete graphics primitives:

\special{pdf: 0 0 m}\special{pdf: 8.627899 4.98132 l}\special{pdf: 4.98132 8.627899 l}\special{pdf: h}\special{pdf: S}

(for PDF output)

Till Tantau FOSDEM 2015 29 / 47

Page 42: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

How does it work?

The triangle \tikz \draw (0,0)-- (30:10pt) -- (60:10pt) -- cycle; The triangle

Finally, these are translated into concrete graphics primitives:

\special{dvisvgm:raw<path d = " M 0 0

L 8.660254 5L 5 8.660254Z"

style = "stroke">}

(for SVG output)

Till Tantau FOSDEM 2015 30 / 47

Page 43: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Outline

Graph DrawingAimsSolutions: Force-Based MethodsSolutions: The Sugiyama Method

Graph Drawing in TikZWhat is TikZ?How to Draw a Graph with TikZ

Graph Drawing in TikZ with LuaProgramming in TEXProgramming in LuaHow to Implement a Graph Drawing Algorithm

Till Tantau FOSDEM 2015 31 / 47

Page 44: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The TikZ syntax for graphs

A succinct, well-designed syntax for graphs is important whenhumans specify graphs ‘‘by hand’’.

The TikZ syntaxmixes the philosophies of DOT and TikZ.

\tikz \graph {Hello [rounded rectangle]

-> World [tape)-> "$c^2$" [circle, dashed];

World-> "$\delta$" [diamond]-> Hello;

};

Hello

World

c2 δ

Till Tantau FOSDEM 2015 32 / 47

Page 45: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The TikZ syntax for graphs

Node options follow nodes.

Edge options follow edges.

Special notation for edges.

Natural syntax for trees.

\tikz \graph {Hello [rounded rectangle]

-> World [tape]-> "$c^2$" [circle, dashed];

World-> "$\delta$" [diamond]-> Hello;

};

Hello

World

c2 δ

Till Tantau FOSDEM 2015 32 / 47

Page 46: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The TikZ syntax for graphs

Node options follow nodes.

Edge options follow edges.

Special notation for edges.

Natural syntax for trees.

\tikz \graph {Hello [rounded rectangle]

-> World [tape]-> "$c^2$" [circle, dashed];

World->[dashed, blue] "$\delta$"[diamond]->[bend right, "foo"’] Hello;

};

foo

Hello

World

c2 δ

Till Tantau FOSDEM 2015 32 / 47

Page 47: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The TikZ syntax for graphs

Node options follow nodes.

Edge options follow edges.

Special notation for edges.

Natural syntax for trees.

\tikz \graph {a -> b -- c <- d <-> e;

};

a

b c d e

Till Tantau FOSDEM 2015 32 / 47

Page 48: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The TikZ syntax for graphs

Node options follow nodes.

Edge options follow edges.

Special notation for edges.

Natural syntax for trees.

\tikz \graph [binary tree layout] {root -> {left -> {1,2 -> 3 [second]

},right -> {4 -> { , 5 }

}}

};

root

left

1 2

3

right

4

5

Till Tantau FOSDEM 2015 32 / 47

Page 49: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Explicit coordinates . . .

\tikz \graph {Hello [x=0, y=2, rounded rectangle];World [x=2, y=2, tape];"$c^2$" [x=4, y=2, circle, dashed];"$\delta$" [x=4, y=0, diamond];

Hello -> World -> "$c^2$";World -> "$\delta$" -> Hello;

};

Hello World c2

δ

Till Tantau FOSDEM 2015 33 / 47

Page 50: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

. . . versus algorithmic graph drawing.

\usegdlibrary{force}\tikz \graph [spring layout, node distance=1.2cm] {Hello [x=0, y=2, rounded rectangle];World [x=2, y=2, tape];"$c^2$" [x=4, y=2, circle, dashed];"$\delta$" [x=4, y=0, diamond];

Hello -> World -> "$c^2$";World -> "$\delta$" -> Hello;

};

Hello

World

c2

δ

Till Tantau FOSDEM 2015 34 / 47

Page 51: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

. . . versus algorithmic graph drawing.

\usegdlibrary{layered}\tikz \graph [layered layout] {Hello [x=0, y=2, rounded rectangle];World [x=2, y=2, tape];"$c^2$" [x=4, y=2, circle, dashed];"$\delta$" [x=4, y=0, diamond];

Hello -> World -> "$c^2$";World -> "$\delta$" -> Hello;

};

Hello

World

c2 δ

Till Tantau FOSDEM 2015 34 / 47

Page 52: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Graph drawing is even useful in seemingly trivial cases.

x1

x1 + x2

x3

\tikz [>={Stealth[bend] Stealth[bend, red]}]\graph [simple necklace layout, necklace routing] {"$x_1$" -> "$x_1+x_2$" -> "$x_3$" -> "$x_1$"

};

Till Tantau FOSDEM 2015 35 / 47

Page 53: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Graph drawing is even useful in seemingly trivial cases.

x1

x1 + x2

x3

x1

x1 + x2

x3\tikz [>={Stealth[bend] Stealth[bend, red]}]\graph [simple necklace layout, necklace routing] {"$x_1$" -> "$x_1+x_2$" -> "$x_3$" -> "$x_1$"

};

Till Tantau FOSDEM 2015 35 / 47

Page 54: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Outline

Graph DrawingAimsSolutions: Force-Based MethodsSolutions: The Sugiyama Method

Graph Drawing in TikZWhat is TikZ?How to Draw a Graph with TikZ

Graph Drawing in TikZ with LuaProgramming in TEXProgramming in LuaHow to Implement a Graph Drawing Algorithm

Till Tantau FOSDEM 2015 36 / 47

Page 55: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

TEX und LuaTEX

TEX is great, but . . .

implementing large algorithms is practically impossible since

we miss floating point numbers, strings, control structures, arrays,records,modules, . . .

Lua is a minimalistic, elegant language, . . .

. . . that has been integrated into recent TEX versions:

The sum of the first 100 numbers is\newcount\i \newcount\sum\loop\advance\sum by \i\relax\ifnum\i<100\advance\i by 1\relax

\repeat\the\sum

The sum of the first100 numbers is5050

Till Tantau FOSDEM 2015 37 / 47

Page 56: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

TEX und LuaTEX

TEX is great, but . . .

implementing large algorithms is practically impossible since

we miss floating point numbers, strings, control structures, arrays,records,modules, . . .

Lua is a minimalistic, elegant language, . . .

. . . that has been integrated into recent TEX versions:

The sum of the first 100 numbers is\directlua{local sum = 0for i=1,100 dosum = sum + i

endtex.print(sum)

}

The sum of the first100 numbers is5050

Till Tantau FOSDEM 2015 37 / 47

Page 57: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Lua by examples: Hello World.

print "Hello World!"

Lua is an imperative scripting language. . .

. . . that gets you going quickly . . .

. . . and is really tiny (compiler and libraries around 200kB).

Till Tantau FOSDEM 2015 38 / 47

Page 58: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Lua by examples: Variables and types.

local x = 1local y = 2local z = "Hello there"

if 2*x == y thenprint "Ok"

end

if z == "Hello there" thenprint "Ok"

end

The syntax is a bit ‘‘Pascal-like’’.

There are only few types (numbers, strings, functions, tables).

You cannot and need not specify types.

Till Tantau FOSDEM 2015 39 / 47

Page 59: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Lua by examples: Functions.

function factorial (n)if n <= 1 thenreturn 1

elsereturn n*factorial(n-1)

endend

Functions are first-order citizens.

They can be passed around and closures are fully supported.

Till Tantau FOSDEM 2015 40 / 47

Page 60: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Lua by examples: Everything is a (hash) table.

local array1 = { 2, 3, "hallo" }local array2 = { 4, 3, 2, 1 }local record = {start = 1,stop = 2

}

Lua’s ‘‘everything is a table’’ paradigm:An array hashes positive integers to entries.A struct hashes strings to entries(so record.start is syntactic sugar for record["start"]).

Lua’s hash tables areincredibly fast (strings are prehashed, integers are not hashed butinternally form an array),incredibly easy (they grow and shrink automatically, the syntax isvery well designed).

Till Tantau FOSDEM 2015 41 / 47

Page 61: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Lua: What else?

Lua supports coroutines.

Lua supports meta-programming and thereby classes and objects.

Lua does not crash.

Lua does garbage collection.

Lua integrates seamlessly with C in both directions.

Till Tantau FOSDEM 2015 42 / 47

Page 62: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

The interplay of Lua and TikZ.

TikZ Layer

\graph[tree layout]{

a -> b -> c

};

node positioning callback

edge positioning callback

Lua Layer

beginGraphDrawingScope(...)

addNode(...)

addEdge(...)

runGraphDrawingAlgorithm()

load algorithm and run it

endGraphDrawingScope()

Till Tantau FOSDEM 2015 43 / 47

Page 63: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Outline

Graph DrawingAimsSolutions: Force-Based MethodsSolutions: The Sugiyama Method

Graph Drawing in TikZWhat is TikZ?How to Draw a Graph with TikZ

Graph Drawing in TikZ with LuaProgramming in TEXProgramming in LuaHow to Implement a Graph Drawing Algorithm

Till Tantau FOSDEM 2015 44 / 47

Page 64: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Complete code for a very simple graph drawing algorithm.

-- File SimpleDemo.lualocal MyAlgorithmClass = {}function MyAlgorithmClass:run()local g = self.digraphlocal alpha = (2 * math.pi) / #g.verticeslocal radius = g.options.radiusfor i,vertex in ipairs(g.vertices) dovertex.pos.x = radius * math.cos(i * alpha)vertex.pos.y = radius * math.sin(i * alpha)

endend

-- "Publish" the algorithmlocal graph_drawing_framework =require "pgf.gd.interface.InterfaceToAlgorithms"

graph_drawing_framework.declare {key = "simple demo layout",algorithm = MyAlgorithmClass,preconditions = { connected = true }

}

Till Tantau FOSDEM 2015 45 / 47

Page 65: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

We can immediately use the algorithm – no compilation orinstallation is needed.

\usegdlibrary{SimpleDemo}...\tikz \graph [ simple demo layout, radius=1cm ] {a -- b -- c -- a;d -- e;f -- g -- h -- d -- f;e -- g;

};

a

b

c

d

ef

g

h

Till Tantau FOSDEM 2015 46 / 47

Page 66: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

We can immediately use the algorithm – no compilation orinstallation is needed.

\usegdlibrary{SimpleDemo}...\tikz \graph [ simple demo layout, radius=1cm] {a --[orient=right] b -- c -- a;d -- e;f -- g -- h -- d -- f;e -- g;

};

a b

cd

ef

g

h

Till Tantau FOSDEM 2015 46 / 47

Page 67: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

We can immediately use the algorithm – no compilation orinstallation is needed.

\usegdlibrary{SimpleDemo}...\tikz \graph [ simple demo layout, radius=1cm]a --[orient=right] b -- c -- a;d -- e;f -- g -- h -- d --[stub,red] f;e --[stub, red] g;

};

a

b

c

d

ef

g

h

Till Tantau FOSDEM 2015 46 / 47

Page 68: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

We can immediately use the algorithm – no compilation orinstallation is needed.

\usegdlibrary{SimpleDemo}...\tikz \graph [ simple demo layout, radius=1cm,

nodes={circle, fill=..., ...},edges={circle connection bar, ...}] {

a --[orient=right] b -- c -- a;d -- e;f -- g -- h -- d -- f;e -- g;

};

a b

c d

ef

g

h

Till Tantau FOSDEM 2015 46 / 47

Page 69: New Algorithmic Graph Drawing in TikZ with Lua - FOSDEM · 2015. 1. 31. · Typographyandgraphdrawingdonotmixeasily. 148 M. Chimani et al. h f 1 f 2 f 3 f 5 f 4 f 6 p 1 6 2 3 4 5

Summary

Graph drawing is about drawing graphs

quickly,

such that some aesthetic criteria are met and

such that structure in the graphs becomes visible.

Graph drawing in TikZ is directed at

userswho wish to draw graphs as part of TEX documents

and researcherswho implement new algorithms.

Graph drawing in TikZwith Luameans that

algorithms can and must be implement in the Lua language

inside a framework that takes care of common tasks.

Till Tantau FOSDEM 2015 47 / 47