1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
package Graph::Nauty;
use strict;
use warnings;
require Exporter;
our @ISA = qw( Exporter );
our @EXPORT_OK = qw(
are_isomorphic
automorphism_group_size
canonical_order
orbits
orbits_are_same
);
our $VERSION = '0.5.3'; # VERSION
our $worksize = 0;
our $warn_deprecated = 1;
require XSLoader;
XSLoader::load('Graph::Nauty', $VERSION);
use Graph::Nauty::EdgeVertex;
use Graph::Undirected;
use Scalar::Util qw(blessed);
sub _cmp
{
my( $a, $b, $sub ) = @_;
if( blessed $a && $a->isa( Graph::Nauty::EdgeVertex:: ) &&
blessed $b && $b->isa( Graph::Nauty::EdgeVertex:: ) ) {
return $a->color cmp $b->color;
} elsif( blessed $a && $a->isa( Graph::Nauty::EdgeVertex:: ) ) {
return 1;
} elsif( blessed $b && $b->isa( Graph::Nauty::EdgeVertex:: ) ) {
return -1;
} else {
return $sub->( $a ) cmp $sub->( $b );
}
}
sub _nauty_graph
{
my( $graph, $color_sub, $order_sub ) = @_;
$color_sub = sub { "$_[0]" } unless $color_sub;
$order_sub = sub { "$_[0]" } unless $order_sub;
die "cannot handle graphs with self-loops\n" if $graph->self_loop_vertices;
if( grep { $graph->has_edge_attributes( @$_ ) } $graph->edges ) {
# colored bonds detected, need to transform the graph
my $graph_now = Graph::Undirected->new( vertices => [ $graph->vertices ] );
for my $edge ( $graph->edges ) {
if( $graph->has_edge_attributes( @$edge ) ) {
my $edge_vertex = Graph::Nauty::EdgeVertex->new( $graph->get_edge_attributes( @$edge ) );
$graph_now->add_edge( $edge->[0], $edge_vertex );
$graph_now->add_edge( $edge_vertex, $edge->[1] );
} else {
$graph_now->add_edge( @$edge );
}
}
$graph = $graph_now;
}
my $nauty_graph = {
nv => scalar $graph->vertices,
nde => scalar $graph->edges * 2, # as undirected
e => [],
d => [],
v => [],
};
my $n = 0;
my $vertices = { map { $_ => { index => $n++, vertex => $_ } }
sort { _cmp( $a, $b, $color_sub ) ||
_cmp( $a, $b, $order_sub ) }
$graph->vertices };
my @breaks;
my $prev;
for my $v (map { $vertices->{$_}{vertex} }
sort { $vertices->{$a}{index} <=>
$vertices->{$b}{index} } keys %$vertices) {
# scalar $graph->neighbours( $v ) cannot be used to get the
# number of neighbours since Graph v0.9717, see
# https://github.com/graphviz-perl/Graph/issues/22
my @neighbours = $graph->neighbours( $v );
push @{$nauty_graph->{d}}, scalar @neighbours;
push @{$nauty_graph->{v}}, scalar @{$nauty_graph->{e}};
push @{$nauty_graph->{original}}, $v;
for (sort { $vertices->{$a}{index} <=> $vertices->{$b}{index} }
@neighbours) {
push @{$nauty_graph->{e}}, $vertices->{$_}{index};
}
if( defined $prev ) {
push @breaks, int(_cmp( $prev, $v, $color_sub ) == 0);
}
$prev = $v;
}
push @breaks, 0;
return ( $nauty_graph, [ 0..$n-1 ], \@breaks );
}
# Converts Graph to dreadnaut input
sub _to_dreadnaut
{
my( $graph, $color_sub, $order_sub ) = @_;
my( $nauty_graph, undef, $breaks ) = _nauty_graph( @_ );
my $out = 'n=' . $nauty_graph->{nv} . " g\n";
my $offset = 0;
my @neighbour_list;
for my $v (0..$nauty_graph->{nv}-1) {
my $neighbour_count = $nauty_graph->{d}[$v];
push @neighbour_list,
join( ' ', @{$nauty_graph->{e}}[$offset..$offset+$neighbour_count-1] );
$offset += $neighbour_count;
}
$out .= join( ";\n", @neighbour_list ) . ".\n";
my $partition = '';
$partition .= 0 if $nauty_graph->{nv};
for (0..$#$breaks-1) {
$partition .= $breaks->[$_] ? ',' : '|';
$partition .= $_ + 1;
}
$out .= "f=[$partition]\n";
return $out;
}
sub automorphism_group_size
{
my( $graph, $color_sub ) = @_;
my $statsblk = sparsenauty( _nauty_graph( $graph, $color_sub ),
undef,
$worksize );
return $statsblk->{grpsize1} * 10 ** $statsblk->{grpsize2};
}
sub orbits
{
my( $graph, $color_sub, $order_sub ) = @_;
my( $nauty_graph, $labels, $breaks ) =
_nauty_graph( $graph, $color_sub, $order_sub );
my $statsblk = sparsenauty( $nauty_graph,
$labels,
$breaks,
undef,
$worksize );
my %orbits;
for my $i (0..$nauty_graph->{nv}-1) {
my $vertex = $nauty_graph->{original}[$i];
next if blessed $vertex && $vertex->isa( Graph::Nauty::EdgeVertex:: );
my $orbit = $statsblk->{orbits}[$i];
push @{$orbits{$orbit}}, $vertex;
}
return map { $orbits{$_} } sort keys %orbits;
}
sub are_isomorphic
{
my( $graph1, $graph2, $color_sub ) = @_;
$color_sub = sub { "$_[0]" } unless $color_sub;
return 0 if !$graph1->could_be_isomorphic( $graph2 );
my @nauty_graph1 = _nauty_graph( $graph1, $color_sub );
my @nauty_graph2 = _nauty_graph( $graph2, $color_sub );
return 0 if $nauty_graph1[0]->{nv} != $nauty_graph2[0]->{nv};
# aresame_sg() seemingly segfaults with empty graphs, thus this is
# a getaround to avoid it:
return 1 if $nauty_graph1[0]->{nv} == 0;
my $statsblk1 = sparsenauty( @nauty_graph1, { getcanon => 1 }, $worksize );
my $statsblk2 = sparsenauty( @nauty_graph2, { getcanon => 1 }, $worksize );
for my $i (0..$nauty_graph1[0]->{nv}-1) {
my $j = $statsblk1->{lab}[$i];
my $k = $statsblk2->{lab}[$i];
return 0 if _cmp( $nauty_graph1[0]->{original}[$j],
$nauty_graph2[0]->{original}[$k],
$color_sub ) != 0;
}
return aresame_sg( $statsblk1->{canon}, $statsblk2->{canon} );
}
sub canonical_order
{
my( $graph, $color_sub, $order_sub ) = @_;
my( $nauty_graph, $labels, $breaks ) =
_nauty_graph( $graph, $color_sub, $order_sub );
my $statsblk = sparsenauty( $nauty_graph,
$labels,
$breaks,
{ getcanon => 1 },
$worksize );
return grep { !blessed $_ || !$_->isa( Graph::Nauty::EdgeVertex:: ) }
map { $nauty_graph->{original}[$_] }
@{$statsblk->{lab}};
}
# DEPRECATED: order of orbits may be different even in isomorphic graphs
sub orbits_are_same
{
my( $graph1, $graph2, $color_sub ) = @_;
$color_sub = sub { "$_[0]" } unless $color_sub;
return 0 if !$graph1->could_be_isomorphic( $graph2 );
warn 'orbits_are_same() is deprecated, as order of orbits may be different ' .
'even in isomorphic graphs' . "\n" if $warn_deprecated;
my @orbits1 = orbits( $graph1, $color_sub );
my @orbits2 = orbits( $graph2, $color_sub );
return 0 if scalar @orbits1 != scalar @orbits2;
for my $i (0..$#orbits1) {
return 0 if scalar @{$orbits1[$i]} != scalar @{$orbits2[$i]};
return 0 if $color_sub->( $orbits1[$i]->[0] ) ne
$color_sub->( $orbits2[$i]->[0] );
}
return 1;
}
1;
__END__
=head1 NAME
Graph::Nauty - Perl bindings for Nauty
=head1 SYNOPSIS
use Graph::Nauty qw(
are_isomorphic
automorphism_group_size
canonical_order
orbits
);
use Graph::Undirected;
my $A = Graph::Undirected->new;
my $B = Graph::Undirected->new;
# Create graphs here
# Get the size of the automorphism group:
print automorphism_group_size( $A );
# Get automorphism group orbits:
print orbits( $A );
# Check whether two graphs are isomorphs:
print are_isomorphic( $A, $B );
# Get canonical order of vertices:
print canonical_order( $A );
=head1 DESCRIPTION
Graph::Nauty provides an interface to Nauty, a set of procedures for
determining the automorphism group of a vertex-coloured graph, and for
testing graphs for isomorphism.
Currently Graph::Nauty only supports
L<Graph::Undirected|Graph::Undirected>, that is, it does not handle
directed graphs. Both colored vertices and edges are accounted for when
determining equivalence classes.
=head2 Vertex color
As L<Graph|Graph> supports any data types as graph vertices, not much
can be inferred about them automatically. For now, Graph::Nauty by
default stringifies every vertex (using Perl C<""> operator) and splits
them into equivalence classes. If different behavior is needed, a custom
anonymous subroutine can be passed inside an option hash:
print orbits( $A, sub { return length $_[0] } );
Subroutine gets a vertex as its 0th parameter, and is expected to return
a string, or anything stringifiable.
In subroutines where the order of returned vertices is important, a
second anonymous subroutine can be passed to order vertices inside each
of the equivalence classes:
print orbits( $A, sub { return length $_[0] }, sub { return "$_[0]" } );
If an ordering subroutine is not given, stringification (Perl C<"">
operator) is used by default.
=head2 Edge color
Edge colors are generated from L<Graph|Graph> edge attributes. Complete
hash of each edge's attributes is stringified (deterministically) and
used to divide edges into equivalence classes.
=head2 Working storage size
Nauty needs working storage, which it does not allocate by itself.
Graph::Nauty follows the advice of the Nauty user guide by allocating
the recommended amount of memory, but for certain graphs this might not
be enough, still. To control that, C<$Graph::Nauty::worksize> could be
used to set the size of memory in the units of Nauty's C<setword>.
=head1 INSTALLING
Building and installing Graph::Nauty from source requires shared library
and C headers for Nauty, which can be downloaded from
L<https://users.cecs.anu.edu.au/~bdm/nauty/>. Both the library and C
headers have to be installed to locations visible by Perl's C compiler.
=head1 SEE ALSO
For the description of Nauty refer to L<http://pallini.di.uniroma1.it>.
=head1 AUTHOR
Andrius Merkys, L<mailto:merkys@cpan.org>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2020 by Andrius Merkys
Graph::Nauty is distributed under the BSD-3-Clause license.
=cut
|