Georg Grasegger^{1} and Jan Legerský^{2,3}

^{1} Johann Radon Institute for Computational and Applied Mathematics (RICAM), Austrian Academy of Sciences

^{2}Johannes Kepler University Linz, Research Institute for Symbolic Computation (RISC)

^{3}Department of Applied Mathematics, Faculty of Information Technology, Czech Technical University in Prague

**Abstract**
In this paper we present the SageMath package FlexRiLoG (short for flexible and rigid labelings of graphs).
Based on recent results the software generates motions of graphs
using special edge colorings.
The package computes and illustrates the colorings and the motions.
We present the structure and usage of the package.
$$
\newcommand{\RR}{\mathbb{R}}
\DeclareMathOperator{\Upairs}{U}
\newcommand{\upairs}[1]{\Upairs(#1)}
\DeclareMathOperator{\CDC}{CDC}
\newcommand{\cdc}[1]{\CDC(#1)}
$$

This notebook is an interactive version of the paper *FlexRiLoG - A SageMath Package for Motions of Graphs* (doi:10.1007/978-3-030-52200-1_44). It can be launched on-line using Binder:

A graph with a placement of its vertices in the plane is considered to be flexible if the placement can be continuously deformed by an edge length preserving motion into a non-congruent placement. The study of such graphs and their motions has a long history (see for instance (Burmester 1893, Dixon 1899, Kempe 1877, Stachel 2013, Walter and Husty 2007, Wunderlich 1954, Wunderlich 1976, Wunderlich 1981)). Recently we provided a series of results (Grasegger, Legerský, and Schicho 2019a, (Grasegger, Legerský, and Schicho 2019b) with a deeper analysis of the existence of flexible placements. This is done via special edge colorings called NAC-colorings (“No Almost Cycles”, see (Grasegger, Legerský, and Schicho 2019a)). These colorings classify the existence of a flexible placement in the plane and give a construction of the motion.

**Basic definitions**

We briefly give a precise definition of flexibility of a graph.
A *framework* is a pair $(G,p)$
where $G=(V_G,E_G)$ is a graph and $p : V_G \rightarrow \RR^2$
is a *placement* of $G$ in $\RR^2$.
The placement might be possibly non-injective but for all edges $uv \in E_G$ we require $p(u) \neq p(v)$.

Two frameworks $(G,p)$ and $(G,q)$ are *equivalent* if
for all $uv \in E_G$,
\begin{align}\label{eq:equivalence}
\| p(u) - p(v) \| = \| q(u) - q(v)\|\,.
\end{align}
Two placements $p,q$ of $G$ are said to be *congruent* if the equation above holds for all pairs of vertices $u,v \in V_G$.
Equivalently, $p$ and $q$ are congruent if there exists a Euclidean isometry $M$ of $\RR^2$ such that $M q(v) = p(v)$ for all $v \in V_G$.

A *flex* of the framework $(G,p)$ is a continuous path $t \mapsto p_t$, $t \in [0,1]$,
in the space of placements of $G$ such that $p_0= p$ and each $(G,p_t)$ is equivalent to $(G,p)$.
The flex is called trivial if $p_t$ is congruent to $p$ for all $t \in [0,1]$.

We define a framework to be *flexible* if there is a non-trivial flex in $\RR^2$.
Otherwise is is called *rigid*.
We say that a labeling $\lambda: E_G\rightarrow \RR_{>0}$ of a graph $G$ is *flexible*
if there is a flexible framework $(G,p)$ such that
$p$ induces $\lambda$, namely, $\|p(u) - p(v) \| = \lambda(uv)$ for all $uv\in E_G$.
On the other hand, $\lambda$ is *rigid* if $(G,p)$ is rigid for all placements $p$ inducing $\lambda$.
A flexible labeling $\lambda$ of a graph is *proper* if there exists a framework $(G,p)$ such that
$p$ induces $\lambda$ and it has a non-trivial flex with all but finitely many placements being injective.
We call a graph *movable* if it has a proper flexible labeling.

**Outline of the paper**

We have given the necessary definitions. Section 2 describes the main functionality of the FlexRiLoG dealing with colorings and motions. In this paper we do not provide the algorithms themselves but refer to the respective theorems and literature. In Section 3 we describe how to use the package to ask for movable graphs.

FlexRiLoG (Grasegger and Legerský 2020a) is a package for SageMath running in versions 8.9 and 9.0 (The Sage Developers 2020). The latest release of the package can be installed by executing:

`sage -pip install --upgrade flexrilog`

The development version of FlexRiLoG can be found in the repository (Grasegger and Legerský 2020b), where also other options of installation are described.

A convenient way of using the package instead of the `sage`

console is a Jupyter notebook
(coming with \sage, launch by `sage -n jupyter`

).
The file `examples/flexrilog_Motions_of_Graphs.ipynb`

in (Grasegger and Legerský 2020b)
provides a Jupyter notebook version of this paper
(see https://jan.legersky.cz/flexrilogICMS2020
redirecting to a version of the notebook executable on-line using Binder).

The package allows to check whether a graph has a NAC-coloring, in particular to list all of them. A motion or flex obtained from a NAC-coloring can be constructed and displayed. Moreover, it implements the results of ((Grasegger, Legerský, and Schicho 2019b)) regarding the existence of proper flexible labelings, namely, the check of a necessary condition and construction of a proper flex from a pair of NAC-colorings. There is also functionality providing tools for classification of all proper flexible labeling, which is out of the scope of this paper (see (Grasegger, Legerský, and Schicho 2020) for details).

The package provides data types in different classes for dealing with graphs, colorings and motions. In order to use the data types provided by the package, they have to be loaded.

In [ ]:

```
from flexrilog import FlexRiGraph, GraphMotion
```

The main object will always be of type `FlexRiGraph`

.
This class inherits properties of the standard `Graph`

from SageMath
and adds specific properties for investigations of flexibility and rigidity.
In this paper we focus on the flexibility part.
A `FlexRiGraph`

can be constructed by the following command.

In [ ]:

```
FlexRiGraph([[0,1],[1,2],[0,2]])
```

Further constructions can be made via integer encoding described in (Capco et al. 2018) and via objects of the standard `Graph`

class.

In [ ]:

```
FlexRiGraph(graphs.CompleteBipartiteGraph(2,3))
```

Besides the class for graphs there is a class for colorings (`NACcoloring`

).
We do not discuss the class itself here but rather show how to compute colorings of graphs (see Section 2.2).
Furthermore, motions are stored in a third class, `GraphMotion`

. They are discussed in Section 2.3.
The `GraphGenerator`

class stores the code for some important graphs from the area of rigidity and flexibility theory.
We do not go into detail but some of the graphs are used in the paper.

NAC-colorings are a special type of edge colorings using two colors. Unlike proper edge colorings in Graph Theory we do not require incident edges to have different colors.

**Definition 1.**
*Let $G$ be a graph. A coloring of edges $\delta\colon E_G\rightarrow \{\text{blue, red}\}$*
*is called a* NAC-coloring,
*if it is surjective and for every cycle in $G$, either all edges have the same color, or there are at least 2 edges in each color.*

FlexRiLoG contains functionality for computing and showing NAC-col-or-ings of a given graph. The standard output is a textual list but the colorings can be shown in figures as well.

In [ ]:

```
C4 = FlexRiGraph([[0,1],[1,2],[2,3],[0,3]])
C4.NAC_colorings()
```

In [ ]:

```
C4.set_pos({0:(0,0), 1:(1,0), 2:(1,1), 3:(0,1)}) # this is to fix the positions of the vertices
```

In [ ]:

```
C4.show_all_NAC_colorings()
```

It can be checked in polynomial time whether a coloring is a NAC-coloring using (Grasegger, Legerský, and Schicho 2019a, Lemma 2.4). Hence, the question if a graph has a NAC-coloring is in NP, but it is subject to further investigations whether there is a polynomial time algorithm for a general graph. For instance every graph that is not generically rigid has a NAC-coloring due to Theorem 1. It can be checked in polynomial time whether a graph is generically rigid (Jacobs and Hendrickson 1997).

In order to compute all NAC-colorings, lists of edges that necessarily have the same color
due to being in 3-cycles (so called *$\triangle$-connected components*, see (Grasegger, Legerský, and Schicho 2019a))
are determined. So far, we then test all possible combinations how to color them by red and blue.
The edges colored the same in the following picture must have the same color in any NAC-coloring,
but no combination satisfies the conditions of NAC-coloring.

In [ ]:

```
from flexrilog import GraphGenerator
N = GraphGenerator.NoNACGraph()
N.has_NAC_coloring()
```

In [ ]:

```
N.plot(show_triangle_components=True)
```

For graphs with symmetries we get many similar colorings, in a sense that after applying the symmetry one NAC-coloring yields the other. We call such NAC-colorings isomorphic. In order to visualize this, NAC-colorings can be named so that isomorphic ones have the same Greek letter but differ by their index.

In [ ]:

```
C4.set_NAC_colorings_names()
C4.NAC_colorings_isomorphism_classes()
```

Given a NAC-coloring we are able to construct a motion. The following result from (Grasegger, Legerský, and Schicho 2019a) describes the relation.

**Theorem 1.**
*A connected non-trivial graph has a flexible labeling if and only if it has a NAC-coloring.*

The main idea to construct a flex is to place the vertices on a grid in such a way that all the edges lie on grid lines.
This can be achieved by placing vertices according to the color component of the graph.
For color components we remove all edges of the other color and then take connected components of the remaining graph.
Then all vertices which lie in the same red component are placed in the same column of the grid and
all vertices from the same blue component are placed in the same row of the grid.
By this procedure each vertex is assigned a unique grid point and all edges of the graph lie on the grid lines.
In FlexRiLoG this can be done with the classmethod `GraphMotion.GridConstruction`

.

In [ ]:

```
from flexrilog import GraphMotion, GraphGenerator
P = GraphGenerator.ThreePrismGraph()
delta = P.NAC_colorings()[0]
motion_P = GraphMotion.GridConstruction(P, delta)
motion_P.parametrization()
```

There is also the option to generate an animated SVG showing the NAC-coloring,
which is automatically displayed when used in a Jupyter notebook (the picture below is a screenshot).
If the `fileName`

is specified, the SVG animation is stored and a web browser can be used to view it.
Note that not all web browsers support SVG animations.
It can be chosen, whether the edges are colored according to the NAC-coloring in use.
The package also distinguishes the vertex layout depending on whether it is drawing a graph having no specific placement properties (dark vertices),
or drawing a motion, in which edge lengths are fixed (light vertices).

In [ ]:

```
motion_P.animation_SVG(edge_partition="NAC",
fileName="3-prism_grid")
```

More generally the base points of the grid can be chosen arbitrarily to get a zig-zag grid. This can be used to avoid degenerate subgraphs. Base points consist of two lists. The standard values consists of lists with points $(i,0)$ and $(0,i)$ respectively. Using them we get a rectangular initial grid. A zig-zag grid in general does not need to be initially rectangular. It is uniquely determined by the base points and drawing parallel lines. Doing so the grid consists of parallelograms. Usually the grid itself is not easily visible from the output motion.

In [ ]:

```
motion_P = GraphMotion.GridConstruction(P, delta,
zigzag=[[[0,0], [3/4,1/2], [2,0]],
[[0,0], [1,0]]])
motion_P.animation_SVG(edge_partition="NAC")
```

Using the grid construction, non-adjacent vertices might overlap, i.e., the constructed framework is not proper. Note, that this cannot be avoided by zig-zag constructions either but depends solely on the NAC-coloring in use. For some graphs all NAC-colorings result in overlapping vertices. In FlexRiLoG it can be checked whether this is the case.

In [ ]:

```
P.has_injective_grid_construction()
```

In [ ]:

```
Q1 = GraphGenerator.Q1Graph()
Q1.has_injective_grid_construction()
```

For some graphs, a proper flexible labeling exists due to the following lemma ((Grasegger, Legerský, and Schicho 2019b)), which relates movability to spatial embeddings.

**Lemma 1.**
*Let $G$ be a graph with an injective embedding $\omega:V_G\rightarrow\RR^3$ such that for every edge $uv\in E_G$, the vector $\omega(u)-\omega(v)$ is parallel to one of the four vectors $(1,0,0)$, $(0,1,0)$, $(0,0,1)$, $(-1,-1,-1)$, and all four directions are present. Then $G$ is movable. Moreover, there exist two NAC-colorings of $G$ such that two edges are parallel in the embedding $\omega$ if and only if they receive the same pair of colors.*

The package tries to construct such a spatial embedding for all pairs of NAC-colorings.

In [ ]:

```
inj, nacs = Q1.has_injective_spatial_embedding(certificate=True); inj
```

In [ ]:

```
graphics_array([d.plot() for d in nacs])
```

From the spatial embedding we can construct a motion of the graph. The motion can be transformed in such a way that a particular edge is fixed.

In [ ]:

```
motion_Q1 = GraphMotion.SpatialEmbeddingConstruction(Q1, nacs)
motion_Q1.fix_edge([5,6])
motion_Q1.parametrization()
```

In [ ]:

```
motion_Q1.animation_SVG()
```

Besides the sufficient condition on movability above, there is also a necessary condition given in ((Grasegger, Legerský, and Schicho 2019b)). For this condition we consider all NAC-colorings and look for monochromatic paths. Adding certain edges according to these paths we get a bigger graph with similar movability properties.

For a graph $G$, let $\upairs{G}$ denote the set of all pairs $\{u,v\}\subset V_G$ such that $uv\notin E_G$ and
there exists a path from $u$ to $v$ which is monochromatic for all NAC-colorings $\delta$ of $G$.

If there exists a sequence of graphs $G_0, \dots, G_n$ such that
$G=G_0$,
$G_i=(V_{G_{i-1}},E_{G_{i-1}} \cup \upairs{G_{i-1}})$ for $i\in\{1,\dots,n\}$, and
$\upairs{G_n}=\emptyset$,
then the graph $G_n$ is called *the constant distance closure of $G$*, denoted by $\cdc{G}$.

**Theorem 2.**
*A graph $G$ is movable if and only if $\cdc{G}$ is movable. Particularly, if $\cdc{G}$ is the complete graph, then $G$ is not movable.*

We can see that the following graph $G$ is not movable ($G_1$ has no NAC-coloring since $\{3,4\},\{5,6\}\in \upairs{G}$, hence, $\upairs{G_1}$ are all non-edges of $G_1$).

In [ ]:

```
G = GraphGenerator.MaxEmbeddingsLamanGraph(7)
G.show_all_NAC_colorings()
```

In [ ]:

```
G.constant_distance_closure().is_complete()
```

We gave a brief overview of the package and therefore did not cover all functionality. The package contains a documentation. As research in the field of flexible and movable graphs is going on the package is further developed, both regarding improvements as well as new functionality (for instance $n$-fold rotationally symmetric frameworks, see (Dewar, Grasegger, and Legerský 2020)). The most current version of FlexRiLoG can be found in (Grasegger and Legerský 2020b).

**Acknowledgments**

This project was supported by the Austrian Science Fund (FWF): P31061, P31888 and W1214-N15, and by the Ministry of Education, Youth and Sports of the Czech Republic, project no. CZ.02.1.01/0.0/ 0.0/16_019/0000778. The project has received funding from the European Union's Horizon 2020 research and innovation programme under the Marie Skłodowska-Curie grant agreement No 675789.

Burmester, L. 1893. "Die Brennpunktmechanismen." *Zeitschrift Für
Mathematik Und Physik* 38: 193–223.

Capco, J., Gallet, M., Grasegger, G., Koutschan, C., Lubbes, N., and Schicho, J. 2018. "An algorithm for computing the number of realizations of a Laman graph." Zenodo. https://doi.org/10.5281/zenodo.1245506.

Dewar, S., Grasegger, G., and Legerský, J. 2020. "Flexible Placements of Graphs with Rotational Symmetry." https://arxiv.org/abs/2003.09328

Dixon, A. C. 1899. "On certain deformable frameworks." *Messenger* 29
(2): 1–21.

Grasegger, G., Legerský, J., and Schicho, J. 2020. "On the Classification of Motions of Paradoxically Movable Graphs." https://arxiv.org/abs/2003.11416

Grasegger, G., and Legerský, J. 2020a. "FlexRiLoG — package for Flexible and Rigid Labelings of Graphs." Zenodo. https://doi.org/10.5281/zenodo.3078757

Grasegger, G., and Legerský, J. 2020b. "FlexRiLoG — package for Flexible and Rigid Labelings of Graphs, repository." https://github.com/Legersky/flexrilog/.

Grasegger, G., Legerský, J., and Schicho, J. 2019a. "Graphs with Flexible
Labelings." *Discrete & Computational Geometry* 62 (2): 461–80.
https://doi.org/10.1007/s00454-018-0026-9.

Grasegger, G., Legerský, J., and Schicho, J. 2019b. "Graphs with Flexible Labelings allowing Injective
Realizations." *Discrete Mathematics* 343 (6), Art. 111713.
https://doi.org/10.1016/j.disc.2019.111713.

Jacobs, D.J., Hendrickson, B. 1997. "An algorithm for two-dimensional rigidity percolation: The pebble game." *Journal of Computational Physics* 137 (2): 346-365.
https://doi.org/10.1006/jcph.1997.5809.

Kempe, A. B. 1877. "On Conjugate Four-piece Linkages." *Proceedings of
the London Mathematical Society* s1-9 (1): 133–49.
https://doi.org/10.1112/plms/s1-9.1.133.

Stachel, H. 2013. "On the Flexibility and Symmetry of Overconstrained
Mechanisms." *Philosophical Transactions of the Royal Society of London
A: Mathematical, Physical and Engineering Sciences* 372.
https://doi.org/10.1098/rsta.2012.0040.

The Sage Developers. 2020. *SageMath, the Sage Mathematics Software
System (Version 9.0)*. https://www.sagemath.org.

Walter, D., and Husty, M. L. 2007. "On a Nine-Bar Linkage, Its Possible
Configurations and Conditions for Paradoxical Mobility." In *12th World
Congress on Mechanism and Machine Science, Iftomm*.

Wunderlich, W. 1954. "Ein merkwürdiges Zwölfstabgetriebe."
*Österreichisches Ingenieur-Archiv* 8: 224–28.

Wunderlich, W. 1976. "On Deformable Nine-Bar Linkages with Six Triple
Joints." *Indagationes Mathematicae (Proceedings)* 79 (3): 257–62.
https://doi.org/10.1016/1385-7258(76)90052-4.

Wunderlich, W. 1981. "Mechanisms Related to Poncelet's Closure Theorem."
*Mechanisms and Machine Theory* 16 (6): 611–20.
https://doi.org/10.1016/0094-114X(81)90067-7.