Debian Codegraph @

texmf-dist/doc/latex/lt3graph/lt3graph.tex

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \iffalse %%%%
2 % %
3 % Copyright (c) 2017 - Michiel Helvensteijn (www.mhelvens.net) %
4 % %
5 % https://github.com/mhelvens/latex-lt3graph %
6 % %
7 % This work may be distributed and/or modified under the conditions %
8 % of the LaTeX Project Public License, either version 1.3 of this %
9 % license or (at your option) any later version. The latest version %
10 % of this license is in http://www.latex-project.org/lppl.txt %
11 % and version 1.3 or later is part of all distributions of LaTeX %
12 % version 2005/12/01 or later. %
13 % %
14 % This work has the LPPL maintenance status 'maintained'. %
15 % %
16 % The Current Maintainer of this work is Michiel Helvensteijn. %
17 % %
18 % This work consists of the files lt3graph.tex and lt3graph.sty. %
19 % %
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \fi %%%%
21
22 \documentclass[a4paper]{lt3graph-packagedoc}
23
24 \usepackage {amssymb}
25 \usepackage {tikz}
26
27 \usetikzlibrary {matrix}
28
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 % Setup %
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32
33 \NoIndentAfterEnv{center}
34
35 \moretexcs{
36 \rightsquigarrow,
37 \draw,
38 \node,
39 \cs_generate_variant:Nn,
40 \int_eval:n,
41 \cs_new:Nn,
42 \clist_new:N,
43 \clist_put_right:Nn,
44 \graph_new:N,
45 \graph_put_vertex:Nn,
46 \graph_put_vertex:Nnn,
47 \graph_put_edge:Nnn,
48 \graph_put_edge:Nnnn,
49 \graph_if_cyclic:NTF,
50 \graph_map_edges_inline:Nn,
51 \graph_map_topological_order_inline:Nn,
52 \graph_map_vertices_inline:Nn,
53 \graph_put_vertex:Nnf,
54 \graph_get_degree:Nn,
55 \graph_map_outgoing_edges_inline:Nnn,
56 \graph_get_vertex:NnNT,
57 \graph_put_edge:Nnnf,
58 \graph_display_table:N,
59 \graph_set_transitive_closure:NNNn,
60 \graph_display_table:Nn,
61 \graph_set_transitive_reduction:NN
62 }
63
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 % Global Changes %
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67
68 \def\extratitlenote{
69 The prefix \texttt{lt3} indicates that this package
70 is a user-contributed \texttt{expl3} library, in contrast to
71 packages prefixed with \texttt{l3}, which are officially supported
72 by the \LaTeX3 team.
73 }
74
75 \changes{0.0.1}{2013/10/08}
76 {initial version}
77
78 \changes{0.0.8}{2013/12/01}
79 {a great many untracked changes}
80
81 \changes{0.0.9}{2013/12/07}
82 {creation of the documentation}
83
84 \changes{0.1.0}{2014/04/13}
85 {fixed a bug in\\
86 \texttt{\textbackslash graph_(g)put_vertex} and\\
87 \texttt{\textbackslash graph_(g)put_edge},
88 which caused their global versions not to work properly}
89
90 \changes{0.1.1}{2014/04/14}
91 {fixed a similar bug in\\
92 \texttt{\textbackslash graph_(g)put_edges_from}}
93
94 \changes{0.1.2}{2014/04/14}
95 {allowing \texttt{\textbackslash graph_map_topological_order_\ldots}
96 to be nested}
97
98 \changes{0.1.3}{2014/04/21}
99 {fixed a bug in \texttt{\textbackslash graph_remove_vertex} and
100 added a \texttt{\textbackslash graph_vertex_count:N} function}
101
102 \changes{0.1.4}{2014/08/31}
103 {no longer loading individual l3kernel packages, which leads to
104 an error for recent versions of expl3}
105
106 \changes{0.1.9}{2017/09/20}
107 {tracking changes in expl3}
108
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 \begin{document} %
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
113 \maketitle
114
115 \section {Introduction} % % % % % % % % % % % % % % % % % % % % % % % % % % % %
116
117 This package provides a data-structure for use in the \LaTeX3 programming
118 environment. It allows you to represent a \emph{directed graph}, which
119 contains \emph{vertices} (nodes), and \emph{edges} (arrows) to
120 connect them.\footnote
121 { Mathematically speaking, a directed graph is a tuple
122 \( (V, E) \) with a set of vertices \( V \) and a set of edges
123 \( E \subseteq V \times V \) connecting those vertices. }
124 One such graph is defined below:
125
126 \begin{latex-example}
127 \ExplSyntaxOn
128 \graph_new:N \l_my_graph
129 \graph_put_vertex:Nn \l_my_graph {v}
130 \graph_put_vertex:Nn \l_my_graph {w}
131 \graph_put_vertex:Nn \l_my_graph {x}
132 \graph_put_vertex:Nn \l_my_graph {y}
133 \graph_put_vertex:Nn \l_my_graph {z}
134 \graph_put_edge:Nnn \l_my_graph {v} {w}
135 \graph_put_edge:Nnn \l_my_graph {w} {x}
136 \graph_put_edge:Nnn \l_my_graph {w} {y}
137 \graph_put_edge:Nnn \l_my_graph {w} {z}
138 \graph_put_edge:Nnn \l_my_graph {y} {z}
139 \graph_put_edge:Nnn \l_my_graph {z} {x}
140 \ExplSyntaxOff
141 \end{latex-example}
142
143 Each vertex is identified by a \emph{key}, which, to this library, is a string:
144 a list of characters with category code 12 and spaces with category code 10.
145 An edge is then declared between two vertices by referring to their keys.
146
147 We could then, for example, use TikZ to draw this graph:
148
149 \begin{latex-example-show}
150 \centering
151 \begin{tikzpicture}[every path/.style={line width=1pt,->}]
152 \newcommand{\vrt}[1]{ \node(#1){\ttfamily\vphantom{Iy}#1}; }
153 \matrix [nodes={circle,draw},
154 row sep=1cm, column sep=1cm,
155 execute at begin cell=\vrt] {
156 v & w & x \\
157 & y & z \\ };
158 \ExplSyntaxOn
159 \graph_map_edges_inline:Nn \l_my_graph
160 { \draw (#1) to (#2); }
161 \ExplSyntaxOff
162 \end{tikzpicture}
163 \end{latex-example-show}
164
165 Just to be clear, this library is \emph{not about drawing} graphs.
166 It does not, inherently, understand any TikZ.
167 It is about \emph{representing} graphs. This allows us to perform analysis
168 on their structure. We could, for example, determine if there is
169 a cycle in the graph:
170
171 \begin{latex-example-show}
172 \ExplSyntaxOn
173 \graph_if_cyclic:NTF \l_my_graph {Yep} {Nope}
174 \ExplSyntaxOff
175 \end{latex-example-show}
176
177 Indeed, there are no cycles in this graph. We can also list
178 its vertices in topological order:
179
180 \begin{latex-example-show}
181 \ExplSyntaxOn
182 \clist_new:N \LinearClist
183 \graph_map_topological_order_inline:Nn \l_my_graph
184 { \clist_put_right:Nn \LinearClist {\texttt{#1}} }
185 \ExplSyntaxOff
186 Visiting dependencies first: \( \LinearClist \)
187 \end{latex-example-show}
188
189 There is a great deal more that can be done with graphs
190 (some of which is even implemented in this library). A
191 common use-case will be to attach data to vertices and/or
192 edges. You could accomplish this with a property map from
193 |l3prop|, but this library has already done that for you!
194 Every vertex and every edge can store arbitrary token lists.\footnote
195 {This makes the mathematical representation of our graphs
196 actually a 4-tuple \( (V, E, v, e) \), where \( v: V \to T\!L \)
197 is a function that maps every vertex to a token list and
198 \( e: E \to T\!L \) is a function that maps every edge (i.e., pair of vertices)
199 to a token list.}
200
201 In the next example we store the \emph{degree} (the number
202 of edges, both incoming and outgoing) of each vertex
203 inside that vertex as data. We then
204 query all vertices directly reachable from \texttt{w}
205 and print their information in the output stream:
206
207 \begin{latex-example}
208 \ExplSyntaxOn
209 \cs_generate_variant:Nn \graph_put_vertex:Nnn {Nnf}
210 \graph_map_vertices_inline:Nn \l_my_graph {
211 \graph_put_vertex:Nnf \l_my_graph {#1}
212 { \graph_get_degree:Nn \l_my_graph {#1} }
213 }
214 \ExplSyntaxOff
215 \end{latex-example}
216
217 It's just an additional parameter on the |\graph_put_vertex| function.
218 Edges can store data in the same way:
219
220 \begin{latex-example}
221 \ExplSyntaxOn
222 \graph_map_edges_inline:Nn \l_my_graph {
223 \graph_put_edge:Nnnn \l_my_graph {#1} {#2}
224 { \int_eval:n{##1 * ##2} }
225 }
226 \ExplSyntaxOff
227 \end{latex-example}
228
229 The values |##1| and |##2| represent the data stored in,
230 respectively, vertices |#1| and |#2|. This is a feature
231 of |\graph_put_edge:Nnnn| added for your convenience.
232
233 \needspace{2\baselineskip}
234 We can show the resulting graph in a table, which is handy for debugging:
235
236 \begin{latex-example-show}
237 \ExplSyntaxOn \centering
238 \graph_display_table:N \l_my_graph
239 \ExplSyntaxOff
240 \end{latex-example-show}
241
242 \fboxsep=1pt
243 The \fcolorbox{black}{green}{\vphantom{Ig}green} cells represent edges
244 directly connecting two vertices. The \fcolorbox{black}{green!40!yellow}{\vphantom{Ig}\tiny (tr)}
245 cells don't have edges, but indicate that there is a sequence of edges connecting
246 two vertices transitively.
247
248 Two vertices can have at most two arrows connecting them:
249 one for each direction. If you want to represent a \emph{multidigraph}
250 (or \emph{quiver}; I'm not making this up),
251 you could consider storing a (pointer to a) list at each edge.
252
253 Finally, we demonstrate some transformation functions.
254 The first generates the transitive closure of a graph:
255
256 \begin{latex-example}
257 \ExplSyntaxOn
258 \graph_new:N \l_closed_graph
259 \cs_new:Nn \__closure_combiner:nnn { #1,~#2,~(#3) }
260 \graph_set_transitive_closure:NNNn
261 \l_closed_graph \l_my_graph
262 \__closure_combiner:nnn {--}
263 \ExplSyntaxOff
264 \end{latex-example}
265 \begin{latex-example-show}
266 \ExplSyntaxOn \centering
267 \graph_display_table:N \l_my_graph
268 \(\ \ \rightsquigarrow\ \ \)
269 \graph_display_table:Nn \l_closed_graph
270 { row_keys = false, vertex_vals = false }
271 \ExplSyntaxOff
272 \end{latex-example-show}
273
274 There is a simpler version (|\graph_set_transitive_closure:NN|)
275 that sets the values of the new edges to the empty token-list.
276 The demonstrated version takes an expandable function to determine the new value,
277 which has access to the values of the two edges being combined (as |#1| and |#2|),
278 as well as the value of the possibly already existing transitive edge (as |#3|).
279 If there was no transitive edge there already, the value passed as |#3| is
280 the fourth argument of the transformation function; in this case |--|.
281
282 The second transformation function generates the transitive reduction:
283
284 \begin{latex-example}
285 \ExplSyntaxOn
286 \graph_new:N \l_reduced_graph
287 \graph_set_transitive_reduction:NN
288 \l_reduced_graph \l_my_graph
289 \ExplSyntaxOff
290 \end{latex-example}
291 \begin{latex-example-show}
292 \ExplSyntaxOn \centering
293 \graph_display_table:N \l_my_graph
294 \(\ \ \rightsquigarrow\ \ \)
295 \graph_display_table:Nn \l_reduced_graph
296 { row_keys = false, vertex_vals = false }
297 \ExplSyntaxOff
298 \end{latex-example-show}
299
300
301
302 \section {API Documentation} % % % % % % % % % % % % % % % % % % % % % % % % %
303
304 Sorry! There is no full API documentation yet. But in the meantime, much of the API
305 is integrated in the examples of the previous section, and everything is
306 documented (however sparsely) in the implementation below.
307
308
309
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 \end{document} %
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%