| 1 |
User's Guide |
| 2 |
=============== |
| 3 |
|
| 4 |
The **pynauty** module provides a Graph class to represent graphs and |
| 5 |
functions for isomorphism testing and computing automorphism groups of |
| 6 |
graphs. The graphs can be undirected or directed. They can contain loops |
| 7 |
but no multiple edges. There is always a vertex-coloring associated with |
| 8 |
them. Ordinary, that is not vertex-colored, graphs can be represented |
| 9 |
with all vertices having the same color. |
| 10 |
|
| 11 |
|
| 12 |
Vertex Coloring |
| 13 |
--------------- |
| 14 |
|
| 15 |
Let V be the set of vertices of a graph G. A partition of vertices is a |
| 16 |
collection of non-empty and pairwise disjoint subsets (parts) of V such |
| 17 |
that the union of these subsets is V. A vertex-coloring defines a |
| 18 |
partition of vertices, with vertices of the same color belonging to the |
| 19 |
same part. Similarly, a partition of vertices defines a vertex-coloring |
| 20 |
by giving the same distinct color to vertices in the same part. So |
| 21 |
vertex-coloring and partition of vertices are essentially the same. |
| 22 |
|
| 23 |
In **pynauty**, a vertex-coloring is specified as an ordered partition |
| 24 |
of vertices. The order of parts is significant but the order of vertices |
| 25 |
within each part is not. Such an ordered partition is represented by a |
| 26 |
list of sets where each set in the list specifies a subset of the vertex |
| 27 |
set. So the color of a vertex is the index of the part containing the |
| 28 |
vertex. The subsets must be non-empty and pairwise disjoint. It is not a |
| 29 |
requirement to cover all vertices, all the vertices not appearing are |
| 30 |
put together in a single part of their own. |
| 31 |
|
| 32 |
The significance of vertex-coloring while computing with graphs in |
| 33 |
pynauty is the following. The partition induced by a vertex coloring |
| 34 |
imposes the restriction on possible automorphisms of the graph that |
| 35 |
vertices must stay in their original part (i.e. keep their color) under |
| 36 |
any automorphism. |
| 37 |
|
| 38 |
Two vertex-colored graphs are *isomorphic* if there is a bijection between |
| 39 |
their vertex sets which preserves adjacency and color. |
| 40 |
|
| 41 |
|
| 42 |
Classes |
| 43 |
------- |
| 44 |
|
| 45 |
.. module:: pynauty |
| 46 |
.. autoclass:: Graph |
| 47 |
:members: |
| 48 |
|
| 49 |
|
| 50 |
Functions |
| 51 |
--------- |
| 52 |
|
| 53 |
.. autofunction:: autgrp |
| 54 |
.. autofunction:: isomorphic |
| 55 |
.. autofunction:: certificate |
| 56 |
.. autofunction:: canon_label |
| 57 |
.. autofunction:: delete_random_edge |
| 58 |
.. autofunction:: Version |
| 59 |
|
| 60 |
|
| 61 |
Examples |
| 62 |
-------- |
| 63 |
|
| 64 |
These examples show using **pynauty** in interactive sessions. We |
| 65 |
assume that the **pynauty** module has been imported by:: |
| 66 |
|
| 67 |
>>> from pynauty import * |
| 68 |
|
| 69 |
Create a Graph object by connetcting some vertices step by step:: |
| 70 |
|
| 71 |
>>> g = Graph(5) |
| 72 |
>>> g.connect_vertex(0, [1, 2, 3]) |
| 73 |
>>> g.connect_vertex(2, [1, 3, 4]) |
| 74 |
>>> g.connect_vertex(4, [3]) |
| 75 |
>>> |
| 76 |
>>> print(g) |
| 77 |
Graph(number_of_vertices=5, directed=False, |
| 78 |
adjacency_dict = { |
| 79 |
0: [1, 2, 3], |
| 80 |
2: [1, 3, 4], |
| 81 |
4: [3], |
| 82 |
}, |
| 83 |
vertex_coloring = [ |
| 84 |
], |
| 85 |
) |
| 86 |
|
| 87 |
The same graph can be created in one step by supplying the adjacency |
| 88 |
relations to Graph:: |
| 89 |
|
| 90 |
>>> g = Graph(number_of_vertices=5, directed=False, |
| 91 |
... adjacency_dict = { |
| 92 |
... 0: [1, 2, 3], |
| 93 |
... 2: [1, 3, 4], |
| 94 |
... 4: [3], |
| 95 |
... }, |
| 96 |
... ) |
| 97 |
>>> |
| 98 |
|
| 99 |
Compute the automorphism group of the graph:: |
| 100 |
|
| 101 |
>>> autgrp(g) |
| 102 |
([[3, 4, 2, 0, 1]], 2.0, 0, [0, 1, 2, 0, 1], 3) |
| 103 |
>>> |
| 104 |
|
| 105 |
Let's add a new edge and see how the automorphism group would change:: |
| 106 |
|
| 107 |
>>> g.connect_vertex(1, [3]) |
| 108 |
>>> print(g) |
| 109 |
Graph(number_of_vertices=5, directed=False, |
| 110 |
adjacency_dict = { |
| 111 |
0: [1, 2, 3], |
| 112 |
1: [3], |
| 113 |
2: [1, 3, 4], |
| 114 |
4: [3], |
| 115 |
}, |
| 116 |
vertex_coloring = [ |
| 117 |
], |
| 118 |
) |
| 119 |
>>> autgrp(g) |
| 120 |
([[0, 1, 3, 2, 4], [1, 0, 2, 3, 4]], 4.0, 0, [0, 0, 2, 2, 4], 3) |
| 121 |
>>> |
| 122 |
|
| 123 |
In the spirit of *Scientific Reproducibility* we might want to record |
| 124 |
what version of the package produced the result: |
| 125 |
|
| 126 |
>>> print('Computed by', Version()) |
| 127 |
Computed by Pynauty version 2.8.8 |
| 128 |
>>> |
| 129 |
|
| 130 |
Fixing vertex 3 by coloring reduces the automorphism group:: |
| 131 |
|
| 132 |
>>> g.set_vertex_coloring([set([3])]) |
| 133 |
>>> print(g) |
| 134 |
Graph(number_of_vertices=5, directed=False, |
| 135 |
adjacency_dict = { |
| 136 |
0: [1, 2, 3], |
| 137 |
1: [3], |
| 138 |
2: [1, 3, 4], |
| 139 |
4: [3], |
| 140 |
}, |
| 141 |
vertex_coloring = [ |
| 142 |
set([3]), |
| 143 |
set([0, 1, 2, 4]), |
| 144 |
], |
| 145 |
) |
| 146 |
>>> autgrp(g) |
| 147 |
([[1, 0, 2, 3, 4]], 2.0, 0, [0, 0, 2, 3, 4], 4) |
| 148 |
>>> |
| 149 |
|
| 150 |
Testing two graphs for isomorphism: |
| 151 |
|
| 152 |
.. code-block:: python |
| 153 |
|
| 154 |
In [8]: print(a) |
| 155 |
Graph(number_of_vertices=13, directed=False, |
| 156 |
adjacency_dict = { |
| 157 |
0: [6, 7, 8, 9], |
| 158 |
1: [6, 7, 8, 11], |
| 159 |
2: [7, 8, 10, 12], |
| 160 |
3: [7, 9, 11, 12], |
| 161 |
4: [8, 10, 11, 12], |
| 162 |
5: [9, 10, 11, 12], |
| 163 |
6: [0, 1, 9, 10], |
| 164 |
7: [0, 1, 2, 3], |
| 165 |
8: [0, 1, 2, 4], |
| 166 |
9: [0, 3, 5, 6], |
| 167 |
10: [2, 4, 5, 6], |
| 168 |
11: [1, 3, 4, 5], |
| 169 |
12: [2, 3, 4, 5], |
| 170 |
}, |
| 171 |
vertex_coloring = [ |
| 172 |
], |
| 173 |
) |
| 174 |
|
| 175 |
In [9]: b = a.copy() |
| 176 |
|
| 177 |
In [10]: delete_random_edge(b) |
| 178 |
Out[10]: (9, 3) |
| 179 |
|
| 180 |
In [11]: print(b) |
| 181 |
Graph(number_of_vertices=13, directed=False, |
| 182 |
adjacency_dict = { |
| 183 |
0: [6, 7, 8, 9], |
| 184 |
1: [6, 7, 8, 11], |
| 185 |
2: [7, 8, 10, 12], |
| 186 |
3: [7, 11, 12], |
| 187 |
4: [8, 10, 11, 12], |
| 188 |
5: [9, 10, 11, 12], |
| 189 |
6: [0, 1, 9, 10], |
| 190 |
7: [0, 1, 2, 3], |
| 191 |
8: [0, 1, 2, 4], |
| 192 |
9: [0, 5, 6], |
| 193 |
10: [2, 4, 5, 6], |
| 194 |
11: [1, 3, 4, 5], |
| 195 |
12: [2, 3, 4, 5], |
| 196 |
}, |
| 197 |
vertex_coloring = [ |
| 198 |
], |
| 199 |
) |
| 200 |
|
| 201 |
In [12]: isomorphic(a,b) |
| 202 |
Out[12]: False |
| 203 |
|